Home / Java / Data Types in Java

Data Types in Java

Variables are memory locations used to store values. Based on the data type of the variable operating system specifies, which kind of data and how much variable reserves the space in memory.

Data Types in Java

There are mainly two data types available in Java.

  • Primitive Data Type
  • Reference/Object Data Types

Primitive Data Types

Primitive data types are the language supported data types, There are eight types of Primitive Data Types in Java

Byte

  • Byte data type consist of 8bit in memory.
  • Minimum values that can be stored in Byte -128 (-2 ^ 7)
  • Maximum Values that can be stored in Byte 127

As we already discussed about the Java Garbage Collector. So the default value assigned to Byte is 0

Short

  • Short data type consist of 16bit or 2Byte
  • Minimum value that Short data type can accommodate -32,768 (-2^15)
  • Maximum values that Short data type can store 32,767  (2^15 -1)
  • Short data type can be used to save memory. Because an integer is 2 times lesser than int
  • Default Value 0

int

  • int data type consist of 32bits or 4 Bytes.
  • Minimum value that can be stored in an int variable is – 2,147,483,648 (-2^31),
  • Maximum value that can be stored in an int variable is 2,147,483,647(2^31 -1),
  • int data type is used where there is no concern with memory.
  • Default Value 0

long

  • long data type is 64bits or 8 Bytes.
  • Minimum value is -9,223,372,036,854,775,808(-2^63),
  • Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1),
  • This data type is used when an int is not able to fulfill need.
  • Default Value 0

float

  • float data type consist of 32bit or 4 Bytes.
  • float data type is used to store floating values.
  • Default value is 0.0f

double

  • double data type is 2 x float 64bit or 8 bytes.
  • double data type is used where float is not sufficient to store the floating value.

boolean

  • boolean data type consist of 1bit.
  • There are only two possible values boolean can store true or false.
  • Default value is false.

char

  • char data type is single 16bit Unicode character.
  • Minimum value is ‘\u0000’.
  • Maximum value is ‘\uffff’.
  • Char data type is used to store any character.

Reference Data Types

  • Classes, objects and various types of array comes under Reference data type.
  • Default value of any reference data type is null.
  • A reference variable can be used to reference any object or variable.

 

Share This:

About Hasnain