Java Programming - Java.lang Class - Discussion
Discussion Forum : Java.lang Class - Pointing out the correct statements (Q.No. 2)
2.
Which two statements are true about wrapper or String classes?
- If x and y refer to instances of different wrapper classes, then the fragment x.equals(y) will cause a compiler failure.
- If x and y refer to instances of different wrapper classes, then x == y can sometimes be true.
- If x and y are String references and if x.equals(y) is true, then x == y is true.
- If x, y, and z refer to instances of wrapper classes and x.equals(y) is true, and y.equals(z) is true, then z.equals(x) will always be true.
- If x and y are String references and x == y is true, then y.equals(x) will be true.
Answer: Option
Explanation:
Statement (4) describes an example of the equals() method behaving transitively. By the way, x, y,and z will all be the same type of wrapper. Statement (5) is true because x and y are referring to the same String object.
Statement (1) is incorrect—the fragment will compile. Statement (2) is incorrect because x == y means that the two reference variables are referring to the same object. Statement (3) will only be true if x and y refer to the same String. It is possible for x and y to refer to two different String objects with the same value.
Discussion:
3 comments Page 1 of 1.
Fed truzzo said:
3 years ago
@All.
Here is my coding part.
public class Test {
public static void main(String[] args)
{
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
}
}
Please rectify me, when I'm wrong.
Here is my coding part.
public class Test {
public static void main(String[] args)
{
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
}
}
Please rectify me, when I'm wrong.
Meethio said:
6 years ago
According to me, 3 and 5 are obviously correct.
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
String x="Hi";
String y="Hi";
if(x==y)
System.out.println("He2llo World");
if(x.equals(y))
System.out.println("He1llo World");
}
}
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
String x="Hi";
String y="Hi";
if(x==y)
System.out.println("He2llo World");
if(x.equals(y))
System.out.println("He1llo World");
}
}
Anand said:
9 years ago
I think 3, 4 and 5 are true.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers