DEVFYI - Developer Resource - FYI

Can one create a method which gets a String and modifies it?

Java Interview Questions and Answers (part 1)


(Continued from previous question...)

139. Can one create a method which gets a String and modifies it?

No. In Java, Strings are constant or immutable; their values cannot be changed after they are created, but they can be shared. Once you change a string, you actually create a new object. For example:
String s = "abc"; //create a new String object representing "abc"
s = s.toUpperCase(); //create another object representing "ABC"

(Continued on next question...)

Other Interview Questions