As we learnt about variables, you may remember that every variable must be specified data type. So now, lets talk about about data types in Java.
Lets recall the code we used before:
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
Here you can see many data types. Lets talk about it.
There are two types of data in Java. They are:
- Primitive data types
- Non-primitive data types
Types of data
Do you know why data types are so important? Because it says what variable is going to hold. So that operations rule and memory allocation for variable will be done accordingly.
Primitive Data Types
| Data Type | Size | Range |
|---|---|---|
| byte | 1 byte | -128 to 127 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| long | 8 bytes | 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| float | 4 bytes | for storing 6 to 7 decimal digits |
| double | 8 bytes | for storing 15 decimal digits |
| boolean | 1 bit | true or false |
| char | 2 bytes | 0 to 65,535 (ASCII) |
Non Primitive Data Types
They are string, array, method, class and objects which we will talk later in coming topics.
Example:
Code:
public class DataTypes {
public static void main(String[] args) {
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
System.out.println(myNum);
System.out.println(myFloatNum);
System.out.println(myLetter);
System.out.println(myBool);
System.out.println(myText);
}
}
Note that f should be used after value in float data type.
Output:
Associativity and Precedence of Operators in Java Previous Next Type Casting in Java

