Java is a statically typed programming language, this means that variables must be declared before its use. So, in Java when a variable is created we have to specify its data type as well. This will define what type of values it can take and what type of operations it can perform.

For example, in this statement int value =123; we are defining variable value as an integer. There are mainly two types of data types in Java.

  1. Primitive data types
  2. Object data types

In Java, Primitive data types are predefined and named using reserved keywords by the language. There are several advantages with primitive data types over object data types.

Below are the pritive data types available in Java programming language.

short

The short data type is a 15-bit signed integer. It has a range from -32,768 to 32,767. The default value for short data type is 0.
Example: short a = 1;

  • We can use short data type to save memory in large arrays, in situations where the memory savings really matters and we know the numbers are within short data type range.

int

The int data type is a 32-bit signed integer. It has a range from -231 to 231-1. The default value for int data type is 0.
Example: int a = 1;

  • Starting from Java 8, we can use int to represent an unsigned 32-bit integer as well, which has range from 0 to 232-1, use Integer class object data type to use int data type as an unsigned integer.
  • int data type takes 4 bytes of memory.
long

The long data type is a 64-bit signed integer. It has a range from -263 to 263-1. Use this data type when you need a range of values outside of int range. The default value for long is 0.
Example: long a = 123; or you can specify it as long a = 123L; (suffixed with L, this tells compiler that we have a long number literal.);

  • Starting from Java 8, we can use long to represent an unsigned 64-bit long, which has range from 0 to 264-1, use Long class object data type to use long data type as an unsigned long.
  • long data type takes 8 bytes of memory.
float

The float data type is a single-precision 32-bit floating point. It is implemented with IEEE 754 standard for floating point arithmetic. The default value for float is 0.0f

Example: float a = 0.1f;
  • If you need to save memory in large arrays of floating point numbers, float data type can be used.
  • Never use float data type for precise values such as currency, use either double or java.math.BigDecimal for that purpose.
  • float data type takes 4 bytes of memory.
double

The double data type is a double-precision 64-bit floating point. It is implemented with IEEE 754 standard for floating point arithmetic. The default value for double is 0.0d

Example: double a = 0.1; or you can specify with d or D suffixed double a = 0.1d;
  • This data type is the default choice for any decimal values.
  • It has accuracy up to 15 decimals, consider using java.math.BigDecimal if you need more accuracy.
  • double data type takes 8 bytes of memory.
char

The char data type is a single 16-bit Unicode character.

Example: char a = 'a'; (need to wrap it in single quotes)
  • It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive)
  • char takes 2 bytes of memory.
boolean

The boolean data type has only two possible values: true and false. The default value is false.

Example: boolean val = true;
  • Use this data type for simple flags that tracks true or false conditions.
  • boolean takes 1 bit of memory.
byte

The byte data type is an 8-bit signed integer, which has range from -128 to 127.

Example: byte val = 1;
  • The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
  • They can also be used in place of int where their limits are in the range of byte, this can help to clarify the code.
  • byte takes 1 byte of memory.

Object data types are the ones created by user, they refer to a particular object. These are also called reference data types. These includes Strings, arrays or any Classes created by user.
For example Fruit apple = new Fruit(); here apple is of data type Fruit.

In Java, an array of primitive data types is treated as an Object. So, it will have same behavior as object data type.

Example: int[] numbers = {1,2,3}; here numbers will be treated as an Object.