Primitive Data Type
The programming language pre-defines a primitive data type. It contains no extra sophisticated methods and specifies the size and type of variable values.
These are the most fundamental data types in the Java programming language.
There are eight kinds of primitive data types supported by Java:
- Boolean data type
- byte data type
- char data type
- short data type
- int data type
- long data type
- float data type
- double data type
Boolean data type
A boolean data type comprises a single bit of data that can only contain true or false values. True/false circumstances are tracked using this data type.
byte data type
It's an 8-bit two-s complement signed integer. It stores numbers in the range of -128 to 127. A byte data type helps conserve significant quantities of memory.
char data type
A single character is stored in this data type. Single quotes, such as 'A' or 'a', must be used to enclose the character. Alternatively, ASCII values can be used to show certain characters. It is 2 bytes in size.
short data type
The short data type is a 16-bit signed two's complement integer. It keeps track of values ranging from -32,768 to 32767. Use a short to conserve memory in big arrays, similar to byte.
int data type
It is a 4 Bytes or 32-bit signed two's complement integer. Whole numbers between -2147483648 and 2147483647 can be stored in this data format.
long data type
It is an 8 Bytes, or 64-bit signed two's complement integer. Whole numbers between -263 to 263-1can be stored in this data format.
float data type
The float data type is a 32-bit single-precision floating-point data type. It is capable of storing fractional integers between 3.4e-038 and 3.4e+038. The value should end with an “f.”
double data type
The float data type is a 64-bit double-precision floating-point data type. It is capable of storing fractional integers between 1.7e−308 and 1.7e+308. The value should end with a “d.”
Let us give an example and try to use all of the primitive data types in java.
Code
public class Main
{
public static void main(String[] args) {
//boolean
boolean bt = true;
System.out.println(bt);
//byte
byte n;
n = 127;
System.out.println(n); // prints 127
//char
char a = 'A';
char b = 66;
System.out.println(a); // prints A
System.out.println(b); // Displays B
//short
short m= 1234;
System.out.println(m);
//int
int num1=56782;
System.out.println(num1);
//long
long num2 = 15000000000L;
System.out.println(num2);
//float
float num3 =3.14f;
System.out.println(num3);
//double
double num4 = 69.784d;
System.out.println(num4);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
true
127
A
B
1234
56782
15000000000
3.14
69.784
You can also read about the topic of Java Destructor and Hashcode Method in Java.
Non-Primitive Data Type
These data types are not actually defined by the programming language but are created by the programmer. These are also called Object Data Types or Reference Data types.
Since they contain a memory address of variable value rather than the value itself.
There are four kinds of non-primitive data types supported by Java:
- Arrays
- Strings
- Classes
- Interfaces
Now let us study them in-depth.
Arrays
In Java, arrays are objects that represent homogenous data structures. Arrays are data structures that contain one or more values of a given data type and allow for indexed access to those values. The index of an array element is used to access it.
Strings
A sequence of characters is referred to as a string. On the other hand, a string in Java is an object that represents a sequence of characters. The java .lang package. A string object is created using the String class. Unlike C/C++, Java strings do not have a null character at the end.
Classes
In Java, a class is a user-defined blueprint or prototype containing all your data. To represent the behavior of an object, a class has fields (variables), properties, and methods.
Interfaces
An interface in Java is a user-defined blueprint or prototype which includes all your data just like a class, but the methods declared in the interface are all abstract. That is, it contains only definition and signature and no body.
Must Read Array of Objects in Java and Swap Function in Java
Frequently asked questions
-
What is an object in Java?
Object in java is nothing but an instance of a class. An object has the state and behavior of the class it belongs to but with a unique identity.
-
Do strings in java require a null character at the end of the string like in C/C++?
No , strings in Java do not require any null character at the end of the string.
-
Why is the char 2 byte in size in java?
Other languages, such as C/C++, utilize just ASCII characters, and 8 bits is sufficient to represent all ASCII characters. However, Java uses the Unicode system, not the ASCII coding system, and 8 bits is insufficient to express all Unicode characters; hence Java requires 2 bytes for characters.
Key Takeaway
Today we learned about different data types in Java and how they differ in functioning and properties.
We came upon two significant kinds of data types known as Primitive and Non-Primitive Data Types in Java and learned about each one of them in depth.
To study more about data types, refer to Abstract Data Types in C++.
If you wish to learn more about Java from experts, you can check out the Basics of Java with Data Structures and Algorithms course by coding ninjas.
Happy Coding!