Table of contents
1.
Introduction
2.
Primitive Datatypes
2.1.
Boolean
2.1.1.
Example
2.1.2.
Output
2.2.
Byte
2.2.1.
Example
2.2.2.
Output
2.3.
Char
2.3.1.
Example
2.3.2.
Output
2.4.
Short
2.4.1.
Example
2.4.2.
Output
2.5.
Int
2.5.1.
Example
2.5.2.
Output
2.6.
Long
2.6.1.
Example
2.6.2.
Output
2.7.
Float
2.7.1.
Example
2.7.2.
Output
2.8.
Double
2.8.1.
Example
2.8.2.
Output
3.
Non-Primitive Datatypes
3.1.
Strings
3.1.1.
Example
3.1.2.
Output
3.2.
Arrays
3.2.1.
Example
3.2.2.
Output
3.3.
Classes
3.4.
Interface
4.
Primitive vs Non-Primitive
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Difference between Primitive and Non-Primitive Datatypes

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When we talk about programming languages, we can’t skip an intangible part of how data is stored and accessed by these languages. However, sometimes the type of data stored is more important than the data itself. As it defines how we access, modify and use that data. 

This is where datatypes come into picture.

A data type is essentially an attribute provided to a variable which tells how we intend to use the variable. This also defines the types of operations that can be performed on the variable, the properties they possess, and what can be deduced from the type of data stored in the variable.

On the basis of the above characteristics the datatypes in java are divided into two categories:

  1. Primitive Datatypes
  2. Non-Primitive Datatypes

We will learn more about what distinguishes each of them apart further in the blog.

Also see, Duck Number in Java and Hashcode Method in Java.

Primitive Datatypes

Java has some predefined datatypes that have their size, type of values, properties, and other characteristics defined for them by default. These are known as the Primitive datatypes. There are 8 primitive datatypes in Java: 

  • Byte
  • Short
  • Int
  • Float
  • Double
  • Char
  • Boolean

 Primitive datatypes are stored in the stack when a variable is declared. Thus, when we copy a variable it creates another variable completely irrespective of the original variable.

Of the 8 primitive datatypes, they are divided into 4 aspects that define the basic input of each data type, namely: integer, float, character, boolean. 

Boolean

The boolean datatype comprises of a bit that decides whether the variable stores a true or false value. This datatype can be used as flags that can be used in conditional statements.

Example

class booleanData{
	public static void main(String args[]){
    	boolean test = true;
    	System.out. println(test); //Output will be true
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Byte

Byte is a type of integer datatype. This is an 8-bit signed two’s complement integer. Thus, is able to store integers within -128 to 127. When working with values that can not exceed this range, this can be helpful for saving memory.

Example

class byteData{
	public static void main(String args[]){
		byte test1, test2;
		test1 = 15;
		test2 = 150;
		System.out. println(test1); //Prints 15
		System.out. println(test2); //throws error as out of range
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Char

Char stores a single character in the variable. The character must always be enclosed within single quotes. We can also mention ASCII values as the character.

Example

class charData{
	public static void main(String args[]){
		char test1, test2;
		test1 = 'a';
		test2 = 65;
		System.out. println(test1); //Prints a
		System.out. println(test2); //Displays A
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

 

Short

Short is a integer type data that stores values in the range -32,768 to 32767. By default short takes 2 bytes of space.

Example

class shortData{
	public static void main(String args[]){
		short test1, test2;
		test1 = 5116;
		test2 = 150000;
		System.out. println(test1); //Prints 5116
		System.out. println(test2); //throws error as out of range
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Int

The most used integer type datatype that stores values in the range -2147483648 to 2147483647. 

Example

class intData{
	public static void main(String args[]){
		int test;
		test = 151655;
		System.out. println(test); //Prints 151655
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Long

It is an integer type datatype that has a very high range due to the fact that it can store 64-bit two’s complement integers i.e. from -263 to 263-1.

Example

class longData{
	public static void main(String args[]){
		long test;
		test = 1500000001;
		System.out. println(test); //Prints 1500000001
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Float

When it comes to storing decimal values, float is one of the most used datatype. It can be used to store fractional values from 1.2e-038 to 3.4e+038.

Example

class floatData{
	public static void main(String args[]){
		float test;
		test = 35.45;
		System.out. println(test); //Prints 35.45
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Double

When using fractional values that exceed the limit of float datatype, we need to use double data type. The range for this type of datatype is from 1.7e-308 to 1.7e+308.

Example

class doubleData{
	public static void main(String args[]){
		double test;
		test = 554.554d;
		System.out. println(test); //Prints 554.554
	}
}
You can also try this code with Online Java Compiler
Run Code

Output


Also see, Swap Function in Java

Non-Primitive Datatypes

When we are in need of Data Structures that need to be defined by the programmer to make it work like a custom variable that enables us to address problems that could not be addressed by the primitive datatypes. As these are basically reference variables that reference a memory location which stores a data, when we try to copy a particular data, both the copies will point to the same memory location. They are also called Object datatypes.

Strings

In Java, string acts as a sequence of characters stored as an object, and thus can be accessed as a string object. java.lang.String can be used to create a string object.

Example

class arrayData{
	public static void main(String args[]){
		String test = “Hello”;
		System.out. println(test);
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Arrays

Arrays are contiguous data of the same type i.e. homogeneous data structures that can be accessed using the index for a specific data.

Example

class arrayData{
	public static void main(String args[]){
		int[] test = {12, 4, 5, 2, 5};
		System.out. println(test[4]);
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

Practice by yourself on online Java compiler.

Classes

Classes are objects that include all the data. A class can contain variables and methods that describe the behavior of the object being defined.

Interface

An interface is similar to a class with the only difference being that all the methods in an interface are abstract by default. 

Also see, Free Space Management in OS

Primitive vs Non-Primitive

Also see, IEnumerable vs IQueryable

Read More, addition of two numbers in java

FAQs

1. What is primitive casting in Java?

Ans: When we convert one primitive datatype into another, it is known as primitive casting. Casting can be of two types: Implicit and Explicit casting.
 

2. What are wrapper classes in Java?

Ans: In some cases, we are not able to directly use primitive datatypes in Java. Here we need to make a wrapper class so as to enable us to wrap primitive data in Objects.

 

3. What is Java Autoboxing?

Ans: Autoboxing is when primitive datatypes are converted into their corresponding wrapper class while compilation. 

 

4. What is the default value of local variables in Java?

Ans: There is no default value for local variables declared in Java.

 

Key Takeaways

In this blog, we discussed the different datatypes in Java and how to distinguish Primitive and Non-Primitive Datatypes.

To study more about data types, refer to Abstract Data Types in C++ and Data Types in C++.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass