wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes String Concat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Concat" Watch "String Concat" New topic
Author

String Concat

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
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 ?
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

look to whom your are assigning the reference..!!!

if you can find the reference you have your answer...!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

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

Joined: Oct 02, 2003
Posts: 11161
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
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

Joined: Dec 11, 2004
Posts: 13410

It's very common to create objects that get used once and then get released for GC.

Example:


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

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
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
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

Joined: Oct 10, 2005
Posts: 110
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

Joined: Oct 02, 2003
Posts: 11161
    
  16

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

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Concat
 
Similar Threads
Referencing to another object
String question
Is Kathy&Bert book wrong? Or my understanding wrong?
String class
Strings