Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
String s = "java";
s.toUpperCase();
System.out.println(s);
}
}java
- Expecting
JAVA: aStringnever changes;toUpperCasereturns a new string, which is lost here because it is not assigned back tos. - Expecting a compile error: calling a method and ignoring its result is legal in Java, so the line runs and leaves
sas it was.
- How would you change the program so that it prints
JAVA? - Why did the designers of Java make
Stringimmutable?