+ 1

Java String + operator

so i have the below code as an example and want to know what exactly happens. so i have 3 strings: 2 initialized with the same length and one with the total length. if i say c = a + b what exactly happens? is there a new string object created by the + operator or are the 2 values just copied? https://code.sololearn.com/c1giuHJRv1P7/?ref=app

20th Oct 2017, 4:23 PM
Niklas
Niklas - avatar
5 Answers
+ 3
you are concatenating the strings stored in a and b and then assigning it to c. and for the next question you are overwriting if c already consists of a string
21st Oct 2017, 3:33 AM
Talluri Saisumanth
Talluri Saisumanth - avatar
+ 2
This creates a new string šŸ˜‰ String c,b,a = "test1"; b = "test2"; c = new String (a + b); System.out.println(c);
20th Oct 2017, 6:01 PM
D_Stark
D_Stark - avatar
+ 1
your just assigning c to use the values of a and b its called string concatenation
20th Oct 2017, 5:25 PM
D_Stark
D_Stark - avatar
0
yea but internal is it creating a new string or not?
20th Oct 2017, 5:26 PM
Niklas
Niklas - avatar
0
does it create a new string if the c already has a value and im just overwriting it?
20th Oct 2017, 5:33 PM
Niklas
Niklas - avatar