DEV Community

Laxmi Anandache
Laxmi Anandache

Posted on

Is Java Playing Favorites: Primitives vs. Objects?

Ever wondered what would happen if you typed:

Date date;
date.now();

Well, boom, instant compile-time error! Why? Because date is just a variable, not an object yet. It's like showing up at a party without your invitation—no object, no method access!

To make things right, you have to create an object first, like:

date = new Date();
Or, if you want to be fancy, you could:

Date yesterday = new Date();
date = yesterday;

But hold on a second—are these Java object variables the same as C++ pointers? Let’s dive into that. In Java, variables don’t work the same way they do in C++. Java variables are passed by value, and objects and variables are not the same thing.

In Java:

Objects are stored in heap memory.
Reference variables that point to objects live in the stack.
So, when it comes to passing by value, here’s how it goes down:

  1. Passing Primitives: Let’s start with a classic—primitives. These little guys are pass-by-value champs.

`
public class PrimitivesUnitTest {
@test
public void whenModifyingPrimitives_thenOriginalValuesNotModified() {
int x = 1;
int y = 2;

    // Before Modification
    assertEquals(x, 1);
    assertEquals(y, 2);

    modify(x, y);

    // After Modification
    assertEquals(x, 1);  // x stays unchanged!
    assertEquals(y, 2);  // y stays unchanged!
}

public static void modify(int x1, int y1) {
    x1 = 5;
    y1 = 10;
}
Enter fullscreen mode Exit fullscreen mode

}
`
In this case, the x and y are primitive types, so they’re like little treasure chests of value. When we pass them to modify(), we don’t send the chests—just copies of the values inside them. Any changes to x1 and y1 won’t affect the original treasure chests x and y. Magic!

  1. Passing Objects: Now, things get more interesting with objects. In Java, objects live in the heap, and reference variables are the keys to access them (stored in the stack).

When you pass an object, Java doesn’t send the object itself—it sends a copy of the reference (aka the key) that points to the object in the heap. It’s like giving someone a copy of your house key. They can go into your house, but you both have the same key! So, any changes made inside the house (object) will be reflected in both places.

```public class ModifyObjects {
public static void main(String[] args) {
Date date = new Date();
modifyDate(date);
System.out.println(date); // Changes are reflected here!
}

public static void modifyDate(Date date) {
    date.setYear(2025);  // Modify the object in the heap
}
Enter fullscreen mode Exit fullscreen mode

}```

Here, we’re passing the reference to the date object, not the actual object itself. Both the caller and the method share the same object in memory. Any change made to date in the method will show up everywhere.

Wrapping it Up: All About Passing by Value
So, what’s the takeaway? Everything in Java is passed by value—but, when we deal with objects, it’s the reference value that gets passed, not the actual object. Objects are passed by reference, but the reference itself is passed by value. Confused yet?

Here’s the recap:

Primitives: You’re passing copies of the actual values.
Objects: You’re passing copies of the reference to the object. You’re still sharing the same object in memory, though!
In Java, pass-by-value is the rule. But with objects, it’s like passing the key to the kingdom, not the whole castle.

So it was not that difficult posting my first article.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs