Skip to main content

Java Data Types

Primitive Data Types

Primitive type is also known as simple data type. In Java programming primitive data is divide into 8 section. 6 data types is relates to numbers, and two data type is related to characters and booleans. In given below table are mentioned primitive data type and their size.

Data Type Bits Bytes Range/Values
byte 8 1 -128 to 127
short 16 2 -32768 to 32767
int 32 4 -(2^31) to (2^31)-1
float 32 4 -(2^31) to (2^31)-1
long 64 8 -(2^63) to (2^63)-1
double 64 8 -(2^63) to (2^63)-1
char 16 2 0 to 65,536 (Unsigned)
boolean true , false

Size of boolean data type is depending of virtual machine. All primitive data types operations are by default defined by the java system. Such as numeric data type can be used to perform all mathematical and comparison operation.

byte

Size of byte data type is equal to 1 byte (8 bit). They can store the value of range in between of(-128 to 127).

short

When we are store whole number which are greater than 127 then we can choose short data type. short data type are hold the 2 byte of memory. And their range is from (-32768 to 32767).

int

Integer is capable to hold the value of whole number of 4 bytes. Their range is from (-2147483648 to 2147483647).

long

Size of long data type are bigger to int data type. That are capable to hold 64 bit signed integer values. Long data literals is form of integer values and that are append with the end of capital L charactor.

long data1 = 1903894L;
long data2 = - 893L; 

float

This type of variable is capable to store Single-precision floating-point format of 32 bit (4 bytes).

float data1 = 123.81F;
float data2 = -100.6F;

double

This type of variable is capable to store double-precision floating-point format of 64 bit (8 bytes).

char

Character data type is single letter which are surrounded by single quotes. Character variable is defined in java programing by using char keyword. In some special case we can assign unicode literals, (escape sequence) value to characters variable..

//character literals in java
class CharType{

  public static void main(String args[]){

    char symbolChar='?';
    char queenUnicode='\u2655'; //unicode literals   
    char scapeSequence ='\''; //scape sequence character
    
    System.out.println(symbolChar); //?
    System.out.println(queenUnicode); // ♕
    System.out.println(scapeSequence); // '
  }
}

Output

?
♕
'

Character data type in java programming is capable to perform basic mathematical operation. And there are possible to assign integer values to character variable that value are internally convert this integer value to character value based on ASCII system. See few examples.

//Example of char data type
class CharType 
{ 
    public static void main(String[] args){ 
      
      char first = 65 ; //Assigning the integer literals

      char second = 'A'+5; //Addition operation
         
        char third = 'Z'-22; //Subtraction operation

      System.out.println("first : " + first);
      System.out.println("second : " + second);
      System.out.println("third : " + third);
    } 
}

Output

first : A
second : F
third : D

boolean

Boolean data type are capable to store true and false value. Generally all comparing expression are produce boolean result. When expression is valid condition then that are produced true value. Otherwise generate false value.

//boolean data type in java
class DataType{

  public static void main(String args[]){

    int speed = 90;

    boolean highSpeed = speed > 80;//logical true
    boolean errorStatus = false;  //set boolean literal

    System.out.println(" High Speed status : "+highSpeed);
    System.out.println(" Error status : "+errorStatus);
  }
}

Default value initialization

In Java Programming you'll are not access an uninitialized variable value. But when this variable is part of instance of class. And if those variable are not assign any initial values when create class objects. Then in this situation compiler are set the default values of class instance variables. See this example.


public class DefaultValue{
  //Declare the primitive variables
  byte month;
  char alphabets;
  short city;
  int age;
  long population;
  float price;
  double salary ;
  boolean status;

  public void record(){

    //Display default values of variable
    System.out.println("byte : "+month);
    System.out.println("char : "+alphabets);
    System.out.println("short : "+city);
    System.out.println("int : "+age);
    System.out.println("long : "+population);
    System.out.println("float : "+price);
    System.out.println("double : "+salary);
    System.out.println("boolean : "+status);

  }

  public static void main(String[] args){
    //Make a object of class
    DefaultValue obj = new DefaultValue();

    //Access class method
    obj.record(); 

  }
}
Default value of primitive data type
byte : 0
char : 
short : 0
int : 0
long : 0
float : 0.0
double : 0.0
boolean : false

literals Values

literals is constant values which are assigning to variables. In this section view basic example of literals format which are supported in Java Programming.

class Literals 
{ 
    public static void main(String[] args){ 
      
      //Assign binary literals value to variable
      byte binary = 0b01010; //10 decimal value

      //Assign decimal literals value to variable
      int decimal = 100; //100 decimal value

      //Assign hexadecimal literals value to variable
      int hexadecimal = 0xC;  //12 decimal value

      //Assign octal literals value to variable
      short octal = 012; //8 decimal value

      //formatted integer 
      double salary = 1_00000.50; //formatted number 1_00000 = 100000 


      System.out.println(binary);
      System.out.println(decimal);
      System.out.println(hexadecimal);
      System.out.println(octal);
      System.out.println(salary);
    } 
}
10
100
12
10
100000.5

Find the capacity of data type : Using wrapper class and MAX_VALUE or MIN_VALUE attributes you'll can find the minimum and maximum value of data type.

//Get min and max value of primitive data type
class CapacityValue 
{ 
    public static void main(String[] args){ 
      
      int maxInt = Integer.MAX_VALUE;   //MAX VALUE of integer
      float minFloat = Float.MIN_VALUE; //MIN VALUE of float
      
      System.out.println("Max Integer : " + maxInt);
      System.out.println("Min Float : " + minFloat);
    } 
}
Max Integer : 2147483647
Min Float : 1.4E-45




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment