Java Data Types Variables are nothing but reserved memory locations to
store values. This means that when you create a variable you reserve some space
in memory Based on the data type of a variable, the operating system allocates
memory and decides what can be stored in the reserved memory. Therefore, by
assigning different data types to variables, you can store integers, decimals,
or characters in these variables
There are two data types available in Java:
- Primitive Data Types
- Reference/Object Data Types
Primitive Data Types: There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword.
Sample Program :
public class DataTypesde
{
public static void main(String[] args)
{
//
int
int
i=10;
System.out.println(i); //10
System.out.println(Integer.MAX_VALUE); //2147483647
System.out.println(Integer.MIN_VALUE); //-2147483648
System.out.println(Integer.SIZE); //32
//
double
double
d=20.9;
double
d1=20;
System.out.println(d); //20.9
System.out.println(d1); //20.0
System.out.println(Double.MAX_VALUE); //1.7976931348623157E308
System.out.println(Double.MIN_VALUE); //4.9E-324
System.out.println(Double.SIZE); //64
//char
char c='a';
char c2='d';
System.out.println(c); //a
System.out.println(c2); //b
System.out.println(Character.MAX_CODE_POINT); //1114111
System.out.println(Character.MAX_VALUE); //
System.out.println(Character.MAX_VALUE);
System.out.println(Character.SIZE); //16
// string
String s1="anji"; //anji
System.out.println(s1);
//
float
float f=10;
float f2=10;
System.out.println(Float.MAX_VALUE); //3.4028235E38
System.out.println(Float.MIN_VALUE); //1.4E-45
System.out.println(Float.SIZE); //32
System.out.println(f); //10.0
//
byte
byte b1=10;
System.out.println(b1); //10
System.out.println(Byte.SIZE); //8
System.out.println(Byte.MAX_VALUE); //127
System.out.println(Byte.MIN_VALUE); //-128
//short
short sh=25;
System.out.println(sh); //25
System.out.println(Short.MAX_VALUE); //32767
System.out.println(Short.MIN_VALUE); //-32768
System.out.println(Short.SIZE); //16
//
long
long t=123;
System.out.println(t); //123
System.out.println(Long.MAX_VALUE); //9223372036854775807
System.out.println(Long.MIN_VALUE); //-9223372036854775808
System.out.println(Long.SIZE); //64
}
}
Data types and default size:
Data types and default size:
Data Type | Default Value | Default size |
---|---|---|
boolean | false | 1 bit |
char | '\u0000' | 2 byte |
byte | 0 | 1 byte |
short | 0 | 2 byte |
int | 0 | 4 byte |
long | 0L | 8 byte |
float | 0.0f | 4 byte |
double | 0.0d | 8 byte |