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
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
Comments
Post a Comment