What are Data Types in Java?
According to the name, the type of data can be stored in the variable. Mainly there are two types of languages.- Statically typed language
- Dynamically typed language
Data Types in Java
There are two data types in Java.- Primitive Data Types
- Non-Primitive Data Types
Primitive Data Types
The predefined data types in Java are known as primitive data types. They are 8 data types which are described below.Integer
The integer data type is by default 32 bits signed two’s complement integer.Size
32 bitsDefault
0Range Of Value
-2,147,483,648 to 2,147,483,647Example
class Main {
public static void main(String[] args) {
// declaring int value
int intNumber = -125000;
System.out.println(intNumber);
}
}
Output
-125000
Float
The float data type is a single-precision 32-bit floating point. If you are dealing with large arrays and want to save memory then you can use the float instead of double. We should never use this data type for precise values such as currency.Size
32 bitsDefault
0.0Range Of Value
up to 7 decimal digitsExample
class Main {
public static void main(String[] args) {
// declaring float value
float floatNumber = -32.8f;
System.out.println(floatNumber);
}
}
Output
-32.8
Double
The double data type is a double-precision 64-bit floating point. The default choice for decimal values is this data type. We should never use this data type for precise values such as currency.Size
64 bitsDefault
0.0Range Of Value
up to to 16 decimal digitsExample
class Main {
public static void main(String[] args) {
// declaring double value
double doubleNumber = -24.3;
System.out.println(doubleNumber);
}
}
Output
-24.3
Long
The long data type is by default 64 bits two’s complement integer. If you are dealing with values wider than those provided by the int then use this data type.Size
64 bitsDefault
0Range Of Value
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Example
class Main {
public static void main(String[] args) {
// declaring long value
long longNumber = -423322000000L;
System.out.println(longNumber);
}
}
Output
-423322000000
Byte
The byte data type is 8 bits signed two’s complement integer. When memory saving is a priority then you can use this data type in large arrays.Size
8 bitsDefault
0Range Of Value
-128 to 127Example
class Main {
public static void main(String[] args) {
// declaring byte value
byte range = 100;
System.out.println(range);
}
}
Output
100
Boolean
The boolean data type has two possible values true and false representing 1 bit of information but it’s size is not precisely defined.Size
1 bitDefault
falseRange Of Value
false, trueExample
class Main {
public static void main(String[] args) {
// declaring boolean value
boolean flag = true;
System.out.println(flag);
}
}
Output
true
Char
The char data type is a single 16 bits Unicode character.Size
16 bitsDefault
\u0000 or 0Range Of Value
\u0000 to \uffffExample
class Main {
public static void main(String[] args) {
// declaring char value
char letter = '\u0050';
System.out.println(letter);
}
}
Output
P
Short
The short data type is 16 bits signed two’s complement integer.Size
16 bitsDefault
0Range Of Value
-32,768 to 32,767Example
class Main {
public static void main(String[] args) {
// declaring short value
short temperature = -22;
System.out.println(temperature);
}
}
Output
-22
Non-Primitive Data Types
Those data types that are not predefined in the Java and are created by the programmers such as Strings, Arrays, Classes are called non-primitive data types. They are also known as reference types because they refer to objects.Differences Between Primitive and Non-Primitive Data Types
The main differences between primitive and non-primitive data types are listed below.- Primitive data types are predefined while non-primitive is created by the programmers in Java.
- Many operations can be performed by calling different methods through these non-primitive data types but it’s not possible with primitive data types.
- Non-primitive data types can be null in value but it’s not the case with primitive data types.
- Primitive data types start with a lowercase letter but non-primitive starts with an uppercase letter.
GO TO FULL VERSION