Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Object creation

Greg Morphis
Greenhorn
Posts: 13
Hopefully just a simple question..
I'm reading the SCJP 6 Study Exam and there's an example of creating an 2-D array of objects.

The book says there's 8 objects created:
1 2-D Cat [][] array object
2 Cat[] array object
5 Cat object

However since you're passing in a String, I'm assuming that the Cat class has a

instance variable and that the constructor is setting that variable, for example Cat("Bilbo")
Is it safe to assume that there's actually 13 objects created?

1 2-D Cat [][] array object
2 Cat[] array object
5 Cat object
5 String object
?
Are there tricky questions like this on the exam?
Now my second question is given the code above, how best is it to break it down and visualize it?
Do you first split the outer {} and then inner {}'s ?
How complex do these get on the exam? {{{{{{{{{{{{{}}}}}}}}}}} deep?

Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Hello Greg, of the five questions you asked, I want to try and give you some answers, two dimension arrays, three dimension arrays etc are the same in the sense that we are talking about 'Domension', if you understand array 'Dimension' you will be able to resolve such problems no matter how 'deep' an exam question can go. The following code will give you an idea of how to break the array and visualize it:

The aspect of 'Trickish Questions' like this on the exams, I am afraid I dont know, I guess we need some help to clarify that.
I hope this helps.

Paul Clapham
Sheriff
Posts: 21443
33
You need to realize that setting the value of a variable is not the same as creating an object. If the variable is of a primitive type (e.g. int, double, and so on) then naturally there are no objects involved at all. If it's of some other type, then "assigning an object" to the variable does not create another object, it simply assigns a reference to the object to the variable. (Think of that as a "pointer" if it helps.)

Greg Morphis
Greenhorn
Posts: 13
I understand that setting a value is not the same as Object creation but Strings are Objects and if you say new Cat("Fluffy");
You're creating a new Cat Object and initializing some String variable (Object) to "Fluffy".
So that looks like 2 Objects on the heap to me. 1 for the Cat and 1 for the String Object. Is that not right?
So 5 Cat objects + 5 String variables = 10 Objects, plus the 2 Cat array objects plus the 2-D Cat array object.

The book says that only 8 Objects are created but if you're passing in a String value in the Constructor, there must be a String instance variable.
I just don't want to see a question like this on the test and answer one way and be wrong.

Should the answer be 8 or 13..
If you saw this on the exam with both 8 and 13 as options, what would you pick?

Paul Clapham
Sheriff
Posts: 21443
33
• 1
Greg Morphis wrote:You're creating a new Cat Object and initializing some String variable (Object) to "Fluffy".
So that looks like 2 Objects on the heap to me. 1 for the Cat and 1 for the String Object. Is that not right?

That I don't know. Technically, String literals (which are indeed regular String objects) are stored in a special place (called something like "string pool") and they are already there when your program starts running. So nothing in the posted code actually creates them, although they certainly are created at some point. That makes this sort of question rather ambiguous when you throw in some String literals.

However if you need to get the "right" answer to this sort of question with String literals, find out what the test writers consider to be the right answer (i.e. do String literals get counted or not) and follow that rule.

Mala Gupta
Author
Ranch Hand
Posts: 359
10
• 1
Greg,

>>>>>>>>>>>>>>>>
Are there tricky questions like this on the exam?
>>>>>>>>>>>>>>>>
Yes.

>>>>>>>>>>>>>>>>
Now my second question is given the code above, how best is it to break it down and visualize it?
Do you first split the outer {} and then inner {}'s ?
>>>>>>>>>>>>>>>>

Though the exam questions like to fit in multiple lines of code on the same line, it is always advisable to indent your code to make it much more readable. For example, you can indent the following line of code:

as follows:

The second (indented) line of code makes it much easier to track the number of objects created (5 Cat objects, 2 Cat array objects, 1 2-D Cat array object)

>>>>>>>>>>>>>>>>
How complex do these get on the exam? {{{{{{{{{{{{{}}}}}}}}}}} deep?
>>>>>>>>>>>>>>>>

Not more than 3 levels.

>>>>>>>>>>>>>>>>
Is it safe to assume that there's actually 13 objects created?
>>>>>>>>>>>>>>>>

It will be interesting to note that when you execute code new Cat("Fluffy"), you might be using the String literal value "Fluffy", from a pool of String values. You *always* create a new Object, if you create a String object by using the keyword new, as follows:

However, if you use a literal String value as follows:

Java can reuse this String from the pool of String values that it maintains. Hence, line of code in question does not necessarily create new String values.

>>>>>>>>>>>>>>>>
If you saw this on the exam with both 8 and 13 as options, what would you pick?
>>>>>>>>>>>>>>>>

I would choose 8 because I can't be sure about *creation* of the String values. Java can use these values from the pool of Strings, if available (no creation involved in this case).

cheers
Mala

Mala Gupta
Author
Ranch Hand
Posts: 359
10
Oops! can't delete the post! It was submitted twice, when I refreshed the page, after getting hold of the JavaRanch team, red faced, while they were tinkering with the server! :-)

Greg Morphis
Greenhorn
Posts: 13
Many thanks Mala, that's the answer I was looking for.
And thank you too Paul and Jacob for helping explain as well!
When you mention "you can indent your code" do you mean within their software or do you write it down on a piece of paper like that?
I assume you mean you can copy their code onto a piece of paper and write it the way you want. Correct?
Again, thanks!

Andreas Svenkson
Ranch Hand
Posts: 179
I disagree with you Mala.

I would choose 8 because I can't be sure about *creation* of the String values. Java can use these values from the pool of Strings, if available (no creation involved in this case).

I would choose 13. Why? Because of the fact that you have been given a specific code and what isn't in that piece of code is not part of the question. Hence, every one of those constructors create a String object on the heap (inside the String pool).

I don't think it makes sense to assume that the Strings COULD have been created IF some other code than that specified had been executed first.

// Andreas

Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Andreas Svenkson wrote:I disagree with you Mala.

I would choose 8 because I can't be sure about *creation* of the String values. Java can use these values from the pool of Strings, if available (no creation involved in this case).

I would choose 13. Why? Because of the fact that you have been given a specific code and what isn't in that piece of code is not part of the question. Hence, every one of those constructors create a String object on the heap (inside the String pool).

I don't think it makes sense to assume that the Strings COULD have been created IF some other code than that specified had been executed first.

// Andreas

Ikpefua wrote:Hello Andreas, there is a part where I agree with Mala, when you create a String with the 'new' keyword eg; You are effectively placing an Object on the heap. 'String literals' are obtained from the String pool, hence an Object is NOT placed on the Heap, you know 'String Pool' is a kind of 'Heap' for Strings only, think about the logic.

Andreas Svenkson
Ranch Hand
Posts: 179
Yes I see what you mean Ikpefua, but I was (am) under the impression that the String pool is on the heap. But perhaps I am mistaken there? Gonna have a look in K&B...

EDIT: Come to think of it, it shouldn't matter... Where these String objects are stored should be irrelevant, the main issue here is if any new String objects are created. I stand by my first post...

// Andreas

Mala Gupta
Author
Ranch Hand
Posts: 359
10
Andreas,

>>>>>>>>>>>
I don't think it makes sense to assume that the Strings COULD have been created IF some other code than that specified had been executed first
>>>>>>>>>>>

The creation of String literal objects can not be confirmed/ is not obvious. It is matter of *how* the String objects are created and used in Java. String literal values can be reused from pool, if available. Since we know how Java treats the String literal values, it makes sense to exclude these species in a question which tests you on the number of objects that are (obviously) created.

cheers
Mala

Mala Gupta
Author
Ranch Hand
Posts: 359
10
Greg,

>>>>>>
When you mention "you can indent your code" do you mean within their software or do you write it down on a piece of paper like that?
I assume you mean you can copy their code onto a piece of paper and write it the way you want. Correct?
>>>>>>

You cannot modify/ indent any code that is displayed in the real Java Programmer exam. In this case, the best bet is to indent it using paper.

When you are working with a mock exam/ questions, you can copy the code and indent it using your favorite editor.

cheers
Mala

Greg Morphis
Greenhorn
Posts: 13
I found this in the book, which rests my nerves a little!
"Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too"!
So it appears as though Strings, when talked about in the context of garbage collection, don't matter on the exam!

Andreas Svenkson
Ranch Hand
Posts: 179
Mala Gupta wrote:Andreas,

>>>>>>>>>>>
I don't think it makes sense to assume that the Strings COULD have been created IF some other code than that specified had been executed first
>>>>>>>>>>>

The creation of String literal objects can not be confirmed/ is not obvious. It is matter of *how* the String objects are created and used in Java. String literal values can be reused from pool, if available. Since we know how Java treats the String literal values, it makes sense to exclude these species in a question which tests you on the number of objects that are (obviously) created.

cheers
Mala

I'm not sure I know what you mean mate. If the above code is the only code executed, then all those String literals used in the constructors will be added to the literal pool the moment they are used, creating String objects on just in time for their usage. Do you mean that this is not certain? Because I think it is, as long as no other code has executed before it.

// Andreas

Bert Bates
author
Sheriff
Posts: 8900
5
Hi Guys,

I think that we will "fix" K&B to say, "...other than String objects, how many objects will be created?" - We were really trying to talk about the object-ness of arrays, not trying to trick you about String objects.

In general, on the real exam, you should assume that (unless the exam explicitly says otherwise), when you look at a piece of code, the String pool is empty before that code executes.

hth,

Bert

Joanne Neal
Rancher
Posts: 3742
16
Bert Bates wrote:you should assume that (unless the exam explicitly says otherwise), when you look at a piece of code, the String pool is empty before that code executes.

But according to StringsLiterally that's not a valid assumption.
When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table.

So all the literals will exist before the code that creates the array is run.

Bert Bates
author
Sheriff
Posts: 8900
5
Hi Joanne,

I was talking ONLY about the exam - not the real world

Bert

Mala Gupta
Author
Ranch Hand
Posts: 359
10
Andreas Svenkson wrote:
I'm not sure I know what you mean mate. If the above code is the only code executed, then all those String literals used in the constructors will be added to the literal pool the moment they are used, creating String objects on just in time for their usage. Do you mean that this is not certain? Because I think it is, as long as no other code has executed before it.

// Andreas

Andreas,

I mean that the creation of new String objects cannot be confirmed, when you execute this line of code. This is due to the existence of the String pool, as discussed in various posts in this thread.

As you stated, "If the above code is the only code executed, then... ", the creation of new String objects is conditional. Hence, their creation isn't certain.

As Bert mentioned - the question should have been "...other than String objects, how many objects will be created?"

However, my friend, you may encounter a similar 'ambiguous' question/ statements in the real exam. As is said - "to err is to human" and the questions are framed by humans!

cheers
Mala

Andreas Svenkson
Ranch Hand
Posts: 179
Mala Gupta wrote:
As you stated, "If the above code is the only code executed, then... ", the creation of new String objects is conditional. Hence, their creation isn't certain.

As Bert mentioned - the question should have been "...other than String objects, how many objects will be created?"

cheers
Mala

Well, what I was (and am) saying is that; if the above code is the only code part of the question, then that is the only code that should be taken into account. As such, this code WILL cause the String literals to be placed in the string constant pool.

Joanne makes a good point about the fact that the String literals are placed in the constant pool at class loading time, but imo we are still in a position to answer: "how many objects does this code produce", and even if the string objects are created at class loading time (and not when the constructors are invoked), they are still created by this code - which is the only code we know of and are in a position to judge the outcome from!

We are not in a position to assume that another class has loaded before this code, and used the same string literals. This to me is the key issue of this question, such an assumption feels almost "outrageous" to me in terms of answering a question like this.

And... I think that Bert means that this question "shouldn't" have used string literals, and if it hadn't then it would have made all the difference. But if it does, like now, then they should be taken into account. (Correct me if I am wrong Bert??)

Regards,
Andreas