Java string not converting to lowercase

Why does the string s

print "Hello, World"

after converting to lowercase?

public class Practice {
    public static void main(String[] args){
        String s = "Hello, World";
        s.toLowerCase();
        System.out.println(s);
    }
}

      

+3


source to share


2 answers


String

are immutable. You need to assign the result to a String#toLowerCase

variable:



s = s.toLowerCase();

      

+8


source


Strings are a special kind of objects in Java. The Java developers deliberately created them to be immutable (for various security and performance reasons).

This means that when you think you are changing the state of a String object, a new String object is actually being created (and the previous one is not changed).



Therefore, to make your code work, you need to assign the result of the method to some line:

s = s.toLowerCase();

      

+2


source







All Articles