Java Data Types

Variables are reserved memory locations which are used to store values. Different data types are assigned to variables depending on the value it will store in memory. Java has two data types.

  • Primitive Data Type
  • Object Data Type

Primitive Data Types

Primitive data types are predefined in java and there is a unique keyword for each data type. Following are primitive data type examples.

byte

  • 8-bit signed integer
  • Range: -128 to 127
  • Example: byte a = 50

short

  • 16-bit signed integer
  • Range: -32,768 to 32,767
  • Example: short a = 1050

int

  • 32-bit signed integer
  • Range: -2,147,483,648 to 2,147,483,647
  • Example: int a = 100000

long

  • 64-bit signed integer
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • Example: long a = 10000000

float

  • single-precision 32-bit floating point
  • should not use for precise values like currency
  • Example: float a = 453.46

double

  • single-precision 64-bit floating point
  • should not use for precise values like currency
  • Example: float a = 445353.46

boolean

  • 1-bit
  • possible values (true and false)
  • Example: boolean a = true

char

  • 16-bit single Unicode character
  • Example: char a =’A’

Reference Data Types

Reference data types are used to create and access objects. The reference variables are declared with a specific data type and it cannot be changed e.g. Car, Employee, Dog etc.

Example: Employee newEmployee = new Employee(“Jhon”);

Leave a Reply

Your email address will not be published. Required fields are marked *