There are two data types available in Java:
- Primitive Data Types
- Reference/Object Data Types
a) Primitive Data Types :
There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a key word Let us now look into detail about the eight primitive data types.
Primitive Data Types:
|Data Type||Memory Size||Default Value||Declaration||Range|
|byte||8 bits||0||byte a = 9;||-128 to 127|
|short||16 bits||0||short b = 89;||-32768 to 32767|
|int||32 bits||0||int c = 24;||-2147483648 to|
|long||64 bits||0||long d = 985465;||-9223372036854775808 to |
|float||32 bits||0.0f||float e = 87.36f;||+/- 1.4023×10-45 to |
|double||64 bits||0.0||double f = 85.454;||+/-4.9406×10-324 to |
|char||16 bits||‘ u0000 ‘||char g = ‘c’ ;||0 to 65535|
|boolean||JVM Dependent||false||boolean h = true ;|
b) Reference Data Types:
- Reference variables are created using defined constructors of the classes. They are used to access objects. These variable are declared to be of a specific type that cannot be changed For example, Employee, Puppy etc.
- Class objects, and various type of array variables come unde reference data type.
- Default value of any reference variable is null.
- A reference variable can be used to refer to any object of the declared type or any compatible type.
- Example: Animal animal = new Animal(“giraffe”);
A literal is a source code representation of a fixed value. They are represented directly in the code without any computation.
Literals can be assigned to any primitive type variable.
byte a = 68;
char a = ‘A’
Escape Sequences :
Definition : A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences: Escape Sequences .