• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Created String Object (Java OCA 8 Programmer I Study Guide, Sybex)

 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear team,
I am trying to study to get the OAC, and in that moment i am doing the String part of the Oracle Study Guide (chapter 5).
The chapter reports this example (page 260):



I would like to knon how many objects will be created by this code in the heap.
The book says it creates 2 objects ("Java" and "Java Rules!") so I can't understand if the " Rules" object is created in the Heap too.

I am looking forward to to hear from you.

Best regards
Nicola
 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have only one String variable so, in the first line of code your s should be x.

So in my opinion there will be more the two objects in the String pool.

1. "Java" ; from initialization
2. " Rules!" ; from x.concat(" Rules!")
3. "Java Rules!" ; is created from concatenation
4. "x = " ; is a String object from println
5. "x = Java" ; the object created from "x = " + x;

 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Claudiu,

I didn't understand that


creates an object into the pool.
So, can you confirm that for each literal string we write in our code, it will be located into the pool?

best regards

Nicola
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this link, recommended by Roel, very useful on the topic of strings and the string pool

hope it helps!
 
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
Nicola Viola wrote:So, can you confirm that for each literal string we write in our code, it will be located into the pool?

The post from Claudiu is absolutely spot-on Have a cow!

String literals are a little bit special and that's probably why the study guide doesn't focus on these literals. But every String literal is definitely a String object. And it might even be reused many, many, many times. Illustrated in this code snippetThis code snippet prints true, so that means that both reference variables s1 and s2 refer to the same object (and only one String object will be created). In fact, that's one of the reasons why a String object must be immutable, otherwise the same String object can't be reused.

Now let's have a look at your code snippetSo in these statements three String literals (objects) are used: "Java", " Rules!", and "x = ". On line1 another String object (not a literal) is created: "Java Rules!". A String object is (as you know) immutable and therefore can't be changed (so reference variable x will still be referring to the String literal/object "Java"). Because the newly created String object "Java Rules!" is not assigned to a reference variable, this String object is lost and will be eligible for GC after line1 completes. On line2 another String object (not a literal) is created: "x = Java". This String object is used as a parameter of the println method and printed to the console (after line2 is completed, this String object will be eligible for GC as well).

Hope it helps!
Kind regards,
Roel
 
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
Nicola Viola wrote:I am looking forward to to hear from you.

I think "String (and the number of objects created/eligible for GC)" is without any doubt the most popular topic in this forum. So using the search function you'll find plenty of topics about "String (and the number of objects created/eligible for GC)". These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls) about String (and the number of objects created/eligible for GC):
  • Question about strings
  • Garbage Collection Question
  • Confusion with String object (String Pool & Garbage Collection)
  • Confusion with String...
  • new String("abc") - one object or two?
  • strings and the string pool
  • OCA Java SE 8 Programmer - Question on String immutability
  • String method chaining and garbage collector
  • Understanding Equality, on page 118 (Java OCA 8 Programmer I Study Guide)
  • Doubts about String Concatenation Operator & Garbage Collection (K&B7 CH5 Q9)

  • I think you know what to do today

    Hope it helps!
    Kind regards,
    Roel
     
    Nicola Viola
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you indeed to everyone!

    Best regards

    Nicola
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic