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 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 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:
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;
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;
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.
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
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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…