Data types are means to identify the type of data and associated operations of handling it. Java, like any other language, provides ways and facilities to handle different types of data by providing data types.
Java data types are of two types:
- Primitive (or intrinsic) data types
- Reference data types
Primitive data types come as a part of the language. Java provides eight primitive data types, which are: byte, short, int, long, float, double, char, and boolean.
Reference data types are constructed from primitive data types. These are: classes, arrays and interface. But their storage mechanism is different from primitive data types.
Primitive Datatypes
Primitive data types are the “basic” data values. The word ‘Primitive’ means a fundamental component that may be used to create other larger parts. Thus, by primitive data types, we mean fundamental data types offered by Java.
Java supports the following four categories of primitive data types:
- Numeric Integral primitive types:
byte: It’s an integer data type with 1 byte (8 bits). The range of values is -128 to 127. Zero is the default value. For instance, byte b=10;
short: It’s a data type with 2 bytes. The range of values is -32768 to 32767. Zero is the default value. for instance, short s=11;
int: The integer data type is 4 bytes (32 bits). The range of values is -2147483648 to 2147483647. Zero is the default value. for instance, int i=10;
long: It’s an integer data type with 8 bytes (64 bits). Between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. Zero is the default value. e.g., long l=100012;
- Fractional primitive types:
float: The float data type is 4 bytes (32 bits). 0.0f is the default value. float ff=10.3f, for example
double:The double data type is an 8-byte (64-bit) float data type. 0.0d is the default value. for instance, double db=11.123;
- Character primitive types: char: An unsigned Unicode character with a size of 2 bytes (16 bits). From 0 to 65,535 example: char c=’a’;
- Boolean primitive types: The boolean type is a special type for representing true/false values, and this group represents it. They are the language’s defined constants. example: boolean b=true;
Reference Data types
Broadly, a reference in Java is a data element whose value is an address. Arrays, classes, and interfaces are reference data types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to (an address of) the value or set of values represented by the variable.
Difference between primitive and reference data types:
Properties | Primitive data types | Reference Data Types |
---|---|---|
Origin | Pre-defined data types | User-defined data types |
Stored structure | Stored in a stack | The original object is stored in heap, while the reference variable is stored in stack. |
When copied | Two different variables is created along with different assignment(only values are same) | Two reference variable is created but both are pointing to the same object on the heap |
When changes are made in the copied variable | Change does not reflect in the original ones. | Changes reflected in the original ones. |
Default value | Primitive datatypes do not have null as default value | The default value for the reference variable is null |
Example | byte, short, int, long, float, double, char, boolean | array, string class, interface etc. |
Note: also read about the Escape Sequence
Follow Me
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment