Menu
Topics Index
...
`

String Literal2861


If you need explanation Read this topic

If you need Answer Take test on this topic

String Literal2861
What will be the output of the following program?
public class StringDemo {
    public static void main(String[] args) {
        String str1 = "Merit";
        String str2 = "Merit";
        String str3 = new String("Merit");
        if (str1 == str2) {
            System.out.println("str1 and str2 are equal");
        }
        if (str1 == str3) {
            System.out.println("str1 and str3 are equal");
        }
        if (str2 == str3) {
            System.out.println("str2 and str3 are equal");
        }
    }
}

A. str1 and str2 are equal
str1 and str3 are equal
str2 and str3 are equal
B. str2 and str3 are equal
C. str1 and str2 are equal
D. Compilation Error or Runtime Error
Topic: Literals In Java

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.

ans is c,,, bcoz new is keyword wwhich create a new object of string class, which is not comptable with == ,.

Posted by Raju VarshnEy    2014-11-04 08:38:42


Ans is C.
As we know that String have a special feature called String Constant Pool(SCP), if we assign string literals to the String references then the references point to the object which is created in SCP. And str1 and str2 are assigned with "Merit" literal which is located in SCP and both references are pointing to same object. Where as str3 is assigned to a new Object which is created using 'new' keyword which intends to create object in heap... so str1 and str2 will be pointing to the same object present in SCP and str3 will be pointing to the object present in heap.. And '==' will do reference comparison.. As str1 and str2 pointing to same object the first if condition evaluates and prints "str1 and str2 are equal". Other if conditions will fail.

Posted by Gopinath Manchikanti    2014-11-04 08:41:42


A

Posted by Himani Mittal    2014-11-04 11:02:42


c

Posted by Zeg Abder    2014-11-04 11:44:39


Ans is C...here we create 3 Strings...1st two Strings we create with literal so that those will store in the String pool and third (str3) wil store in the heap Bcz we create Object with new...

JVM wont allow the same content in String pool...Example here we create "Merit" which will store in String Pool again we are creating same thing Before allocation JVM wil check whether is there any Object in String pool with these content if it is have than JVM wont allocate new memory block...it wil assign the new reference to already existing object...here str1 and str2 both will point the sam object in Stringpool...str3 will store in the Heap...

here we are checking with == which wil check whether these 2 reference pointing same objet or not....here s1 and s2 are pointing same Object so that 1st if condition wil become true..so it wil print str1 and str2 are equal..

Posted by Uday Kumar    2014-11-04 13:41:23


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-05 01:26:00


© meritcampus 2019

All Rights Reserved.

Open In App