Java for students logo
Explore the English language on a new scale using AI-powered English language navigator.

Types in Java

Before one can use a varialbe in Java, it must be declared. Every variable has a type, which indicates kind of data variable stores and possible operations on that data. As it was mentioned in previous tutorials, Java is an object-oriented programming language. However, developers have decided to include support of primitive data types in Java.

Primitive types

Variables of primitive types can store integer and fractional numbers, single characters and true/false logical values. You may think of those types as of the atomic ones, because they are bricks to constuct more complicated ones. Primitive types are predefined by Java and their names are reserved keywords. There are eight primitive types in Java programming language:

Type
Size
Range
Default value
Wrapper type
byte 8-bit -128..127 0 Byte
short 16-bit -32 768..32 767 0 Short
int 32-bit -2 147 483 648..2 147 483 647 0 Integer
long 64-bit -9 223 372 036 854 775 808..9 223 372 036 854 775 807 0L Long
float 32-bit 32-bit IEEE 754 floating-point numbers 0.0f Float
double 64-bit 64-bit IEEE 754 floating-point numbers 0.0d Double
boolean

not defined precisely

true/false false Boolean
char 16-bit (Unicode character) '\u0000'..'\uffff' '\u0000' Character

Note, that all primitive types in Java are signed. Every primitive type has a corresponding nonprimitive wrapper.

Reference types

Other types in Java (reference types), including String one, are related to specific classes, interfaces or arrays. By default, all variables of reference types are set to null.

Example

It's useful to play with variables and types a little. We'd like to show an example below, demonstrating a simple code working with primitive types.

Types.java

public class Types {

      public static void main(String[] args) {

            // byte

            int byteVar = 100;

            System.out.println("byte variable's value: " + byteVar);

            // short

            int shortVar = 10000;

            System.out.println("short variable's value: " + shortVar);

            // int

            int intVar = 1000000000;

            System.out.println("int variable's value: " + intVar);

            // long

            long longVar = 1000000000000L;

            System.out.println("long variable's value: " + longVar);

            // float

            float floatVar = 1.1f;

            System.out.println("float variable's value: " + floatVar);

            // double

            double doubleVar = 1.12345d;

            System.out.println("double variable's value: " + doubleVar);

            // boolean

            boolean booleanVar = true;

            System.out.println("boolean variable's value: " + booleanVar);

            // char

            char charVar = 'Z';

            System.out.println("char variable's value: " + charVar);

            // bonus: String

            String stringVar = "Hi! I am a string.";

            System.out.println("String variable's value: " + stringVar);

      }

}

Output

byte variable's value: 100
short variable's value: 10000
int variable's value: 1000000000
long variable's value: 1000000000000
float variable's value: 1.1
double variable's value: 1.12345
boolean variable's value: true
char variable's value: Z
String variable's value: Hi! I am a string.

 

Partners Ads