Impetus Interview questions: Code-2

What will be the output of the following...
class A{
public static void main(String [] arg){
        String s1="hello";
        String s2=new String("hello");
        System.out.println(s1==s2);
        String s3=new String("world");
        String s4=new String("world");
       
        System.out.println(s3==s4);
        System.out.println(s1.equals(s2));
        System.out.println(s3.equals(s4));
        String s5="hello";
        System.out.println(s1==s5);
    }

}

Output:  See here.....
false
false
true
true
true

s1 == s2 : false, both refernece variable pointing to different objects
s3 == s4 : false, both refernece variable pointing to different objects
s1.equals(s2): true:  Different objects but values are same
s3.equals(s4): true: Different objects but values are same

 s1 == s5 : true, both refernece variable pointing to same object

Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Upload file on Google cloud storage using Java servlet on google app engine

Session timeout interceptor in struts 2