Data Types in Java

Data types represent the different values to be stored in the variable.

Java is statically typed and also a strongly typed language because in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.

Java has two categories of data types :
1 ) Primitive data Type (e.g., number, character etc).
2 ) Non Primitive Data Type/Reference Data Type / Object data (programmer created types)


Primitive data type

These are the data type which are provided with language. There 8 primitive data types supported by the Java programming

Data Type Size Default Value
byte 1 byte 0
short 2 byte 0
char 2 byte '\u0000' , ' '
int 4 byte 0
float 4 byte 0.0f , 0.0
long 8 byte 0l , 0
double 8 byte 0.0d , 0.0
boolean 1bit (on widows OS) false

Example of Data Types and Variable Declarations in Java


class DataTypes{
     public static void main(String args[]){
        byte byteVar = 5;
        short shortVar = 20;
        int intVar = 30;
        long longVar = 60;
        float floatVar = 20;
        double doubleVar = 20.123;
        boolean booleanVar = true;
        char charVar ='W';
    
        System.out.println("Value Of byte Variable is " + byteVar);
        System.out.println("Value Of short Variable is " + shortVar);
        System.out.println("Value Of int Variable is " + intVar);
        System.out.println("Value Of long Variable is " + longVar);
        System.out.println("Value Of float Variable is " + floatVar);
        System.out.println("Value Of double Variable is " + doubleVar);
        System.out.println("Value Of boolean Variable is " + booleanVar);
        System.out.println("Value Of char Variable is " + charVar);
     }
 }
Value Of byte Variable is 5
Value Of short Variable is 20
Value Of int Variable is 30
Value Of long Variable is 60
Value Of float Variable is 20.0
Value Of double Variable is 20.123
Value Of boolean Variable is true
Value Of char Variable is W

Why char uses 2 byte in java and what is \u0000 ?

It is because java support Unicode character set system than ASCII code system. The \u0000 is the lowest range of Unicode system.

Explanation : Java support more than 18 international languages so java take 2 byte for characters, because for 18 international language 1 byte of memory is not sufficient for storing all characters and symbols present in 18 languages. Java supports Unicode but c support ascii code. In ascii code only English language are present, so for storing all English latter and symbols 1 byte is sufficient. Unicode character set is one which contains all the characters which are available in 18 international languages and it contains 65536 characters.

Why Boolean data types take 1 bit of memory ?

Boolean data type takes zero bytes of main memory space because Boolean data type of java implemented by Sun Micro System with a concept of flip - flop. A flip - flop is a general purpose register which stores one bit of information (one true and zero false).

Note : In C, C++ (Turbo) Boolean data type is not available for representing true false values but a true value can be treated as non-zero value and false values can be represented by zero.

Non Primitive Data Type /Reference Data Type

User/Programmer defined data type is known as Non-Primitive DataType. All Java Classes either it is predefined class or user defined class are the Non-Primitive data types.
for example : String , Array, Exception ,Thread etc.
Let us create our own data type :

//example to create Non-Primitive DataType
class MyDataType
{
 int a;
 double b;
 public void show()
 {
 	System.out.println("Value of a ="+a);
 	System.out.println("Value of b ="+b);
 }
}

Using MyDataType in main method


//using MyDataType in main 
class Example
{
	public static void main(String[] args) {
	//declearing variable of type MyDataType
	MyDataType d;
	//now initailizing
	d=new MyDataType();
	//setting value for d
	d.a=5;
	d.b=56;
	d.show();
	}
}
Value of a =5
Value of b =56.0
woman avatar