Java Data Types
In this tutorial, we will learn about Java data types, especially primitive data types in Java, with the help of examples.
Java Data Types
In Java, a variable must have a defined data type.
Let us see an example:
int myNumb = 9; // Integer
float myFloatNumb = 6.31f; // Floating point number
boolean myBool = true; // Boolean
chat myLetter = 'B'; // Character
String myText = "Hello World"; // String
Data types in Java are divided into groups:
- Primitive data types -
byte
,short
,int
,long
,float
,double
,boolean
andchar
- Non-primitive data types - such as
String
,Arrays
,Classes
, etc.
Primitive Data Types
A primitive data type defines the type and the size of variable values, and it has no additional methods.
Java offers eight primitive data types:
Data Type | Size | Description |
---|---|---|
byte | 1 byte | Saves whole numbers from -128 to 127 |
short | 2 bytes | Saves whole numbers from -32,768 to 32,767 |
int | 4 bytes | Saves whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Saves whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Saves fractional numbers. Sufficient for saving 6 to 7 decimal digits |
double | 8 bytes | Saves fractional numbers. Sufficient for saving 15 decimal digits |
boolean | 1 bit | Saves true or false values |
char | 2 bytes | Saves a single character/letter or ASCII values |
Numbers
Primitive number types are split into two groups:
- Integer types saves whole numbers, positive or negative (like 321 or -641), without decimals. Valid types are
byte
,short
,int
, andlong
. - Floating point types represents numbers with a fractional part, containing one or more decimals.
Note: In Java, the most used data types for numbers are
int
(for whole numbers) anddouble
(for floating point numbers).
Integer Types
Byte
The byte
data type can save whole numbers from -128 to 127.
A byte
can be used instead of int
or other integer types to save memory when using the value between -127 and 127.
Example:
byte myNum = 112;
System.out.println(myNum);
Output:
112
Short
The short
data type can save whole numbers from -32768 to 32767.
Example:
short myNum = 7300;
System.out.println(myNum);
Output:
7300
Int
The int
data type can save while numbers from -2147483648 to 2147483647.
The int
data type is the most used data type to create variables with a numeric value.
Example:
int myNum = 24245100;
System.out.println(myNum);
Output:
24245100
Long
The long
data type can save whole numbers from -9223372036854775808 to 9223372036854775807.
The long
data type is used when the int
is not large enough to save the value.
For the long
data type, we need to end the value with an "L".
Example:
long myNum = 321500000001200L;
System.out.println(myNum);
Output:
321500000001200
Floating Point Types
The floating point type is used when we need a number with a decimal, such as 7.62 or 43.215454.
Float
The float
data type can save fractional numbers from 3.4e-038 to 3.4e+038.
To use the float
data type, we need to end the value with an "f".
Example:
float myNum = 7.21f;
System.out.println(myNum);
Output:
7.21
Double
The double
data type can save fractional numbers from 1.7e−308 to 1.7e+308.
To use the double
data type, we need to end the value with a "d".
Example:
double myNum = 23.85f;
System.out.println(myNum);
Output:
23.85
When to use
float
ordouble
? The precision offloat
is only six or seven decimal digits, whiledouble
data types have a precision of about 15 digits. So it is safer to usedouble
for most calculations.
Scientific Numbers
A floating point number can also be a scientific number with an e
to designate the power of 10.
Example:
float myf1 = 19e2f;
double myd1 = 14E3d;
System.out.println(myf1);
System.out.println(myd1);
Output:
1900.0
14000.0
Booleans
We use the boolean
keyword to declare a boolean data type.
A boolean
data type can only take the values true
or false
.
Boolean values are generally used for conditional testing.
Example:
boolean isSunnyDay = true;
boolean isWindyDay = False;
System.out.println(isSunnyDay); // Outputs true
System.out.println(isWindyDay); // Outputs false
Output:
true
false
Characters
The char
data type is used to save a single character.
The character must be surrounded by single quotes, like 'B' or 'd'.
Example:
char myChar = 'D';
System.out.println(myChar);
Output:
D
We can also use ASCII values to print certain characters.
Example:
char a = 80, b = 87, c = 72;
System.out.println(a);
System.out.println(b);
System.out.println(c);
Output:
P
W
H
Strings
The String
data type is used to save a sequence of characters.
String values need to be surrounded by double quotes "your text"
.
Example:
String hello = "Hello Wrold!";
System.out.println(hello);
Output:
Hello World!
The String is one of the most used and integrated data types in Java. In Java, the String is a non-primitive data type because it refers to an object. It also has methods that are used to perform certain operations.
Non-Primitive Data Types
Non-primitive data types are also called reference types because they refer to objects.
The difference between primitive and non-primitive date types can be given as follows:
- Primitive types are predefined. Non-primitive types are created by the developer and are not defined by Java (
String
is an exception). - Primitive types cannot be used to call methods to perform specific operations, while non-primitive types can call them.
- A primitive type always has a value, while non-primitive types can be
null
. - A primitive type begins with a lowercase letter, while a non-primitive type begins with an uppercase letter.
- The size of a primitive type depends on the data type, while non-primitive types have all the same size.
Examples of non-primitive types are Strings
, Arrays
, Classes
, Interface
, etc. We will learn more about non-primitive types in a later chapter.