Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of the following program?
public class PabloPicasso {
    public static void main(String[] args) {
        String name = new String("Pablo Picasso");
        String nameObj = name;
        System.out.println("name and nameObj are same " + name == nameObj);

A. name and nameObj are same false
B. name and nameObj are same true
C. Some other output 
D. Compilation Error or Runtime Error 
Topic: Java equals method vs == Operator

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer : B

Posted by Deepak Kumar Yadav    2014-06-26 04:14:35

C .only false gets printed .

Posted by Shabaaz Shaikz    2014-06-26 04:44:48


Posted by Rishav Seksaria    2014-06-26 04:47:10


Posted by Htwe Ei Hlaing    2014-06-26 04:47:24

B.because the reference of name assigned to nameObj so name and nameObj contains same hashcode it returns true

Posted by Rajashekar Chelpuri    2014-06-26 05:08:26

B bcoz it refers to same string stream and there fore the values in this stream are identical

Posted by Shri Nandhitha    2014-06-26 05:10:24


Posted by Shashi Kumar    2014-06-26 16:57:05

Congratulations Shabaaz Shaikz. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-27 04:21:29

As you know according to the precedence table the operator + has higher precedence compared to ==.

Posted by Merit Campus    2014-06-27 15:23:29

So first 'name and nameObj are same ' is concatenated with 'Pablo Picasso' and then compared with 'Pablo Picasso'. This will return false and so only 'false' is printed.

Posted by Merit Campus    2014-06-27 15:25:25

If we enclosed name == nameObj in brackets like '(name == nameObj)' then the output will be B.

Posted by Merit Campus    2014-06-27 15:26:17

Hope this clarifies. :-)

Posted by Merit Campus    2014-06-27 15:26:26

© meritcampus 2019

All Rights Reserved.

Open In App