Menu
Question Index
...

What will be the output of the following program?

public class Compare {
    public static void main(String[] args) {
        String str1 = "Merit Campus";
        String str2 = "" + "Merit Campus";
        String str3 = new String("Merit Campus");
        System.out.println(str1 + ", " + str2 + ", " + str3);
        if (str1 == str2)
            System.out.println("Equal 1");
        else
            System.out.println("Not Equal 1");
        if (str1 == str3)
            System.out.println("Equal 2");
        else
            System.out.println("Not Equal 2");
        if (str1.equals(str3))
            System.out.println("Equal 3");
        else
            System.out.println("Not Equal 3");
    }
}


Merit Campus, Merit Campus, Merit Campus
Equal 1
Not Equal 2
Equal 3
Merit Campus, Merit Campus, Merit Campus
Equal 1
Equal 2
Equal 3
Merit Campus, Merit Campus, Merit Campus
Equal 1
Equal 2
Not Equal 3
Merit Campus, Merit Campus, Merit Campus
Equal 1
Not Equal 2
Not Equal 3
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java equals method vs == Operator

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App