When we try to create a String Object JVM first checks the String constant pool,if string doesn’t exist in the pool then it will creates a new String Object isaq
and reference maintained in the pool.The Variable S1 also refer the same String Object.
String s1="isaq";
Above statement create one String Object in String pool.
Now you are doing the
String s2="isaq";
Above statement won’t create any String Object in String pool and S2 refers the same object as S1.Because JVM will check the Pool and it found String object is already present.
To Validate it you can compare String reference using equality operator(==)
. to check whether they are referring the same String Object or not.
solved String object creations in java