• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Concat

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look to whom your are assigning the reference..!!!

if you can find the reference you have your answer...!!
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic