Data types of variables
We have also used the term “int” in our declaration. In Java “int” is used for data type “in-
teger”. Any variable of “int” data type can hold values from -2 32
- 1 to 2 32
- 1. Let us see
other data types in java:
byte : It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data
type can be useful for saving memory in large arrays, where the memory savings actually
short : It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As
with byte, the same guidelines apply: you can use a short to save memory in large arrays, in
situations where the memory savings actually matters.
long : The signed long has a minimum value of -2 63 and a maximum value of 2 63 -1. Use this
data type when you need a range of values wider than those provided by int.
float : This data type is used for variables which to be defined or assigned values with decim-
al places. Since it stores decimal places to give precise values for variables, it hogs large
memory space. If you do not need precision then don't use it.
double : this data type is also used for variables which to be defined or assigned values with
decimal places. Its range is very large compared to float data type.
boolean : The boolean data type has only two possible values: true and false. Use this data
type for simple flags that track true/false conditions.
char : It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535
String : String is not a native data type. It is defined in a library in the Java language. You
can use to store variable values which can be large strings.