Core Java coding questions frequently asked in written tests and interviews - part 2: equals Vs ==
Core Java Coding Questions and Answers for beginner to intermediate level
Q1 | Q2 | Q3 | Q4 | Q5 - Q8 | Q9 | Q10 | Q11 | Q12 - Q14 | Q15 |
Q2. What will be the output of the following code snippet?
Object s1 = new String("Hello"); Object s2 = new String("Hello"); if(s1 == s2) { System.out.println("s1 and s2 are =="); }else if (s1.equals(s2)) { System.out.println("s1 and s2 are equals()"); }A2. The answer is:
s1 and s2 are equals()
Here is the explanation with a diagram.
So, the above question tests your understanding of "==" versus "equals( )" applied to objects in Java. One compares the references and the other compares the actual values.
Here are some follow on questions:
Q. What will be the output for the following code snippet?
Object s1 = "Hello"; Object s2 = "Hello"; if (s1 == s2) { System.out.println("s1 and s2 are =="); } else if (s1.equals(s2)) { System.out.println("s1 and s2 are equals()"); }A. The answer is
s1 and s2 are ==
Now the above answer violates the rule we saw before. This is a special case (or rule) with the String objects and a typical example of the flyweight design pattern in action. This design pattern is used to conserve memory by reducing the number of objects created. The String object creates a pool of string and reuse an existing String object from the pool if the values are same instead of creating a new object. The flyweight pattern is all about object reuse. This rule applies only when you declare the String object as Object s = "Hello"; without using the keyword "new".
-->
This is a very popular Java interview question.
More Java coding questions and answers
- Core Java coding questions frequently asked in written tests and interview - part 3
- Reviewing a given Java code at job interviews and code review sessions
Labels: Core Java
16 Comments:
good examples........
really useful info. thank you.
Can u please clarify
1. if i create Object s3 = new Object("Hello");
where this Hello object will be placed in Heap memory or in String pool or in both?
2. If so, how about Object s1 = "Hello";
It will be placed in only in String pool or in both?
Try this.
public class Temp {
public static void main(String[] args) {
Object o1 = "Hello";
Object o2 = "Hello";
Object o3 = new String("Hello");
Object o4 = new String("Hello");
if(o1 == o2) {
System.out.println("o1 is in String pool as both point to the same object");
}
if(o3 == o4) {
System.out.println("o3 is in String pool as both point to the same object");
}
//If it does not print, then the objects are created in the heap.
}
}
Thanks. Was so helpful.
o1 is in String pool as both point to the same object
thanx nic example
Very Useful information...
Thanks
thank you so much..
very useful info
very useful...
Thak you
it's more useful information......
1. the Hello will be placed in heap as well as in string constant pool both.
so whenever we create a string object by using new operator then always 2 objects get created...
anything between " " is stored in string constant pool.
2. the answer of 2nd is Hello get stored in string constant pool.
because in this way only one object is creating ...
thank u friend ,,...your site helped me to get the job.
Bravo.............!
Object s3 = new Object("Hello")
it wont compile
Thanks for this wonderful site
Nicely presented.. easy to understand concepts.. :)
Post a Comment
Subscribe to Post Comments [Atom]
<< Home