CodeGym /Java Blog /Java Types /Data types in Java
Author
Milan Vucic
Programming Tutor at Codementor.io

Data types in Java

Published in the Java Types group

What are Data Types in Java?

According to the name, the type of data can be stored in the variable. Mainly there are two types of languages.
  1. Statically typed language
  2. Dynamically typed language
Java is the statically typed language, meaning that we have to declare the type of a variable before storing the respective data in it, because it will not store the other data type as we can do in dynamically typed languages like Python, Javascript.

Data Types in Java

There are two data types in Java.
  1. Primitive Data Types
  2. Non-Primitive Data Types
Data types in Java - 1

Primitive Data Types

The predefined data types in Java are known as primitive data types. They are 8 data types which are described below.

Integer

The integer data type is by default 32 bits signed two’s complement integer.

Size

32 bits

Default

0

Range Of Value

-2,147,483,648 to 2,147,483,647

Example


class Main {
  public static void main(String[] args) {
    // declaring int value
    int intNumber = -125000;
    System.out.println(intNumber);	
  }
}

Output

-125000

Float

The float data type is a single-precision 32-bit floating point. If you are dealing with large arrays and want to save memory then you can use the float instead of double. We should never use this data type for precise values such as currency.

Size

32 bits

Default

0.0

Range Of Value

up to 7 decimal digits

Example


class Main {
  public static void main(String[] args) {
    // declaring float value
    float floatNumber = -32.8f;
    System.out.println(floatNumber);	
  }
}

Output

-32.8

Double

The double data type is a double-precision 64-bit floating point. The default choice for decimal values is this data type. We should never use this data type for precise values such as currency.

Size

64 bits

Default

0.0

Range Of Value

up to to 16 decimal digits

Example


class Main {
  public static void main(String[] args) {
    // declaring double value
    double doubleNumber = -24.3;
    System.out.println(doubleNumber); 
  }
}

Output

-24.3

Long

The long data type is by default 64 bits two’s complement integer. If you are dealing with values wider than those provided by the int then use this data type.

Size

64 bits

Default

0

Range Of Value

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Example


class Main {
  public static void main(String[] args) {
    // declaring long value
    long longNumber = -423322000000L;
    System.out.println(longNumber); 	
  }
}

Output

-423322000000

Byte

The byte data type is 8 bits signed two’s complement integer. When memory saving is a priority then you can use this data type in large arrays.

Size

8 bits

Default

0

Range Of Value

-128 to 127

Example


class Main {
  public static void main(String[] args) {
    // declaring byte value
    byte range = 100;
    System.out.println(range);    	
  }
}

Output

100

Boolean

The boolean data type has two possible values true and false representing 1 bit of information but it’s size is not precisely defined.

Size

1 bit

Default

false

Range Of Value

false, true

Example


class Main {
  public static void main(String[] args) {
    // declaring boolean value
    boolean flag = true;
    System.out.println(flag);
  }
}

Output

true

Char

The char data type is a single 16 bits Unicode character.

Size

16 bits

Default

\u0000 or 0

Range Of Value

\u0000 to \uffff

Example


class Main {
  public static void main(String[] args) {
    // declaring char value
    char letter = '\u0050';
    System.out.println(letter);	
  }
}

Output

P

Short

The short data type is 16 bits signed two’s complement integer.

Size

16 bits

Default

0

Range Of Value

-32,768 to 32,767

Example


class Main {
  public static void main(String[] args) {
    // declaring short value
    short temperature = -22;
    System.out.println(temperature); 
  }
}

Output

-22
Data types in Java - 2

Non-Primitive Data Types

Those data types that are not predefined in the Java and are created by the programmers such as Strings, Arrays, Classes are called non-primitive data types. They are also known as reference types because they refer to objects.

Differences Between Primitive and Non-Primitive Data Types

The main differences between primitive and non-primitive data types are listed below.
  1. Primitive data types are predefined while non-primitive is created by the programmers in Java.
  2. Many operations can be performed by calling different methods through these non-primitive data types but it’s not possible with primitive data types.
  3. Non-primitive data types can be null in value but it’s not the case with primitive data types.
  4. Primitive data types start with a lowercase letter but non-primitive starts with an uppercase letter.

Conclusion

We hope by now you understand what is the Java data types and how to use them with examples. Feel free to practice and get back whenever you need more assistance. Happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION