This is one of the common question to be asked in interview to judge your basic level of knowledge. We all know that String is immutable which means that once object created cannot be modified or if try to modify, it will create another String literal in the String pool. So this proves that String is immutable, but this is we are proving theoretically. Now if interviewer ask you to prove the same by writing programs then you will be doing like this,
class A{
public static void main(String[] args){
String s="java"; // Create a object in String pool.
s = s + "java" ; // Create another object with "javajava" as
String literal in the String pool
}
Awesome!!!!!!!!!!
ReplyDelete