In any programming language, we need variables to store values. To create variables, we need to reserve some space in memory. Before you start using variables, first declare the variable with a specific name and its type. In Java, the variable type should be known during the compile time, because Java is a strongly typed programming language. 

Based on the variable’s data type, the operating system assigns memory and determines what can be kept in the reserved memory. Therefore, by assigning different data types in Java to variables, you can store integers, decimals, characters, etc.

Primitive Data Types

The predefined data types in Java by the language and named by the keywords are called Primitive Data Types in Java. Totally, there are eight primitive data types in Java. Let us investigate the details of the below one by one.

  • byte
  • short
  • int 
  • long
  • float
  • double
  • boolean
  • char

What Is Byte?

Byte data type is an 8-bit signed two's complement integer. This sort of data type is applied for saving memory in large arrays, primarily in the place of integers. Because a byte is four times smaller than an int.

Min Value:  -128 (-2^7)

Max Value: 127 (inclusive)(2^7 -1)

Default Value: 0

Size: 1 byte (8 bits)

Example: byte bValue = 100, byte b Const= -50

What Is Short?

Short data type is a 16-bit signed two's complement integer. This kind of data type is similar to byte and used for saving memory in large arrays. A short data type in Java is 2 times smaller than an int.

Min Value:  -32,768 (-2^15)

Max Value: 32,767(inclusive) (2^15 -1)

Default Value: 0

Size:  2 bytes (16 bits)

Example: short sValue = 10000. short rVal = -20000

What Is Int?

Int data type is a 32-bit signed two's complement integer. The default data type in Java is int. To store integral values, unless there is a concern about memory, int is generally used. Later in Java SE 8, the int data type in Java represents an unsigned 32-bit integer, which has the range of values [0,232 -1].

Min Value:  - 2,147,483,648. (-2^31)

Max Value:  2,147,483,647(inclusive). (2^31 -1)

Default Value: 0

Size: 4 byte (32 bits)

Example: int iNum = 100. int iValue = -20000

What Is Long?

Long data type in Java is a 64-bit signed two's complement integer. This kind of data type is used when a wider range than int is needed. Later, in Java SE 8, the long data type in Java represents an unsigned 64-bit long, which has the range of values [0,264 -1]. To support arithmetic operations for unsigned long few methods like divide Unsigned, comparing Unsigned are available in long class.

Min Value:  -9,223,372,036,854,775,808.(-2^63)

Max Value:  9,223,372,036,854,775,807 (inclusive). (2^63 -1)

Default Value: 0L

Size:  8 byte (64 bits)

Example: long lAvalue = 100000L, long lBvalue = -200000L

What Is Float?

Float data type is a single-precision 32-bit IEEE 754 floating point. Float data type in Java is mainly used to save memory in large arrays of floating-point numbers. This data type is certainly not applied for precise values like currency. You can use float when you need scientific calculations.

Default Value: 0.0 f

Size: 4 byte (32 bits)

Example: float fValue1=234.5f, float fValue2 = 2678.2f

What Is Double?

Double data type is a double - precision 64 -bit IEEE 754 floating point. This data type is used as default data type for decimal values, the default choice, generally. Double data type is never used for precise values such as currency. Both float and double data types are specially used for scientific calculations where approximation errors are accepted.

Default value: 0.0d.

Size: 8 byte (64 bits)

Example: double dValue = 123.4

What Is Boolean?

Boolean data type in Java represents one bit of information. There are only two possible values: true or false. Boolean data type’s size is dependent on the virtual machine. This is used for easy flags that track true/false conditions. 

Default value: false

Size: virtual machine-dependent

Example: boolean bStatus = true

What Is Char?

Char data type is a single 16-bit Unicode character and is used to store any character. The character must be surrounded by single quotes. AVA uses Unicode system to represent all characters, so Java uses 2 bytes for character. In other programming languages like C and C++, ASCII code system is used.

Min Value:  '\u0000' (or 0)

Max Value:  '\uffff' (or 65,535 inclusive)

Default Value: '\u0000'

Size : 2 byte (16 bits)

Example: char cLetter='A', char cLetter2 ='\u0061'

Here's How to Land a Top Software Developer Job

Full Stack Development-MEANExplore Program
Here's How to Land a Top Software Developer Job

Best Practices For Data Types in Java

  • In many numeric data types in Java, if you must store whole numbers, int should be used as this is the most used data type.
  • The precision of a float data type is only six or seven decimal digits, but double has precision of about 15 digits. So, using double is very safe for decimal scientific calculations.
  • Boolean data type is mostly used for condition testing.
  • Use proper naming convention for declaring variables. Starting small letter indicates the data type of variable. Consider cLetter. Here, c indicates the char data type convenient to pass character values.

Sample Java Program Showing Data Types in Java.




Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Reference Data Types in Java

Reference variables are produced applying the classes’ defined constructors. They are used to access objects. These variables are announced to be of a particular type that cannot be altered. These data types in Java contain a memory address of variable value and won't store value directly in memory.

Strings are used to hold a sequence of characters in a single variable and defined as an array of characters. Java strings are not terminated with a null character.

Example: String sWord = new String ("Born 2 Win");

Class objects, various type of array variables come under reference data types in Java. Default value of any reference variable is null. A reference variable can be utilized to describe to any object of the compatible type or the declared type.

Employee, Puppy, Animal are few class names.

Example: Animal animal = new Animal("giraffe");

Like a class, an Interface also comes under reference data type.

An Array is a group of like-typed variables, which dynamically allocates memory and comes under the reference data type in Java.

Non-Primitive or Reference data types in Java can be used to call methods to perform certain operations, while primitive data types will not have any methods.

In order to master web application development for virtually any computing platform and develop a firm foundation in Java, enroll in our Full Stack Java Developer Master's Program.

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.