Junior — Middle
Can you give an example of an object whose properties cannot be changed after creation?
sobes.tech AI
Answer from AI
An example of an object with immutable properties is the String class in Java. Once a String object is created, its contents cannot be changed.
This means that methods that seem to modify the string (for example, toUpperCase()) actually return a new String object, and the original remains unchanged.
Example:
String s = "hello";
String s2 = s.toUpperCase(); // s2 = "HELLO", s = "hello"
This behavior ensures safety and convenience when working with strings.