In any programming language, data type tells compiler or interpreter, what type of data variable is going to hold or store i.e whether a variable is going to store number or character or floating point value. Java data types classified as follows.
Primitive data types
Primitive data types allows variable to store single value at a time. Java has eight primitive data types which are given below:
byte, short, int and long are integer primitive data types, means byte, short, int and long type variables are used to store or hold integer values, only range is different for byte, short, int and long data type. All integer data types are signed data types means all variable of integer data type can store positive and negative values.
byte is 8 bit in size. byte variables can store any value in the range of -128 to +127. byte variables are declared with keyword byte.
short is 16 bit in size. short variables can store any value in the range of -32768 to +32767. short variables are declared with keyword short.
int is 32 bit in size. int variables can store any value in the range of –2,147,483,648 to +2,147,483,647. Usually int type variable use in control loops. int variables are declared with keyword int.
long is 64 bit in size. int type is not large enough to hold the desired value. The range of a long is quite large. This makes it useful when variable has to store big numbers like credit card number or Aadhaar card number etc. long variables are declared with keyword long.
Range of integer data types shown in below table:
float and double both are floating data types only there capacity of value storage is different.
float is 32 bit in size. float variables can store any value in the range of 1.4e–045 to 3.4e+038. float variables are declared with keyword float. float has single precision. Variables of type float are useful when you need a fractional component, but don’t require a large degree of precision. For example, float can be useful when representing dollars and cents.
Note: when you are assigning integer value to float variable adding f at the end of value is optional but when you are assigning float value to float variable adding f at the end of value is mandatory. shown in below. example.
double is 64 bit in size. double variables can store any value in the range of 4.9e–324 to 1.8e+308. double variables are declared with keyword double. double has higher precision than float. when you need more accuracy in decimal points then double is best choice over float.
Range of floating point data types shown in below table:
char data type allows variable to store one character at a time. char in java is different from c/c++ programming language. In c/c++ language char takes single byte i.e 8 bits but in java unicodes are used to represent character and unicode takes 2 bytes i.e 16 bits. Range of char in java is 0 to 65535 and there are no negative chars. char variables are declared with keyword char
Note: when you assign integer value to char variable, compiler assigns ASCII character corresponding to integer value. please see character2 and it’s corresponding output in following example.
boolean data type allows variable to store logical values . boolean variables stores either true or false value. boolean variables are declared with keyword boolean
Non primitive data types in java
Non primitive data types allows variable to store address. Non primitive data type is also called as reference type because they refer to memory location where data is stored. class, array, enum etc are non-primitive data types in java.
Class is a non primitive data type. It is use to store the reference address of class object. class data type is declared by using class name.
Array is a non primitive data type. Basically Array is collection of similar type of data. Individual data in array is called as element. Each element in array is accessed by using index. array indexing starts from 0 and index of last element is array size-1. Below array of size 12 is shown. Size of array is fixed when array is created. Default value of array element is zero.