mergesort mergesort - 2 years ago 62
Java Question

Does the Java constructor return an object or an object's ID when called?

I want to know if a constructor in Java returns something. I know there is no return value like '5' or "Hello World." But if we are assigning a variable to it:

People person = new People();

Then wouldn't it logically make sense for the object or ID to be returned? Is the reference in memory where the object is stored assigned to people?

I am just thinking about this now because I am learning python, and want to connect the
method to a constructor and then
to the body of the constructor (i.e. the initial values). My professor keeps telling me
doesn't exist, so I am hoping to get an answer to make things clearer.

Answer Source

In Java you only have primitives and references to objects as types for fields, parameters and local variables. A reference is a bit like an ID, except it can change as any moment without you needing to know when this has happened.

A reference is closer to the concept of a pointer or object index. ie. it refers to a memory location.

new is definitely a keyword in Java, so saying it doesn't exist isn't very meaningful. You could say it doesn't have a one to one mapping in byte code, except byte code is itself run on a virtual machine and the machine actually run could be rather different anyway. i.e. there isn't much point treating byte code as the way things "really" happen.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download