Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

public class Demo03
{
    void run()
    {
        String city = "delhi";
        String typed = new String("delhi");
        boolean sameObject = (city == typed);
        System.out.println(sameObject + " " + city.equalsIgnoreCase(typed));
    }

    public static void main(String[] args)
    {
        new Demo03().run();
    }
}

  • A.The program prints false false
  • B.The program prints true true
  • C.The program prints false true
  • D.The program prints true false

Answer: C

new String is required to create a distinct object, so the two variables reference different objects and the identity test is false. equalsIgnoreCase compares characters, not references, so it is true. This is why identity comparison of strings is a bug rather than a shortcut.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.