Friday, January 24, 2025
HomeProgrammingPrimitive data type vs. Object data type in Java with Examples

Primitive data type vs. Object data type in Java with Examples

In Java, data types are divided into two broad categories: Primitive Data Types and Object Data Types. Here’s a breakdown of both with examples:

1. Primitive Data Types:

Primitive data types are the basic types that store simple values directly in memory. They are not objects, and they are predefined by the Java language.

There are 8 primitive data types in Java:

  • byte: 1 byte (8 bits), ranges from -128 to 127
  • short: 2 bytes (16 bits), ranges from -32,768 to 32,767
  • int: 4 bytes (32 bits), ranges from -2^31 to 2^31-1
  • long: 8 bytes (64 bits), ranges from -2^63 to 2^63-1
  • float: 4 bytes (32 bits), used for decimal values (single-precision)
  • double: 8 bytes (64 bits), used for decimal values (double-precision)
  • char: 2 bytes (16 bits), represents a single Unicode character
  • boolean: 1 bit (only true or false values)
See also  How do I merge two dictionaries in a single expression

Example:

public class PrimitiveExample {
    public static void main(String[] args) {
        int age = 25;               // integer
        double price = 99.99;       // floating-point number
        char grade = 'A';           // character
        boolean isJavaFun = true;   // boolean

        System.out.println("Age: " + age);
        System.out.println("Price: " + price);
        System.out.println("Grade: " + grade);
        System.out.println("Is Java fun? " + isJavaFun);
    }
}

2. Object Data Types:

In contrast to primitive types, object data types in Java represent objects, which are instances of classes. Object types are more complex and can hold multiple values or behaviors. These types are derived from the root class Object, which all classes in Java inherit from.

Example:

public class ObjectExample {
    public static void main(String[] args) {
        String name = "John Doe";         // String is an object type
        Integer number = 10;              // Integer is a wrapper class for primitive int
        Double temperature = 37.5;        // Double is a wrapper class for primitive double

        System.out.println("Name: " + name);
        System.out.println("Number: " + number);
        System.out.println("Temperature: " + temperature);
    }
}

Key Differences:

Feature Primitive Data Types Object Data Types
Memory Allocation Stored directly in memory. Stored as references (pointers) to the object in memory.
Default Value Has a default value (e.g., 0, false). Default value is null.
Size Fixed size (e.g., int = 4 bytes). Size varies depending on the object type.
Performance Generally faster due to direct value storage. Slower due to extra memory overhead.
Mutability Immutable (e.g., int can’t be changed to a different type). Can be mutable (depending on class design).
Nullability Cannot be null. Can be null (because they are references).
See also  How Do I Force “Git Pull” to Overwrite Local Files?

Example of Wrapper Classes:

In Java, wrapper classes are objects that “wrap” primitive data types. For example:

  • Integer for int
  • Double for double
  • Character for char

These wrapper classes allow primitive data types to be treated as objects. Here’s an example:

public class WrapperExample {
    public static void main(String[] args) {
        Integer num = 100; // Wrapper class for int
        Double price = 10.5; // Wrapper class for double

        System.out.println("Num: " + num);
        System.out.println("Price: " + price);
    }
}

In this case, the Integer and Double objects are used to store and manipulate primitive values like int and double.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x