Data Types in Java | Java Tutorial
Data types in java specify the size and type of values that can be stored in an identifier.
There are two types of Data Types in Java.
1. Primitive Data Type
2. Non-primitive Data Type
Primitive Data Type:
There are 8 primitive data types such as byte, short, int, long, float, double, char, and boolean. Size of these 8 primitive data types wont change from one OS to other.
byte, short, int & long – stores whole numbers
float, double – stores fractional numbers
char – stores characters
boolean – stores true or false
Non-primitive Data Type:
Non-primitive data types include Classes, Interfaces and Arrays which we will learn in coming tutorials.
Sample Program:
package classTwoVariables; public class DataTypes { public static void main(String[] args) { byte byteDataType = 127; //Change the value of byte data type to 128 and you will find an error. The range of byte is from -128 to 128 //byte byteDataTypeNew = 128; short shortDataType = 128; //Change the value of byte data type to 32768 and you will find an error. The range of byte is from -32768 to 32767 //short shortDataTypeNew = 32768; int intDataType = 32768; long longDataType = 2147483648L; float floatDataType = 20.99F; double doubleDataType = 49999999.9d; char charDataType = 'M'; boolean booleanDataType = true; System.out.println(byteDataType); System.out.println(shortDataType); System.out.println(intDataType); System.out.println(longDataType); System.out.println(floatDataType); System.out.println(doubleDataType); System.out.println(charDataType); System.out.println(booleanDataType); } }
Output:
127 128 32768 2147483648 20.99 4.99999999E7 M true
Must Read: Java Tutorial