Java datatypes

Java datatypes represent types of variables.

int a = 10

a is a variable of type int

Java datatypes represent the memory size of the variables.

Ex: int is having a memory size of 4 bytes.

Java datatypes represent the range value of the variables.

Ex : Byte can store from [ -128 to 127 ] .

Java data types list

There are 8 primitive datatypes in java: byte, short, int, long, float, double, char, boolean.

byte, short, int, long are used to represent numeric values

float, double is used to represent decimal or floating-point values

char is used to represent a single character

boolean is used to represent true and false

java data types list

Size of data types in java

byte – 1 byte ( 8 bits )

short – 2 bytes ( 16 bits )

int – 4 bytes ( 32 bits )

long – 4 bytes ( 32 bits )

float – 4 bytes ( 32 bits )

double – 8 bytes ( 64 bits )

char – 2 bytes ( 16 bits )

boolean – No Size [ Contains only 2 constants true or false and their memory is decided by JVM ]

Default values of java datatypes

At the time of declaring a variable if we are not assigning any value then JVM provides some default values to those variables which are known as default values.

Note* JVM provides the default values.

byte – 0

short – 0

int – 0

long – 0

float – 0.0

double – 0.0

char – ‘ ‘ [ Single space ]

boolean – false

Java data types range

byte – [ -128 to 127 ]

short – [ -32,768 to 32,767 ]

int – [ -2,147,483,648 to 2,147,483,647 ]

long – [ -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ]

float – [ 3.4e−038 to 3.4e+038 ] 4 bytes 7 decimal digits

double – [ 1.7e−308 to 1.7e+308 ] 8 bytes 16 decimal digits

char – [ ‘\u0000’ to ‘\uffff’ ] OR [ 0 to 65,535 ] 16 bit unicode character

Ex : \u0041 – A

Check this link to understand Unicode characters: https://www.alansofficespace.com/unicode/unicd99.htm

Calculating Java data types range

1 ) byte : 8 bits

Formula : (-2^n) to (2^n – 1)

Calculation : n = 8-1 = 7

= -2^7 to 2^7 – 1

= -128 to 127

2 ) short : 16 bits

Formula : (-2^n) to (2^n – 1)

Calculation : n = 16-1 = 15

= -2^15 to 2^15 – 1

= -32,768 to 32,767

3 ) float : 32 bits

= 3.4e−038 to 3.4e+038

= -3.4 * 10^38 to 3.4 * 10^38 [ 7 decimal digits ]

To understand the calculation of floating-point range we need to understand the floating-point arithmetic in a better way. We have to understand the mathematics behind floating points. From a Java developer perspective, we don’t have to go into that depth still if you want to understand it please check the below links.

https://en.wikipedia.org/wiki/Floating-point_arithmetic#Floating-point_numbers

https://stackoverflow.com/questions/57456646/how-is-float-range-that-large-with-4-bytes-%C2%B13-40282347e38

https://stackoverflow.com/questions/8746443/float-and-int-both-4-bytes-how-come/8746839

FAQ

Why do float values have a letter f at the end? Ex : float result = 101.01f ;

Point values are by default double values. Now if we provide a decimal or floating-point value to a variable then it is considered as a double. But if we have to consider that value as a float then we have to add or suffix the letter f to the value.

In this article, we have covered java datatypes, java data types list, java data types range and the size of data types in java. I hope you found this article interesting and valuable. Please share this article with your friends and help me grow. If you are having any concerns or questions about this article please comment below. If you want to get in touch with me please visit the Contact Me page and send me an email.

1 thought on “Java datatypes”

Leave a Comment