I have a string:
String c = "IceCream";
If I use toUpperCase()
function then it returns the same string, but I want to get "ICECREAM"
.
Where is the problem?
The code
String c = "IceCream";
String upper = c.toUpperCase();
System.out.println(upper);
correctly prints "ICECREAM". However, the original string c isn't changed. Strings in Java are immutable so all operations on the string return a new copy.
Are you expecting the original variable, c
, to have been changed by toUpperCase()
? Strings are immutable; methods such as .toUpperCase()
return new strings, leaving the original un-modified:
String c = "IceCream";
String d = c.toUpperCase();
System.out.println(c); // prints IceCream
System.out.println(d); // prints ICECREAM
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With