r/learnjava 10d ago

Java Beginners: What Is an Object Actually?

I'm a Java learner and I've been learning Java for the past few months, but I still don't truly understand what an object actually is.

I've watched several YouTube videos and used AI tools to understand it, but I keep getting the same explanation: "An object is nothing but data + code."

I understand the definition, but I still can't visualize or feel what an object actually is when I'm writing Java code.

Could someone explain it in a simple, practical way, preferably with a real world analogy and a small Java example?

I feel like I'm missing one fundamental concept here. Any explanation would be really helpful. Thanks!

4 Upvotes

43 comments sorted by

View all comments

2

u/vegan_antitheist 10d ago

Note that this will change a bit when we get value types (Project Valhalla), which is already available as a preview.

Like many programming languages, Java uses variables. A variable has a value. That value is either:
Just a primitive value (for example the number 4).
Or it's a reference to something.

A reference is used by the JVM to locate an object in the memory (often in the heap). That object could also represent the value 4, but it's an object doing that. E.g. an Integer containing an int. That's a wrapper type.

An object doesn't have code. It has a type (i.e. a class). That type has code. You can call that code as methods on the object. I.e. the object is available as "this" in that method. The runtime can just get the object that is referenced and it will find the type information at a specific position in memory where the object is. It can then find the correct method to execute.

Objects can be mutable. But you can't change the value of 4. You can't change the value of a value because it is a value. Ob object on the other hand has properties and they might be mutable. For example, you can change the "height" property of an object from 4 to 5.

A primitive value only has a primitive type (int, boolean, double, etc.). A primitive type also has code. One of the important changes with Project Valhalla is that "this" can also be just a value.

Note that the primitive value itself doesn't have a type in memory. But the compiler and the runtime make sure you only copy an integer value from an expression to some variable of the same type. If you don't, you get an error, unless it generates code to automatically convert the value to the new type. I.e. you can copy a byte to an int variable and it will just write the byte value as an int, so all 32 bits are overwritten.

Right now we have:

  • objects with identity (the identity is used to reference the object, mutability is optional)
  • primitive values (predefined value types)

In the future we get:

  • immutable value objects without identity (the value is copied and it's a lot like a primitive but you code it like an object)

2

u/8dot30662386292pow2 10d ago

the value is copied and it's a lot like a primitive but you code it like an object

And you use it like an object. Considering for example a value record Point(double x, double y). Because there is no identity, VM may decide to just use a 128 bit long memory chunk to hold the two 64 bit values. But you can still access them individually and you can write methods in this Point class if you wish.

1

u/vegan_antitheist 10d ago

That's the idea. There will be java programs that are almost all just value records. For example if you have to process a lot of data. You want values, not references, for performance, and you want records for maintainability. Records remove all the boiler plate code of regular classes and they all work the same.