dear friends, If you declare two string values in single program like
class StringDemo { public static void main(String args[])
{ String s=new String("hello"); s=new String("world"); System.out.println(s); }}
I am getting output: "world".what is the reason why am not getting the string value "happy".please help me am new to java
String s=new String("hello"); when the above statement is executed a String object is created and s is pointing to that object.
s=new String("world"); Now a new String object is created whose value is "world" and s is now pointing to the new object whose value is "world" .
So,now no reference is pointing to "hello" object but the object is not removed. And when u say System.out.println(s), it indirectly mean as System.out.println(s.toString());