DEV Community

Yoshi
Yoshi

Posted on

The Moment I Realized the Essence of Java's Primitive Typesđź’ˇ

While studying the concept of "Generics" in Java, I realized my understanding of "types" was quite shallow. Through personal research, I gained new insights into Java's type system, which I’ll share here.

My Understanding of "Types"

In a previous article I wrote, What Is Polymorphism? Exploring Type-Object Relations in Java, I explored the relationship between declarations and types in Java. To summarize my understanding:

  • Declaration: Defines what kind of data is stored in a memory location.
  • Type: Determines how the stored data can be used.

Types act like filter lenses that define how we perceive the underlying data. By changing the lens, we can focus on shared methods from a parent class or specific behaviors exposed through an interface.

Object Types? Reference Types?

While I grasped the broad concepts, terms like primitive type, reference type, and object type caused confusion. Particularly, the multiple terms used to describe object types seemed to be the root of this confusion:

  • Primitive Types
    • Predefined by the programming language.
    • Store data directly in memory.
    • Also called "value types."
  • Object Types
    • Any data type other than primitive types.
    • Store data in the heap, with variables holding a reference (pointer).
    • Also referred to as "reference types" or "non-primitive types."

Official Definition of Types

Everything seemed to trace back to primitive types. Referring to official documentation, I found the following eight primitive types:

Primitive Type Wrapper Class Size (bits) Default Value Main Uses and Features
byte Byte 8 0 For small integers. Useful for memory efficiency and collections.
short Short 16 0 Slightly larger range than byte. Suitable for moderately small integers.
int Integer 32 0 Default integer type. Commonly used in numeric computations.
long Long 64 0L For larger integer ranges, such as timestamps.
float Float 32 0.0f Single-precision floating-point numbers. Used for moderate precision.
double Double 64 0.0d Double-precision floating-point numbers. Default for decimals.
boolean Boolean 1 (logical) false Represents true or false. Common in flags and state management.
char Character 16 '\u0000' Unicode characters. Used for string manipulation or single-character processing.

Each primitive type has a corresponding wrapper class. These wrappers are essential for operations that require objects, such as collections or generics. Additionally, the String type, while not a primitive type, is given special treatment and behaves somewhat similarly to them.

Primitive Types as Atoms, Object Types as Molecules

One statement in the official documentation struck me as particularly enlightening:

You may have noticed that the new keyword isn't used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class. A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. As shown below, it's possible to assign a literal to a variable of a primitive type:

boolean result = true;  
char capitalC = 'C';  
byte b = 100;  
short s = 10000;  
int i = 100000;

This description helped me see primitive types and object types as analogous to atoms and molecules. Primitive types are the "atomic" building blocks of programming—indivisible and simple. Object types, on the other hand, are "molecules," composed of these atomic elements to create more complex structures.

Primitive types, like atoms, can only hold simple data. However, combining them with object types and adding behaviors through methods enables us to represent complex real-world phenomena. Consider the following class:

public class Car {
    private String brand; // Object type
    private int year;     // Primitive type
    private double price; // Primitive type
}
Enter fullscreen mode Exit fullscreen mode

Classes combine fields of primitive and object types, along with methods, to define their behaviors. Using primitive types as fundamental building blocks, we can construct intricate data structures.

Through this exploration, my understanding of types has deepened. I hope this article proves useful to someone out there.

References

The Java™ Tutorials (Primitive Data Types)

Top comments (0)