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. | 0 |
By: [email protected] On: Wed Dec 06 12:00:09 IST 2017 0 0 0 | 0 |
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 | 0 |
By: [email protected] On: Wed Dec 06 12:01:48 IST 2017 0 0 0 | 0 |
Are You Satisfied :0Yes0No |
thank you! one more question, how many objects will be created here? String s3=s1.concat(s2); | 0 |
By: [email protected] On: Wed Dec 06 12:38:28 IST 2017 0 0 0 | 0 |
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 | 0 |
By: [email protected] On: Fri Dec 08 14:29:38 IST 2017 0 0 0 | 0 |
Are You Satisfied :0Yes0No |
0 | |
By: [email protected] On: Tue Jan 23 17:16:58 IST 2018 0 0 0 | 0 |
Are You Satisfied :0Yes0No |