Jun 26, 2012

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



Anonymous Anonymous said...

good examples........

3:52 PM, August 11, 2012  
Blogger Raghu132 said...

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?

9:37 PM, August 27, 2012  
Blogger Arulkumaran Kumaraswamipillai said...

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.

10:30 AM, August 28, 2012  
Anonymous Anonymous said...

Thanks. Was so helpful.

3:32 PM, September 04, 2012  
Blogger Krishna Kumar said...

o1 is in String pool as both point to the same object

12:31 AM, September 21, 2012  
Anonymous Kelvin said...

thanx nic example

5:19 PM, September 23, 2012  
Blogger sarabjeet said...

I really like that you are providing information on core and advance java , being enrolled in, i was looking for such information online to assist me on core and advance java concepts and your post helped me a lot .thAnks

9:25 PM, September 28, 2012  
Blogger Dipanshu said...

Very Useful information...

5:22 AM, November 01, 2012  
Blogger Saidivya Kande said...

thank you so much..
very useful info

5:46 AM, December 01, 2012  
Blogger arpitha BM said...

very useful...

9:24 PM, March 02, 2013  
Blogger Naresh Goud said...

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

2:57 AM, March 08, 2013  
Blogger Varun Mittal said...

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 ...

5:59 AM, September 14, 2013  
Blogger naresh sanapala said...

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

11:36 PM, September 16, 2013  
Anonymous Anonymous said...


7:21 PM, October 27, 2013  
Anonymous Anonymous said...

Object s3 = new Object("Hello")
it wont compile

3:17 AM, November 13, 2013  
Anonymous Anonymous said...

Thanks for this wonderful site

3:50 PM, November 14, 2013  
Anonymous Anonymous said...

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

7:53 PM, April 24, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home