Javatpoint Logo

Java String

By: bhnprt*** On: Mon Dec 04 12:16:32 IST 2017     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
hi, i'm new to java, need some help
In java string cancat() here-[https://www.javatpoint.com/java-string-concat], they explained concat () method, but the java Strings are immutable. so how the concat() method is working with Strings, please explain
i'm confused....
Up0Down
core java  x  328Tags

 
The String concat() method concatenates the specified string to the end of current string. Syntax:

public String concat(String another)

example of String concat() method.

class TestStringConcatenation3{
public static void main(String args[]){
String s1="Sachin ";
String s2="Tendulkar";
String s3=s1.concat(s2);
System.out.println(s3);//Sachin Tendulkar
}
}

when we write String s3 ,String object is created at string constant pool and value of s3 is sachin tendulkar.
Image Created0Down

By: [email protected] On: Wed Dec 06 12:00:09 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
The String concat() method concatenates the specified string to the end of current string. Syntax:

public String concat(String another)

example of String concat() method.

class TestStringConcatenation3{
public static void main(String args[]){
String s1="Sachin ";
String s2="Tendulkar";
String s3=s1.concat(s2);
System.out.println(s3);//Sachin Tendulkar
}
}

when we write String s3 ,String object is created at string constant pool and value of s3 is sachin tendulkar.


Regards
Priyanka Jadhav.
www.sevenmetor.com
Image Created0Down

By: [email protected] On: Wed Dec 06 12:01:48 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
thank you!
one more question,


how many objects will be created here? String s3=s1.concat(s2);
Image Created0Down

By: [email protected] On: Wed Dec 06 12:38:28 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Hi there,
According to your condition there are 2 situations here.
1. if you are concatenating 2 strings and storing them in another variable. So in that condition String immutability doesn't matters. because here you are not re-modifying any predefined variable.
2. Immutability matters when you re-modifying the String variable which is already carrying some value.
Ex.
class TestStringConcatenation{
public static void main(String args[]){
String s1="Sachin ";
String s2="Tendulkar";
s1=s1.concat(s2);
System.out.println(s1);//Sachin Tendulkar
}
}

Output will be same.

But as we know String is immutable the s1 variable which was carrying value "Sachin" is now lost. It's carrying "Sachin Tendulkar". So the question is that how we are getting this output?
So, answer is very simple. String will not modify the value it simply lost its first reference and garbage collector removed that un-referenced object from memory.
if you want see this change, checkout the before and after concatenation for s1 by using hashCode() method.
Ex.
public class StringClassCheck {

public static void main(String[] args) {

String s1="Sachin ";
String s2="Tendulkar";
System.out.println("Before concatenation s1: "+s1.hashCode());
s1=s1.concat(s2);
System.out.println(s1);//Sachin Tendulkar
System.out.println("After concatenation s1: "+s1.hashCode());
}


}
I hope my answer satisfying.
Thank you!

Regards,
Iroti Mahajan
www.sevenmentor.com
Image Created0Down

By: [email protected] On: Fri Dec 08 14:29:38 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Image Created0Down

By: [email protected] On: Tue Jan 23 17:16:58 IST 2018 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No