What are Data Types in Java – Primitive and Non-Primitive Data Types

Data Types in Java

Data types in Java are specified in the variable declaration and with initialization as well if both are done in a single statement. Data types of a variable indicate the type of value it is going to store as well as the range of possible values it can have after manipulations done on it. Data types in Java are primarily classified into two types:

    • Primitive Data Types
    • Non-Primitive Data Types

What are Primitive Data Types in Java

Primitive data types in java are the basic data types the make up a java program or any other program. Primitive data types in java are of 8 types:

    • boolean
    • char
    • byte
    • short
    • int
    • long
    • float
    • double

Boolean Data Type in Java

Boolean data type in Java is used in variable declaration indicates that the variable can have only 2 values, either true or false. The default value of a variable declared with boolean data type is false. It takes 1 bit of space in the memory. Boolean variable declaration is given below:

//boolean -> data type
//flag -> variable name
boolean flag;

 

Char Data Type in Java

Character data type in Java is a single 16-bit UNICODE character of numeric type occupying 2 bytes of space in memory. Value range of a character variable is ‘\u0000’ (0) – ‘\uffff’ (65,535). Default value of a character variable is ‘\u0000’. They must be enclosed in single quotes like ‘a’ or ‘A’. Character variable declaration is given below:

//char -> data type
//c -> variable name
char c;

Byte Data Type in Java

Byte data type in Java is an integral data type occupying 1 byte of space in memory. It can be used with variables that are confirmed to store values from -128 to 127. The byte data type is an 8-bit signed 2’s complement integer used when small numbers are required in your program. The default value of a byte variable is 0. Byte variable declaration is given below:

//byte -> data type
//b -> variable name
byte b;

 

Short Data Type in Java

Short data type in Java is an integral data type occupying 2 bytes of space in memory. It can be used with variables that are confirmed to store values from -32,768 to 32,767. The short data type is a 16-bit signed 2’s complement integer used when numbers slightly larger than byte type are required in your program. The default value of a short variable is 0. Short variable declaration is given below:

//short -> data type
//s -> variable name
short s;

 

Int Data Type in Java

Int data type in Java is an integral data type occupying 4 bytes of space in memory. It can be used with variables that are confirmed to store values from -2,147,483,648 to 2,147,483,647. The byte data type is a 32-bit signed 2’s complement integer having 0 as default. Integer variable declaration is given below:

//int -> data type
//x -> variable name
int x;

 

Long Data Type in Java

Long data type in Java is an integral data type occupying 8 bytes of space in memory. It can be used with variables that are confirmed to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The long data type is a 64-bit signed 2’s complement integer with 0L as default. Long variable declaration is given below:

//long -> data type
//l -> variable name
long l;

 

Float Data Type in Java

The float data type in Java is a floating-point data type occupying 4 bytes of space in memory. It is a single-precision floating-point number having 0.0f as default. Float variable declaration is given below:

//float -> data type
//var -> variable name
float var;

 

Double Data Type in Java

Double data type in Java is a floating-point data type occupying 8 bytes of space in memory. It is a double-precision floating-point number having 0.0d as default. Double variable declaration is given below:

//double -> data type
//var -> variable name
double var;

 

What are Non-Primitive Data types in Java

Non-primitive data types in Java are known as reference data types because they store the address of a variable, not its value. Non-Primitive Data types include strings, array, class, interface, and many more.

String

Variable of type String contains a string or array of characters and is enclosed within double quotes. Strings in Java are not terminated by the null character. A string is different from an array of characters as  the string is designed to hold a sequence of characters in a single variable whereas, a character array is a collection of separate char type entities. String variable declaration is given below:

//String -> data type

//str1 -> variable name
//String variable created using new operator
String str1 = new String("StudyExperts");

//str2 -> variable name
//String variable created without new operator
String str2 = "StudyExperts";

 

Class

A class in Java is a user-defined entity used to represent real-life objects. It can be defined as a blue-print or prototype from which objects are obtained. A class contains data members (variables) and member functions that work on the said data members. Class when instantiated results in an object that has a copy of the data members and member functions of the class. Declaration of a class contains modifiers, className, data members and member functions in general. Class declaration is given below:

//public -> access modifier
//class -> data types specifying StudyExperts is a class
public class StudyExperts{
//data member declaration

//member functions declaration or definitions
}

 

Interface

An interface is like a class with data members and member functions but the member functions don’t have any definition. An interface can be extended by another interface or implemented by a class which will need to provide definitions of the functions otherwise will be declared abstract allowing it to not have function definitions. It specifies what a class must do and the how part is implemented by the class itself so it can be said that while class is a blue-print of an object, interface is a blue-print of a class. Interface declaration is given below:

//interface -> data type
//In -> variable name
interface In{
//final -> keyword that doesn't allow the variable or method to be overridden by the child class
//x -> variable of type int with value 10
final int x=10;

//abstract method declaration
void display();
}

 

Array

An array in java is a group of similar types of values. They are dynamically allocated. A number of elements of an array object in Java are found using the length member. Elements of an array are ordered and indexing starts from 0. The size of an array must be specified using an int value. Array declaration is given below:

//array of type int with 10 variables
//arr -> variable name
int[] arr = new int[10];

 

Difference between Primitive and Non-Primitive Data Types

Primitive Data Types Non-Primitive Data Types
Primitive data types in Java are pre-defined (already defined in Java). Non-Primitive data types are defined by the programmer except for String data types.
They always have value. They can have nothing for a value denoted by null.
Primitive types when used in the code always start with a lowercase letter. Non-Primitive types when used in the code always start with an uppercase letter.

 

Also, read Variables in Java.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *