Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/comparing-strings-for-equality-in-java
Question 100 / 297:  What is the result of compiling and running the following program?
public class Tester {

public static void main(String[] args) {

String a = "javachamp";

String b = "javachamp";

String c = new String("javachamp");

System.out.print(a==b);

System.out.print(a==c);

System.out.print(b.equals(c));

System.out.print(b.equals(a));

}

}

A  Compilation error
B  falsefalsetruetrue
C  truetruetruetrue
D  truefalsetruetrue
<< First < Previous Next > Last >>
Explanation:

== checks if two references are referring to the same object (physical memory location)

equals() checks if the two objects have the same value

a and b both refer to the same string literal, which was created in a special memory area called "String constant pool". Therefor a==b and b.equals(a) return true.

While c is a reference to an object created in the heap by the virtue of using the keyword "new". Th object is created in the heap with a new memory location, therefor == fails to succeed when comparing a==c or b==c.

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Vanessa Soledad
Start Exam
Stephanie Redfern
Start Quiz
Stephanie Redfern
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>