I please check the code below.
I can understand that String s1 is not assigned and so even though concat(string) method is used it is giving the original output.
But also, in case of String s2 no variable is assigned but concatenation worked.
Can someone please explain?
package com.stringconcat.main;
public class StringConcat {
public static void main(String[] args) {
String s1 = "Hello";
s1.concat(" World");
System.out.println("String s1 output: " + s1);
String s2 = "Hello" /*s1*/;
System.out.println("String s2 output: " + s2.concat(" World"));
}
}
The outputs are:
String s1 output: Hello
String s2 output: Hello World
No comments:
Post a Comment