• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

String Objects

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for doing this testing Punit. I ran your code again and I got the same results. I didn't expect to get "PunitSoto " because I think that gets garbage collected right away because it's just a side effect in that whole expression. The one that I am not sure I understand though is why "Punit" is not in the heap. I am wondering if when java does the initial parsing checking for literal strings and building strings from constant string expressions it figures out, after doing all the parsing, that once the final reference has been used, it doesn't get used again, and decides to garbage collect it. This doesn't make too much sense, but it's just a hypothesis. What is clear is that either the object doesn't get created, or is garbage collected (I think this is more likely) before exit() is called. I also ran the test deleting the final modifier, and in that case the string appears (I am getting 5 strings.)

This heap thing is interesting as an experiment, but I don't think we can extract completely reliable conclusions from it.
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Punit" is not created because the compiler wipes that statement out. The compiler will remove the declaration of the final string and replace the value of the final constant with the actual value. So if you write this

final String s = "Punit";
String s1 = s + "Singh";

Then the compiler would make it

String s1 = "PunitSingh";
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit, so what you are saying is that no String objects are created in the heap for final String references that are compile time constants? That's very interesting, I didn't know that. But yes, I think it makes sense. A final String that is a compile time constant can just be wiped out after replacing all its occurrences in the code by the literal string. Whereas a non-final reference could actually be linked to a different String object at runtime.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:"Punit" is not created because the compiler wipes that statement out. The compiler will remove the declaration of the final string and replace the value of the final constant with the actual value. So if you write this

final String s = "Punit";
String s1 = s + "Singh";

Then the compiler would make it

String s1 = "PunitSingh";



Just for more excitement, I just add one line System.out.println(s); than I got "Punit" in the dump file.



Punit (24 bytes)
PunitSoto (24 bytes)
Ruben (24 bytes)
PunitSingh (24 bytes)
PunitSoto Ruben (24 bytes)



What's happening here guy?

 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program that you gave punit will look like this after compilation

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:The program that you gave punit will look like this after compilation



Hmmm great means we have found a new behavior for final String object. Great....
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what the class showing after decompilation:


 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think the same would happen if you had something like:
final String s = "Text";
String s1 = s; // "Text" will be created

Is it possible that this is too advanced for the purposes of the exam?

Bert mentioned that there won't be questions about Strings in the context of GC, but he didn't say anything about number of objects created questions.

What to answer if the exam asks how many Strings will be created in the StringTest2 example with a final String?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Yes, I think the same would happen if you had something like:
final String s = "Text";
String s1 = s; // "Text" will be created

Is it possible that this is too advanced for the purposes of the exam?

Bert mentioned that there won't be questions about Strings in the context of GC, but he didn't say anything about number of objects created questions.

What to answer if the exam asks how many Strings will be created in the StringTest2 example with a final String?



I think there won't be this type of question. If happened then Bert can reply whether should we consider final String object creation or not.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very nice work done, thanks.

I'm anxious to hear whether this behaviour is covered by the exams or not.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Ronald. I expect there shouldn't be any questions in the exam like this, but if I find one I'm going to feel very silly (especially if I answer it right and I don't get the point.) :lol:
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:Excellent Stuff Ruben, I did all the things you said.
I ran this program:



Hi puneet, I am little confused with line 2 (String s0 = s + "Singh"), As i am thinking there are 2 objects
1. "Singh"
2. s +"Singh" = Punitsingh which is assigning to String S0

Please clear it to me..
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sunil langeh wrote:

Hi puneet, I am little confused with line 2 (String s0 = s + "Singh"), As i am thinking there are 2 objects
1. "Singh"
2. s +"Singh" = Punitsingh which is assigning to String S0

Please clear it to me..



No String s is declared final here so it is compile time constant only, so compiler immediately replaces all its occurrences in the code. And s+"Singh" is executed at compile time, it is not delayed for runtime. If String s was not declared final, than s+"Singh" was executed at run time, then only there will be 2 objects as said by you.
 
sunil langeh
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, Got it, Thanks Puneet
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more fly in the ointment

did You forget about java.lang.StringBuffer


String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code:

x = "a" + 4 + "c"

is compiled to the equivalent of:

x = new StringBuffer().append("a").append(4).append("c")
.toString()




 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what the duce?

I keep wondering where " " went. "Punit" not being there is something understandable.

Also, I wonder why there is no build up String object "PunitSoto ".

Does this program just check application references and find the objects that they refer to? It shows all the Strings after expressions have been worked through. Is it possible to dump the SCP solely?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pavel Cherkashin wrote:one more fly in the ointment
did You forget about java.lang.StringBuffer


String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code:
x = "a" + 4 + "c"
is compiled to the equivalent of:
x = new StringBuffer().append("a").append(4).append("c")
.toString()




Since Java 5, I think StringBuilder().append() is called.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic