1
String str[] = new String[3];

Can someone explain to me the memory map for the above line. How many objects and ref it will have?

4 Answers 4

7

That line allocates one object, which is an array of three String references. These references are initialized to null by default.

It also defines str as a local variable holding a reference to an array of Strings. It initializes the variable with a reference to that object it just created.

So you have this diagram:

str (local variable)
    +--------+             array object    
    |   -----+---------> +-------------+
    +--------+           |    null     | (can hold a reference to a String)
                         +-------------+
                         |    null     | (can hold a reference to a String)
                         +-------------+
                         |    null     | (can hold a reference to a String)
                         +-------------+
Sign up to request clarification or add additional context in comments.

Comments

1

new String[3] will create one object, array of 3 String references, initialized with nulls. Object size (for 32-bit JVM) = header(8) + length(4) + 3 references (4 bytes each)

Comments

0

It will Have one Array instance with the reference of 3 String objets.

Comments

0

Also, noticing that you are probably new to this(might just be a typo, sorry if I am wrong), a correction to your post. The correct syntax in your code snippet should be

String[] str = new String[3];

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.