1

I know that when declaring object instances in c++ like so:

Object object

the Object constructor is called and memory is provided for that object, however i find that when you do this in java the object instance doesn't have a value until:

object = new Object()

is written. I want to know specifically when memory is provided for the object. I thought that both construction and the new keyword allocated memory so Object object = new Object() seems redundant. I read on oracle's site that declaration "reserves" memory and new "allocates" memory, I would like to know what is the difference between the two.

0

5 Answers 5

5

You need to differentiate between the space required for the variable and the space required for the object. Bear in mind that the value of the variable is just a reference - very much like a pointer in C++. So if you have:

Object x = null;

then the variable x itself takes up enough space for a reference (usually 4 or 8 bytes). Now if you have:

x = new Object();

that creates an object - the value of x is now a reference to the newly created object. x itself takes up the same amount of space as before, but there's also the space required for the object itself (basically the fields, a reference for the type of the object, and data for synchronization and house-keeping).

Sign up to request clarification or add additional context in comments.

Comments

2

When you do something like

Object object = new Object()

in Java, object is a reference to the actual instance on the managed heap. Compared to C++, that's roughly doing

Object* object=new Object()

So when you do

Object object; 

in Java, a place is created for the 'reference' to a instance. Similar to

Object* object;

in C++.

Comments

1
Object foo=null;

in meaning of C++ creates reference foo to the object of class Object. So, it consumes memory for reference only.

Object realFoo=new Object();

creates such reference and also real object with whatever is with this object. So, it is memory for the reference and object itself.

In Java there is no memory reservation - only memory allocation.

Comments

0

declaration reserves memory: parameters and variables inside a method will have memory reserved for them in the stackframe.

allocates memory: at runtime, when executing "new", memory will be allocated for the new Object on the heap

Comments

0

It's important to understand that in Java, Object object is simply a reference to an Object called object. If you're familiar with C++, you can think of this reference as a pointer (though it's not quite the same).

On a 64-bit machine, the object reference is 8 bytes. When you actually instantiate an Object using the new keyword and assign it to the reference, this is where memory is allocated for your Object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.