Data Types in Java : Primitive and Non-Primitive Data Types

Data Types in Java : Primitive and Non-Primitive Data Types

22 Jan 2025
Beginner
10.3K Views
29 min read
Learn with an interactive course and practical hands-on labs

Free Java Course With Certificate

Data Types in Java

Data types in Java are a way to classify data to make it easier to understand and work with in programming. They define what kind of value a variable can hold, such as numbers, text, or collections of items. If you’ve worked with data types in Java, you’ll find that other languages follow similar concepts, even if their rules and features vary.

Hence, In this Java tutorial, we'll learn the concepts of Data Types in Javaincluding primitive and non-primitive data types in Java, in detail. We will also explore various Data types in Java with examples.

What are Data Types in Java?

Data types can describe the various sizes and values that can be stored in that particular variable.
  1. Primitive data type: This particular data type includes float, short, boolean, byte, char, long, int, and double.
  2. Non-primitive data type: This particular data type includes arrays, interfaces, strings, and classes.

Data Types in Java

1. Primitive Data Types in Java

Primitive data types in Java are the basic building blocks of data, representing simple values like numbers, characters, and true/false conditions. They are predefined by the language, require minimal memory, and have no additional methods or functionality.

There are 8 types of primitive data, which will be discussed further.

  1. Integer Types
  2. Floating Point Types
  3. Char data type
  4. Boolean data type
Data TypeDescriptionDefault ValueDefault SizeRange
byte8-bit signed integer, used to save memory in large arrays.01 byte-128 to 127
short16-bit signed integer, used for saving memory compared to int.02 bytes-32,768 to 32,767
int32-bit signed integer, the most commonly used integer type.04 bytes-231 to (231) - 1 (-2,147,483,648 to 2,147,483,647)
long64-bit signed integer, used when a larger range than int is needed.0L8 bytes-263 to (263) - 1 (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
float32-bit IEEE 754 floating-point, used for decimal values (less precision than double).0.0f4 bytesApproximately ±3.40282347E+38F
double64-bit IEEE 754 floating-point, used for precise decimal values (default for floating-point).0.0d8 bytesApproximately ±1.79769313486231570E+308
char16-bit Unicode character, used for a single character/letter.'\u0000'2 bytes'\u0000' (0) to '\uffff' (65,535)
booleanStores one of two values: true or false.false~1 bittrue or false

Read More - Advanced Java Interview Questions

1. Integer Types

Integer types store whole numbers, positive or negative, like 233 or -457, without decimals. There are four data types for storing whole numbers. The choice depends on the numeric value.

  1. Byte data type

    The byte data type is an 8-bit signed two’s complement integer. It can store whole numbers from -128 to 127. Its default value is 0. It is used to save memory in large arrays where memory savings are most required. The byte data type saves space because a byte is 4 times smaller than an integer. It can also be used in place of the int data type.

    Syntax

    byte variable_name;
    

    Example of Byte Data Type in Java

    public class Main {
      public static void main(String[] args) {
        byte myNum = 126;
        System.out.println(myNum);  
      }
    }
    

    Output

    126
    
  2. Short data type

    The short data type is a 16-bit signed two’s complement integer. It can store whole numbers from -32768 to 32767. Its default value is 0. A short data type is 2 times smaller than an integer. It can be used when a small range of integer values is required. We will understand through illustration in the Java Compiler.

    Syntax

    short variable_name;
    

    Example of Short Data Type in Java

    public class Main {
      public static void main(String[] args) {
        short myNum = 30000;
        System.out.println(myNum);  
      }
    }
    

    Output

    30000
    
  3. Integer Data Type

    It is a 32-bit signed two’s complement integer. It is a 4-byte integer type with a range of -2^31 to 2^31-1. Its default value is 0. The int data type is generally used as a default data type for integral values.

    Syntax

    short variable_name;
    

    Example of Int Data Type in Java

    public class Main {
      public static void main(String[] args) {
        int myNum = 300000;
        System.out.println(myNum);  
      }
    }
    

    Output

    300000
    
  4. Long Data Type

    The long data type is a 64-bit two’s complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1). It is used in situations when "int" is not large enough to store the value. Here, you should end the value with an "L". Its default value is 0.

    Syntax

    long variable_name;
    

    Example of Long Data Type in Java

    public class Main {
      public static void main(String[] args) {
        long myNum = 15000000000L;
        System.out.println(myNum);  
      }
    }
    

    Output

    15000000000
    

2. Floating Point Types

This data type is used when there is a requirement for decimal numbers like 2.33, 3.2, etc. There are two data types for storing fractional or decimal numbers.

  1. Float data type

    Float data type in Java has a "single precision 32 bits IEEE 754" floating point. The value of this range is unlimited. The size of the float data type in java is 4 bytes (32 bits). It stores fractional numbers. Sufficient for storing 6 to 7 decimal digits. Its default value is 0.0F. You must end the value with an "f" for floats.

    Syntax

    float variable_name;
    

    Example of float Data Type in Java

    public class Main {
      public static void main(String[] args) {
        float myNum = 8.79f;
        System.out.println(myNum);  
      }
    }
    

    Output

    8.79
    
  2. Double data type

    This data type is a "64-bit Double precision IEEE 754 floating point". The size of the double data type is 8 bytes or 64 bits. It has greater precision than `float'. It also stores fractional numbers. Sufficient for storing 15 decimal digits. Its default value is 0.0d. You should end the value with "d" for doubles. You'll understand completely by executing the code below in the Java Playground.

    Syntax

    float variable_name;
    

    Example of double Data Type in Java

    public class Main {
      public static void main(String[] args) {
        double myNum = 78.36d;
        System.out.println(myNum);  
      }
    }
    

    Output

    78.36

Read More - Java Developer Salary

float vs. double

  • The precision of a floating point value indicates how many digits the value can have after the decimal point.
  • The precision of float is six or seven decimal digits, while double variables have a precision of about 15 digits.
  • Therefore, it is recommended to use double for most calculations. However, double takes up twice as much memory as float (8 bytes vs. 4 bytes).

3. Char data type

The char data type is used to store single 16-bit Unicode characters like 'A', '1', or '$'. The character must be surrounded by single quotes. Its value range lies between '\u0000' (or 0) to '\uffff'.

Syntax

char variable_name;

Example of char Data Type in Java

public class Main {
  public static void main(String[] args) {
    char scholarHat = 'S';
    System.out.println(scholarHat);
  }
}

Output

S

Representing single characters using ASCII values

You can also use ASCII values to display certain characters. Here, these values are not surrounded by quotes (' '), as they are numbers

Example

public class Main {
  public static void main(String[] args) {
    char myVar1 = 79, myVar2 = 88, myVar3 = 97;
    System.out.println(myVar1);
    System.out.println(myVar2);
    System.out.println(myVar3);
  }
}

Output

O
X
a

4. Boolean data type

The Boolean data type is used to store only two possible values: true and false. It specifies one bit of information, but its "size" can't be defined precisely. Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. This data type is used for simple flags that track true/false conditions.

Syntax

boolean variable_name;

Example of Boolean Data Type in Java

public class Main {
  public static void main(String[] args) {
    boolean isScholarHat = true;
    boolean isEmpty = false;    
    System.out.println(isScholarHat);
    System.out.println(isEmpty);
  }
}

Output

true
false

Why is the Size of char 2 bytes in Java?

  • In Java, char is 2 bytes (16 bits) because it uses Unicode to support international characters, unlike ASCII, which uses 1 byte.
  • Unicode requires 2 bytes to represent over 65,000 characters in various languages.
  • Java's fixed 2-byte char simplifies processing and ensures compatibility with the Basic Multilingual Plane (BMP). For characters outside BMP, surrogate pairs are used.
  • This design enables efficient handling of global text.

2. Non-Primitive Data Types in Java

Non-primitive data types are called reference types because they refer to objects. The value of a variable in a non-primitive data type cannot be stored directly in memory. This particular data stores the address of the variable's memory. Non-primitive types are created by the programmer and are not defined by Java. There are mainly five non-primitive data types, which will be discussed shortly.

  1. Arrays
  2. Strings
  3. Strings
  4. Object
  5. Interfaces
  6. Enum

1. Arrays

An array is a group of identically typed elements kept in consecutive memory regions. Multiple values of the same type can be stored and accessed using a single variable name.

Syntax

data_type array_name[array_size];

There are two types of arrays in Java, which are:

  1. Single-dimensional array: It is a collection of elements of the same data type that are stored in a contiguous block of memory.
  2. Multi-dimensional array: It is an array that contains one or more arrays as its elements.

For more details on arrays: Arrays in Java

2. Strings

The String data type represents sequences of characters. Strings are represented by various alphabets surrounded by double quotes.

Syntax

<String_Type> <string_variable> = “<sequence_of_string>”;

Demonstration of String Data Type in Java Online Editor

public class Main {
  public static void main(String[] args) {
    String greeting = "Welcome to ScholarHat";
    System.out.println(greeting);
  }
}

Output

Welcome to ScholarHat

Read Java Strings in detail: What is String in Java - Java String Types and Methods (With Examples)

3. Class

A class is a user-defined model or prototype from which objects are made. It stands for the collection of attributes or operations that are shared by all objects of a particular type. To create a class, use the keyword class.

Syntax

access_modifier class
{ 
  data members; 
  methods; 
}

Example of a Class in Java

public class ScholarHat {
  int x = 5;
}

Read More: What is Class in Java?

4. Object

An object represents real-life entities and is the fundamental building block of object-oriented programming. In Java, an object is created from a class. To create an object of a class, specify the class name, followed by the object name, and use the keyword new.

Demonstration of an Object in Java in Java Online Compiler

public class ScholarHat {
  int x = 50;

  public static void main(String[] args) {
    ScholarHat myObj = new ScholarHat();
    System.out.println(myObj.x);
  }
}

Output

50

Read More: What is an Object in Java?

5. Interfaces

Interfaces are collections of "abstract methods." To achieve abstraction, Java follows an interface mechanism. The interface data type contains "constants," "default methods," "static methods," and "nested type."

Example of an Interface in Java

interface ScholarHat {
  public void contentwriters(); // interface method (does not have a body)
  public void seo(); // interface method (does not have a body)
}

Read More: Abstraction in Java

Enum

An enum (short for enumeration) is a specific Java class that represents a collection of constants (unchangeable variables such as final). It's utilized when you know every possible value for a variable.

Example

enum Day {
  MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}   

Why is Enum Non-Primitive?

1. Not Built-In Primitive: Primitive types in Java (e.g., int, char, float) are built-in and hold basic values. Enum is not a built-in type but a custom-defined type that extends the java.lang.Enum class.

2. Behavior Like a Class: Enum in Java is internally represented as a class. You can add methods, fields, and constructors to an enum.

enum Day {
 MONDAY, TUESDAY;
public void display() {System.out.println("This is " + this);
}
}      

3. Memory Allocation: Enums do not store values directly like primitive types but are reference types stored in the heap.

4. Supports Object-Oriented Features: Enums can have methods, fields, and implement interfaces, which are characteristics of objects, not primitives.

Conclusion

In conclusion, Data types in Java are the foundation of how information is stored and processed in the language. With Java's wide range of data types, you can handle everything from simple numbers and characters to complex structures efficiently. By understanding data types in Java, you’ll write cleaner, faster, and more reliable code with ease! Also, consider our Full-Stack Java Developer Certification to enhance your Java skills. Enjoy Coding..!

Dear students, the wait is over now. we come up with the Free Technology Courses. now, you don't need to worry about studying them and making your future brighter.

Top Most Asked Topics
Prime Numbers in Java: Simple Logic and Code
Top 12 Features of Java
Types of variables in Java with examples: Local, Instance & Static
What are Operators in Java - Types of Operators in Java

Practice Yourself with the Following MCQs

Q 1: Which of the following is a primitive data type in Java?

  • (a) String
  • (b) int
  • (c) Object
  • (d) ArrayList
Answer: (b) int

Explanation: In Java, int is a primitive data type, while String, Object, and ArrayList are reference types.

Q 2: What is the size of the float data type in Java?

  • (a) 16 bits
  • (b) 32 bits
  • (c) 64 bits
  • (d) 128 bits
Answer: (b) 32 bits

Explanation: The float data type in Java is a 32-bit IEEE 754 floating-point type used to store decimal numbers.

Q 3: Which of the following data types can store a single 16-bit Unicode character?

  • (a) char
  • (b) byte
  • (c) short
  • (d) String
Answer: (a) char

Explanation: The char data type in Java can store a single 16-bit Unicode character, making it suitable for storing individual characters.

Q 4: Which data type is used to store a value of true or false in Java?

  • (a) int
  • (b) boolean
  • (c) char
  • (d) double
Answer: (b) boolean

Explanation: The boolean data type in Java can hold only two possible values: true or false, and is used for logical operations.

Q 5: What is the default value of a double variable in Java?

  • (a) 0.0
  • (b) 0
  • (c) null
  • (d) NaN
Answer: (a) 0.0

Explanation: The default value of a double variable in Java is 0.0 because it is a floating-point type.

FAQs

In Java, a data type specifies the kind of data that a variable can store, such as characters, integers, or floating-point numbers.

Non-primitive data types are objects like arrays and classes, whereas primitive data types are simple data types like int and true.

Because String is a class in Java and offers more than just data storage, it is not a primitive.

Java's boolean data type is a simple way to store true or false values.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this