Menu
Topics Index
...
`

Relational Operators and ifelseif ladder


If you need explanation Read this topic

If you need Answer Take test on this topic

Relational Operators and ifelseif ladder
What will be the output of the following program?
public class EqualOperators
{
    public static void main(String args[])
    {
        String str1 = "MyName";
        String str2 = "MyName";
        if (str1 == str2)
        {
            System.out.println("Objects are equal");
        }
        else
        {
            System.out.println("Objects are not equal");
        }
        if (!str1.equals(str2))
        {
            System.out.println("Objects are not equal");
        }
        else
        {
            System.out.println("Objects are equal");
        }
    }
}

A. Objects are not equal
Objects are equal
B. Objects are equal
Objects are equal
C. Objects are not equal
Objects are not equal
D. Objects are equal
Objects are not equal
E. 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.

Option is a becoz == checks references and equals method checks quantity

Posted by Vijay Kumar    2014-07-29 03:40:44


Ans is B..as str1 is equal to str2 it prints objects are equal and after that second if condition evaluates to false which prints objects are equal

Posted by Badam Swathi    2014-07-29 03:41:27


A

Posted by Arul Palanisamy    2014-07-29 03:43:20


b. is the correct ans.
because ==operator compares reference of string obj. but equal() method compares the contents of object..
so in both case objects are equal prints..

Posted by Shubham Bansal    2014-07-29 04:20:34


Ans is d.. Bcoz first tym it will check d strings.. Second if loop will be!(0)=1 if(1) means if true.. So objects r not equal will print.

Posted by Neelima Nadikuda    2014-07-29 04:37:09


B bcz both strings str1 &str2 are equal

Posted by Samyuktha Reddy    2014-07-29 05:13:43


b

Posted by Zeg Abder    2014-07-29 07:50:29


A ,bcz both strings are created in string pool,so when content is same new object is never created ,first it checks in pool,since same content is avialable ,reference is pointed to the existing one..so in first if condition is equal since references are same and == checks for references ..in second comdition as usual content is same equal() checks for content ,it is also same

Posted by Prabhakar Reddy    2014-07-29 08:34:46


two different objects are  created for stringsonly when new keywor is used

Posted by Prabhakar Reddy    2014-07-29 08:35:59


A
== checks reference equality
.equals() checks content equality
first condition is false since str1 and str2 are different objects
second condition is ture and its negation will be false
in both cases else part will be executed

Posted by Tushar Ghude    2014-07-29 14:48:53


option a

Posted by Vino Krishnan    2014-07-29 15:15:42


== checks the reference object of the variable and equals() checks the letters of the string

Posted by Vino Krishnan    2014-07-29 15:17:17


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

Posted by Merit Campus    2014-07-30 04:21:36


© meritcampus 2019

All Rights Reserved.

Open In App