Hi,
We can get below type of questions in interview for java strings.
public class StringExample {
public static void main(String a[]) throws Exception {
String s11 = "ABC";
String s111 = "ABC";
String s234 = new String("ABC");
System.out.println(s11.equals(s111));
System.out.println(s11==s111);
//below line prints false as new object will created for s234(hashcode will different for two objects)
System.out.println(s11==s234);
//below line prints true as it will check for string value(ABC) not for object
System.out.println(s11.equals(s234));
}
}
What is the out put:
correct answer is
true
true
false
true
We can get below type of questions in interview for java strings.
public class StringExample {
public static void main(String a[]) throws Exception {
String s11 = "ABC";
String s111 = "ABC";
String s234 = new String("ABC");
System.out.println(s11.equals(s111));
System.out.println(s11==s111);
//below line prints false as new object will created for s234(hashcode will different for two objects)
System.out.println(s11==s234);
//below line prints true as it will check for string value(ABC) not for object
System.out.println(s11.equals(s234));
}
}
What is the out put:
correct answer is
true
true
false
true