This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Concat

 
jignesh soni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String str1 = "1st string";
String str2 = "concat to 1st string";
str1.concat(str2);

str1 is a reference for 1 st string str1 is reference for 2nd string when we concat these two string, there will be a new object on the heap. WhaT WILL BE THE REFERENCE FOR THIS NEW OBJECT ?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look to whom your are assigning the reference..!!!

if you can find the reference you have your answer...!!
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Objects of the String class are immutable. This means that str1 and str2 don't change. So, what could the method concat() possibly do? Create a new object of class String....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are making a bad assumption here, that there MUST BE a reference to a new object. there does not. it is perfectly legal to create an object and have no reference variable pointing to it.
 
jignesh soni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that means, if I concat str1 to str2 then both the original strings will stay in place as they are with their originalo references pointing to them only and a new concateed String object str1 + str2 will be created without having any reference, is that right ? If thats the case then wont this new object immediately be GC'd as it doesnt have any reference and if thats the case then whats the point in using concat if its immediately GCd on creation ?

Pls explain
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very common to create objects that get used once and then get released for GC.

Example:
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If thats the case then wont this new object immediately be GC'd as it doesnt have any reference


The newly created String object will probably not immediately GC'd, but will be immediately eligible for GC. It might get GC'd the next time that garbage collection is done.
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if thats the case then whats the point in using concat if its immediately GCd on creation ?


Just because you are using the concat() method in a pointless manner, doesn't mean that the method pointless. You can also use concat() like so...



Henry
 
jignesh soni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do you know when next GC is and my other part is unanswered yet. I am not clear wheen we concat two strings, third string will be created and which one of these three strings will not have reference, probably newly created concatted string right ?
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No one except teh JVM would know when the next GC cycle will be. You SUGGEST the JVM to run the GC by calling System.gc() but it still depends upon the JVM.

And regarding your so-called second question, it all depends upon the way you write your code. If you write



you have three strings references, str pointing to the newly created string object.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jignesh soni:
So how do you know when next GC is and my other part is unanswered yet. I am not clear wheen we concat two strings, third string will be created and which one of these three strings will not have reference, probably newly created concatted string right ?


if you write the code as in your original post:

String str1 = "1st string";
String str2 = "concat to 1st string";
str1.concat(str2);

then yes, str1 still refers to "1st string", str2 still refers to "concat to 1st string", and there is no reference to the newly created string "1st stringconcat to 1st string". This newly created string is ELIGIBLE for garbage collection, but that may never happen... or it may happen right away. there is no way to know.

HOWEVER, if you change your code to this:

String str1 = "1st string";
String str2 = "concat to 1st string";
String str3 = str1.concat(str2);

after this code runs, you will HAVE a reference to the newly created string, and can use it later in your program.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have written the code as



the newly created string "1st stringconcat to 1st string" is never garbage collected as it is created at the compile time (classloading time rather) and concatenated string literal is placed in the string literal pool.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic