Why does the string s print "Hello, World" after converting to lower case?
public class Practice {
public static void main(String[] args){
String s = "Hello, World";
s.toLowerCase();
System.out.println(s);
}
}
Strings are special kind of objects in Java. Java developers deliberately created them to be immutable (for various security and performance reasons).
That means that when you think you change the state of a String object, actually a new String object is created (and the previous is not really changed).
Therefore, to make your code work, you have to assign the result of the method to some string:
s = s.toLowerCase();