Data Type in Java Programming

Data Type in Java define the kind of data a variable can store, making them essential for efficient memory use and program performance. Every variable in Java has a set datatype, which ensures the variable stores the correct data and restricts operations that are not compatible with that type. Java’s datatypes fall into two main categories: primitive and non-primitive. Primitive datatypes, such as int, float, and char, represent simple values like numbers and characters and have fixed memory sizes, making them very efficient for operations where speed is crucial.

Non-primitive datatypes, including arrays, strings, and classes, are more complex. These datatypes store references to memory locations where data is actually kept, allowing for more flexibility and functionality but requiring more memory management. Choosing the right datatype helps make code cleaner and more optimized; for instance, using an int instead of a long for small numbers saves memory and improves performance. Similarly, using String or custom objects for more complex data structures enables better organization and manipulation of data, which is crucial in larger applications.

Table of Contents

1. Primitive Data Type

Primitive datatypes are the most basic datatypes in Java. They directly store values in memory and have a fixed size, which makes them highly efficient. Java has eight primitive datatypes, each with a unique purpose:

  • byte: Stores an 8-bit integer, suitable for small numbers. Range: -128 to 127.
  • short: Stores a 16-bit integer. Range: -32,768 to 32,767.
  • int: The most commonly used integer type, stores a 32-bit integer. Range: -2,147,483,648 to 2,147,483,647.
  • long: Used for larger integers, stores a 64-bit integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • float: Stores 32-bit decimal values with single precision, often used for large-scale calculations.
  • double: Stores 64-bit decimal values with double precision, suitable for precise calculations.
  • char: Stores a single 16-bit Unicode character, e.g., ‘A’, ‘B’, or symbols.
  • boolean: Stores one of two values, true or false, used in logical conditions.

Example of Primitive Datatypes:

int age = 25;
double salary = 55000.75;
char grade = 'A';
boolean isEligible = true;
Java

In this example:

  • int age stores an integer.
  • double salary stores a decimal value.
  • char grade stores a character.
  • boolean isEligible stores a true/false value.

2. Non-Primitive Data Type

Non-primitive datatypes, also known as reference datatypes, are more complex and store references to objects. Unlike primitive datatypes, they do not have a fixed size and are defined by the programmer. Non-primitive datatypes include classes, arrays, interfaces, and strings.

  • String: A sequence of characters, like “Hello” or “Java”. Strings in Java are objects and can be manipulated using built-in methods.
  • Array: A collection of values of the same datatype. Arrays have a fixed size once initialized.
  • Class: Represents a custom datatype that can have fields (variables) and methods to perform operations.
  • Interface: Defines a contract that a class can implement, specifying a set of methods without implementing them.

Example of Non-Primitive Datatypes:

String name = "Alice";
int[] scores = {85, 90, 78};
Person student = new Person();
Java

In this example:

  • String name is a non-primitive datatype that stores text.
  • int[] scores is an array of integers.
  • Person student is an object of a custom class named Person.

3. Primitive Vs Non-Primitive Datatypes (with Examples)

The table below provides a comparison between primitive and non-primitive datatypes with examples:

FeaturePrimitive DatatypesNon-Primitive Datatypes
DefinitionBasic types storing direct valuesReference types pointing to objects
Memory SizeFixed (e.g., int is 32-bit)Variable (depends on object size)
Examplesint, char, float, booleanString, Array, Class
Stored inStack memoryHeap memory
Default ValueDepends on type (e.g., int is 0)null
Example in Codeint age = 25;String name = "Alice";
Created usingDirect declarationnew keyword or literal syntax
MutableNo (for primitive types like int)Yes (for non-primitive types)

Primitive datatypes are simple and directly store values, whereas non-primitive datatypes point to objects stored in the heap memory.


4. Examples of Code

Here are examples demonstrating the use of both primitive and non-primitive datatypes:

Example of Primitive Datatypes:

public class PrimitiveExample {
    public static void main(String[] args) {
        int number = 100;               // Integer
        float price = 99.99f;           // Floating-point number
        char letter = 'A';              // Character
        boolean isActive = true;        // Boolean value

        System.out.println("Number: " + number);
        System.out.println("Price: " + price);
        System.out.println("Letter: " + letter);
        System.out.println("Active: " + isActive);
    }
}
Java

Output:

Number: 100
Price: 99.99
Letter: A
Active: true
Java

Example of Non-Primitive Datatypes:

public class NonPrimitiveExample 
{
    public static void main(String[] args)
     {
        String message = "Hello, Java!";       // String
        int[] numbers = {10, 20, 30};          // Array
        Person person = new Person("John");    // Object of a class

        System.out.println("Message: " + message);
        System.out.println("Numbers: " + numbers[0] + ", " + numbers[1] + ", " + numbers[2]);
        
        System.out.println("Person Name: " + person.getName());
    }
}

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
Java

Output:

Message: Hello, Java!
Numbers: 10, 20, 30
Person Name: John
Java

In this example:

Person person is an instance of a custom class Person.

String message is a non-primitive datatype that stores a sequence of characters.

int[] numbers is an array that holds multiple integer values.

Youtube Videos:

Datatypes in Java specify the type of data a variable can hold, ensuring proper memory allocation and efficient operations. Java has two main categories: primitive and non-primitive datatypes. Primitive types like int and char store simple values with fixed sizes, while non-primitive types like String and arrays store references to objects, offering greater flexibility for complex data handling.

Share toi your friends