Search this Java developer blog:

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


17 comments:

  1. good examples........

    ReplyDelete
  2. 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?

    ReplyDelete
    Replies
    1. 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 ...

      Delete
    2. Object s3 = new Object("Hello")
      it wont compile

      Delete
  3. 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.
    }
    }

    ReplyDelete
    Replies
    1. o1 is in String pool as both point to the same object

      Delete
  4. Thanks. Was so helpful.

    ReplyDelete
  5. I really like that you are providing information on core and advance java , being enrolled in http://www.wiziq.com/course/1779-core-and-advance-java-concepts, i was looking for such information online to assist me on core and advance java concepts and your post helped me a lot .thAnks

    ReplyDelete
  6. Very Useful information...
    Thanks

    ReplyDelete
  7. thank you so much..
    very useful info

    ReplyDelete
  8. Thak you
    it's more useful information......

    ReplyDelete
  9. thank u friend ,,...your site helped me to get the job.

    ReplyDelete
  10. Bravo.............!

    ReplyDelete
  11. Thanks for this wonderful site

    ReplyDelete
  12. Nicely presented.. easy to understand concepts.. :)

    ReplyDelete

5 recent Java developer posts