Signup/Sign In

Data Types in Java

Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.

In java, data types are classified into two catagories :

  1. Primitive Data type
  2. Non-Primitive Data type

1) Primitive Data type

A primitive data type can be of eight types :

Primitive Data types
charbooleanbyte shortintlongfloat double

Once a primitive data type has been declared its type can never change, although in most cases its value can change. These eight primitive type can be put into four groups


Integer

This group includes byte, short, int, long

byte : It is 1 byte(8-bits) integer data type. Value range from -128 to 127. Default value zero. example: byte b=10;

short : It is 2 bytes(16-bits) integer data type. Value range from -32768 to 32767. Default value zero. example: short s=11;

int : It is 4 bytes(32-bits) integer data type. Value range from -2147483648 to 2147483647. Default value zero. example: int i=10;

long : It is 8 bytes(64-bits) integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;


Example:

Lets create an example in which we work with integer type data and we can get idea how to use datatype in the java program.

	
package corejava;

public class Demo{      
    
    public static void main(String[] args) {
    	// byte type
    	byte b = 20;
    	System.out.println("b= "+b);
    	
    	// short type
    	short s = 20;
    	System.out.println("s= "+s);
    	
    	// int type
    	int i = 20;
    	System.out.println("i= "+i);
    	
    	// long type
    	long l = 20;
    	System.out.println("l= "+l);
    	
   
    }  
}  
	

b= 20 s= 20 i= 20 l= 20

Floating-Point Number

This group includes float, double

float : It is 4 bytes(32-bits) float data type. Default value 0.0f. example: float ff=10.3f;

double : It is 8 bytes(64-bits) float data type. Default value 0.0d. example: double db=11.123;


Example:

In this example, we used floating point type and declared variables to hold floating values. Floating type is useful to store decimal point values.

	
public class Demo{      
    
    public static void main(String[] args) {
    	// float type
    	float f = 20.25f;
    	System.out.println("f= "+f);
    	
    	// double type
    	double d = 20.25;
    	System.out.println("d= "+d);
   
    }  
}  
	

f= 20.25 d= 20.25

Characters

This group represent char, which represent symbols in a character set, like letters and numbers.

char : It is 2 bytes(16-bits) unsigned unicode character. Range 0 to 65,535. example: char c='a';

Char Type Example

Char type in Java uses 2 bytes to unicode characters. Since it works with unicode then we can store alphabet character, currency character or other characters that are comes under the unicode set.

    
public class Demo {

    public static void main(String[] args) {


        char ch = 'S';
        System.out.println(ch);
        
        char ch2 = '&';
        System.out.println(ch2);
        
        char ch3 = '$';
        System.out.println(ch3);

    }

}
    

S & $

Boolean

This group represent boolean, which is a special type for representing true/false values. They are defined constant of the language. example: boolean b=true;

Boolean Type Example

Boolean type in Java works with two values only either true or false. It is mostly use in conditional expressions to perform conditional based programming.

    
public class Demo {

    public static void main(String[] args) {

        boolean t = true;
        System.out.println(t);
        
        boolean f = false;
        System.out.println(f);

    }

}
    

true false

2) Non-Primitive(Reference) Data type

A reference data type is used to refer to an object. A reference variable is declare to be of specific and that type can never be change.

For example: String str, here str is a reference variable of type String. String is a class in Java. We will talk a lot more about reference data type later in Classes and Object lesson.

Reference type are used to hold reference of an object. Object can be instance of any class or entity.

In object oriented system, we deal with object that stores properties. To refer those objects we use reference types.

We will talk a lot more about reference data type later in Classes and Object lesson.


Identifiers in Java

All Java components require names. Name used for classes, methods, interfaces and variables are called Identifier. Identifier must follow some rules. Here are the rules:

  • All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an underscore.
  • After the first character, an identifier can have any combination of characters.
  • Java keywords cannot be used as an identifier.
  • Identifiers in Java are case sensitive, foo and Foo are two different identifiers.

Some valid identifiers are: int a, class Car, float amount etc.