Every variable in Java has a data type.Data types specify the size and type of values that can be stored in an identifier.
In Java Data types are classified into two categories.
- Primitive Data type(built-in data type).
- Non-Primitive Data type(reference data type).
Primitive Data type.
Java has 8 primitive data types
Integer types
- byte - 1byte(8-bits) value range from -128 to 127
- short - 2byte(16-bits) value -32768 to 32768
- int - 4bytes(32-bits) value from -2147483648 to 2147483647
- long - 8bytes(64-bits) value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
public class App
{
public static void main(String[] args) {
// byte type
byte b = 20;
System.out.println("byte= "+b);
// short type
short s =20;
System.out.println("short= "+s);
//int type
int i =20;
System.out.println("int= "+i);
// long type
long l = 20;
System.out.println("long= "+l);
}
}
Floting types
- float - 4byte(32-bits) float data type. ex: 0.3f
- double - 8byte (64-bits) float data type. ex: 11.123
public class App
{
public static void main(String[] args) {
// float type
float f = 20.25f;
System.out.println("float= "+f);
// double type
double d = 20.25;
System.out.println("double= "+d);
}
}
Character Type
- char - 2bytes(16bits) range from 0 to 65,535.
- boolean - true or false.
public class App
{
public static void main(String[] args) {
char ch = 'S';
System.out.println(ch);
char ch2 = '&';
System.out.println(ch2);
char ch3 = '$';
System.out.println(ch3);
}
}
Top comments (0)