Data Types in Java

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 TypeSizeRange
byte1 byte-128 to 127
short2 bytes-32,768 to 32,767
int4 bytes-2,147,483,648 to 2,147,483,647
long8 bytes9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytesfor storing 6 to 7 decimal digits
double8 bytesfor storing 15 decimal digits
boolean1 bittrue or false
char2 bytes0 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:

5
5.99
D
true
Hello


Associativity and Precedence of Operators in Java Previous Next Type Casting in Java