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|
The boolean data type is declared with the boolean keyword. It has only two possible values:
false. This data type can be used for the simple flagging purpose to track true/false conditions.
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).
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.
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.
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.
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
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.
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.
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.
<< Java 8 Functional Interfaces java 11 new features >>
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.