Sunday 9 June 2019

java - Why String concat(String) method is acting differently?

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...