Data types in Java

Java is a statically-typed language, which means that all variables must first be declared before they can be used. It means the variable’s name and types must be defined before it can be used in code.

        boolean bool = true;

By writing the above line we are telling the program that a variable named bool of type boolean is created, which has the default value as true.

Java has 8 types of primitive data types.

Different Primitive Data Types

Java supports the below mentioned primitive data types.

Primitive Type Default Value Wrapper Class
boolean false Boolean
char u0000 Char

Integral Type

byte (byte)0 Byte
short (short) 0 Short
int 0 Integer
long 0L Long

Float Type

float 0.0f Float
double 0.0d Double

boolean

The boolean data type is declared with the boolean keyword. It has only two possible values: true and false. This data type can be used for the simple flagging purpose to track true/false conditions.

char

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff'(or 65,535 inclusive).

byte

The byte data type is declared with the byte keyword.  It can have a minimum value of -128 and a maximum value of 127 (inclusive). The default value is 0. A byte data type can be used in place of int if a variable is supposed to contain the small value in the given range. It will serve two purposes

  • Save space as a byte is 8 bit signed integer which is 4 times smaller than int
  • Their limits help to clarify your code, the fact that a variable’s range is limited can serve as a form of documentation.

short

The short data type can be declared with a short keyword. The short data type is a 16-bit signed two’s complement integer. Its value lies between -32,768 to 32,767 (inclusive). Its default value is 0. short data type can be used in place of int when the value of the variable lies between the range. It will serve two purposes as mentioned above.

int

By default, the int datatype is a 32-bit signed(+/-) two’s complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned(+) 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.

Please note that even in Java 8, int is signed. But there are some methods that treat them as if they were unsigned but there is no way to declare an Unsigned int in Java 8 or beyond.

Try to print the below code to get the minimum and maximum value supported by int. These values can be assigned to a variable.

  • Integer.MAX_VALUE
  • Integer.MIN_VALUE

long

The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In a simple way, you can say that it is an enhanced version of int. This data type should be used when you need a range of values wider than those provided by int.

In Java 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.  

Please note that even in Java 8, long is signed. But there are some methods that treat them as if they were unsigned. compareUnsigned,  divideUnsigned are some methods from the Long wrapper class that support arithmetic operations for unsigned long. More details about Unsigned Int or Long can be found here.

float

The float data type is single-precision(32 bit) IEEE 754 floating-point. Variables of type float are useful when you need a fractional component, but don’t require a large degree of precision. In Single precision, 23 bit is used for the fractional part. This data type can be used for normal day to day calculations.

double

double data type is a double-precision (64-bit) IEEE 754 floating-point. It is floating-point with more precision. In Double-precision, 52 bit is used for the fractional part. As double-precision requires more memory than single-precision hence not advised to use it for normal calculations. It is mainly used for scientific calculations.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.