Last Updated:

Data Types in Java - Primitives and Binary Literals

 
 
Data Types in Java

Primitive Data Types in Java

 

The Java programming language contains eight primitive data types. The four data types for integer values are byte, short, int, and long. The two primitive floating-point data types are float and double. One character is char, one for the condition is boolean.

The following table shows all of these primitive data types with size, range, default value, and various ways to assign them.

TypeSizeRangeDefault valueExample
boolean1 bitNAfalseboolean bool = true;
char16 bitsUnicode characters'\u0000′ or 0char c = ‘A’;
char c = ‘\u0041′;
char c = 65;
char c = ‘\t’;
byte8 bits[-128,127] or
[-2^7 to 2^7-1]
0byte b = 10;
byte b = 0b010;
short16 bits[-32768,32767]0short s = 32;
short s = ‘A’;
int32 bits[-2147483648,2147483647]0int i = 10;
int i = ‘A’;
long64 bits[-2^63,2^63-1]0long l = 3200L;
long l = 3200;
float32 bits[-3.4E38, 3.4E38]0.0ffloat f = (float) 12.34;
float f = 12.34f;
double64 bits[-1.7E308, 1.7E308]0.0double d = 12.34;

Example of a program with primitive types in Java

Below is a simple Java program that shows the different ways to declare primitive data types.

Look closely at char and what happens when an int is converted to a byte via explicit casting.

DataTypes.java

Use underlining in numeric literals

Starting with Java 7, we can use underscores in numeric literals such as  long ccNum = 1234_5678_9101_1121L;

Binary literals

Starting with Java 7, built-in types (byte, short, int, and long) can also be expressed using a binary calculus system. We need to add a prefix: 0b or 0B.

A couple of examples:

This is all you need to know about primitive data types in Java.