Examveda
Examveda

What will be the output of the following program?
public class Test{
        public static void main(String args[]){
		String s1 = "java";
		String s2 = "java";
		System.out.println(s1.equals(s2));
		System.out.println(s1 == s2); 
        }
}

A. false true

B. false false

C. true false

D. true true

Answer: Option D


This Question Belongs to Java Program >> Strings

Join The Discussion

Comments ( 11 )

  1. Anmol Rath
    Anmol Rath :
    2 years ago

    In 5th line = java equals java //true
    in 6th line = java == java //true
    so the answer will be (D) true true

  2. Deepak Dattatray
    Deepak Dattatray :
    2 years ago

    as "java" is string literal and it will be stored in string literal pool .and when second "java" added it will be checked in string literal pool if present .and it is present hence will not be overwritten rather same pointer will be returned back to s2 as well .as we know == goes by reference equality and equals goes by content as both reference and content are same hence both true true

  3. Pooja Chaudhary
    Pooja Chaudhary :
    4 years ago

    in this program string is letral and while content is same so .equals method and == operator is give same output .And letral always present in string constant pool.in this program not create a new object.

  4. Sameer Nigam
    Sameer Nigam :
    5 years ago

    .equals and == are the same thing

  5. Allauddin Mulla
    Allauddin Mulla :
    6 years ago

    s.equals(s2) checks for values so both r same i.e. java and java
    hence it is true
    s1==s2 checks for references here we haven't used new keyword so both will go to constant pool but in constant pool duplicates are not allowed hence references of s1 and s2 will same
    hence answer is true true

  6. Manish Tiwari
    Manish Tiwari :
    7 years ago

    answer will be truetrue

  7. Mohammad Wasim
    Mohammad Wasim :
    7 years ago

    The answer will be true true.

  8. Sony Sonu
    Sony Sonu :
    7 years ago

    The option displayed is wrong.
    Correct answer is option d.

  9. K DINESH
    K DINESH :
    7 years ago

    Here we didn't use new to create string just say as String s1=" java"; like this only.whenever we say like this memory is created in constant pool.In constant pool duplicates are not allowed it means s1 holds java s2 also holds java so equals checks it has same content or not. And == checks it has same address or not .obviously it has same address .

  10. Prateek Ghosh
    Prateek Ghosh :
    7 years ago

    == compares the objects referencing to same literals or not that why true true is the anwer

  11. SaiDeepak Muthyala
    SaiDeepak Muthyala :
    7 years ago

    Why is the bottom statement true? is not the double equal sign a comparison of reference and not the characters of the string object?

Related Questions on Strings