Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects created?

 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects will be created: (Assumed "String Constant Pool" is empty)

String s0 = "xxx";
String s1 = new String();
String s3 = new String("abc");
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources

A very helpful article can be found here.
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = new String("abc");

<OCA/OCP Java® SE 7 Programmer I & II Study Guide>
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamoba Black wrote:
String s = new String("abc");

<OCA/OCP Java® SE 7 Programmer I & II Study Guide>

I checked my copy and couldn't find this question. And I couldn't find it in the OCA exam 1 & 2 exams either. Can you be a little more precise? Page number or question number for example.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what's your actual question/doubt? Do you know the correct answer, but don't you have any idea why it's correct? Or do you want to know the correct answer to this question?
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel, just read the article, it is nice.

But still confused. According to the book.:

Assumed the "String Constant Pool" is EMPTY,
String s = new String("abc");
will create "abc" in the Pool first, then create another object by using "new"
so, two objects created.

but I am not sure is it correct. Cause it is the first time I see this example.
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P265
String s = new String("abc"); // creates two objects,
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamoba Black wrote:P265
String s = new String("abc"); // creates two objects,

Ah, it was not a mock/practice question, but just some text in one of the chapters

Jamoba Black wrote:Assumed the "String Constant Pool" is EMPTY,
String s = new String("abc");
will create "abc" in the Pool first, then create another object by using "new"
so, two objects created.

but I am not sure is it correct. Cause it is the first time I see this example.

All string literals are placed inside the String Constant/Literal pool. "abc" is a String literal, so it's created on the heap and referenced from the String Constant/Literal pool. Because you use the new keyword, the JVM must create a new String object at run-time, rather than using the one from the String Constant/Literal pool. So you'll have 2 "abc" strings on the heap: one referenced from the String Constant/Literal pool and one referenced by the reference variable s. A similar scenario is mentioned in the article I mentioned in my 1st post (String two = new String("someString");)

Let's assume we add the following lines of code:How many objects will be created with each line?

Hope it helps!
Kind regards,
Roel
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, Roel!
I learnt something new.

Meanwhile, I may found some conflict in Mala's book:
P178, example under "COUNTING STRING OBJECTS"
The code at 2 creates a new String object with the value "Summer" and places it in the String constant pool.
- seems to have conflict with what we discussed above.
If we are right, it should be 1, instead of 2, who creates the new String "Summer" and places in the Pool?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamoba Black wrote:
The code at 2 creates a new String object with the value "Summer" and places it in the String constant pool.
- seems to have conflict with what we discussed above.
If we are right, it should be 1, instead of 2, who creates the new String "Summer" and places in the Pool?

Why do you think 2 String objects are created? It says "the code at 2", not "the code creates 2".

Here is the statement from the book:A String object with value "Summer" is created. A reference is added to the String Literal/Constant pool (because it's a String literal). And the same reference is assigned to the reference variable summer2.

Hope it helps!
Kind regards,
Roel
 
Jamoba Black
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't express well.

Page 178 of Mala's book:



Since line 1 has created two objects, one object is "non-pool" by "new" operator, while the other is a String literal (within double quotes) which is put in the Constant Pool.
Line 2 can not create another "Summer" since Line 1 has created a "Summer" in the Pool.

but the book says "The code at line 2 creates a new String object with the value "Summer" and places it in the String constant pool."
which means the author does not think Line 1 has created a String literal "Summer" in the Constant Pool?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamoba Black wrote:Since line 1 has created two objects, one object is "non-pool" by "new" operator, while the other is a String literal (within double quotes) which is put in the Constant Pool.
Line 2 can not create another "Summer" since Line 1 has created a "Summer" in the Pool.

but the book says "The code at line 2 creates a new String object with the value "Summer" and places it in the String constant pool."
which means the author does not think Line 1 has created a String literal "Summer" in the Constant Pool?

Probably the author didn't think of the String at line1 already being in the String Constant/Literal Pool. Or maybe line1 was added later on, but the explanation of line2 wasn't revised.

But just to be 100% clear:
  • at line1: the string "Summer" is created and a reference is added to the String Constant/Literal Pool and (because of the new keyword) another string "Summer" is created and the reference is assigned to the reference variable summer (and is not added to the String Constant/Literal Pool)
  • at line2: no new String object needs to be created, because the string "Summer" is already in the String Constant/Literal Pool, this reference is assigned to the reference variable summer2


  • Hope it helps!
    Kind regards,
    Roel

    (PS. I have notified the author about this errata item in the study guide)
     
    Jamoba Black
    Ranch Hand
    Posts: 40
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Roel!
    I learnt a lot from this topic.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jamoba Black wrote:I learnt a lot from this topic.

    That's the whole intent of these forums

    Nice to know: instead of making a "thank you" post, you could also the post(s) which you liked. It's easier, faster and other ranchers will see immediately which are the "starred" posts.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic