Examveda
Examveda

What will be the output of the following program code?
public class Test{
      public static void main(String args[]){
            String s = "what";
            StringBuffer sb = new StringBuffer("what");
            System.out.print(sb.equals(s)+","+s.equals(sb));
      }
}

A. true,true

B. false,true

C. true,false

D. false,false

E. None of these

Answer: Option D


This Question Belongs to Java Program >> Strings

Join The Discussion

Comments ( 9 )

  1. Priyanka Kumari
    Priyanka Kumari :
    3 years ago

    String we can't create new object of existing object , if we want to create object with existing object . New object will be created along with changed, but
    In string buffer can do changed in existing object .

  2. Tuhin Das
    Tuhin Das :
    4 years ago

    public class Test{
    public static void main(String args[]){
    String s = "what";
    StringBuffer sb = new StringBuffer("what");
    ** System.out.print(s.equals(""+sb)+","+s.equals(""+sb)); // true,true(because this time equals method work and check both values are equal or not because you see String concatenation are happen)
    **System.out.print(s.equals(sb)+","+s.equals(sb)); // false,false(because this time equals method work difference reference )
    }
    }

    O/P- true,true
    O/P- false,false

  3. Swathi Pinky
    Swathi Pinky :
    4 years ago

    in my opinion, the answer should be true, true
    because-String s = "what"; will create the object of s in constant pool of the heap segment.
    And StringBuffer sb = new StringBuffer("what"); will create it's object in non constant pool of the heap segment.
    now the equals method will compare the string present in both s and sb as they both are same so, i think both will give rise to true,true

  4. Abisha Benjamin
    Abisha Benjamin :
    5 years ago

    String and StringBuffer cannot be compared.

  5. Er.vineet Khandelwal
    Er.vineet Khandelwal :
    7 years ago

    I think answer should be false,true
    Because sb is object of StringBuffer class and for StringBuffer class equals() method will not be overridden n here equals method of Object class will be called and that equals() method check reference not content and here reference of both object is different so equals() methos will return false but
    For s.equals(sb)... here s is object of String class and string class equals() method check for content and here content is same so here euals() method will return true.

  6. Lipun Patel
    Lipun Patel :
    7 years ago

    in my opinion, the answer should be true, true
    because-String s = "what"; will create the object of s in constant pool of the heap segment.
    And StringBuffer sb = new StringBuffer("what"); will create it's object in non constant pool of the heap segment.

    now the equals method will compare the string present in both s and sb as they both are same so, i think both will give rise to true,true.

  7. Pravat Kumar
    Pravat Kumar :
    7 years ago

    Hi Priyanka ,

    First one is False because String Buffer equals() method for physical address comparison .So output is false.

    In Second s.equals(sb) ----s is comparing with String buffer object not with String object. toString() method is there in String Buffer class to convert String Buffer to String. So we are getting false here.
    If you will change above line to s.equals(sb.toString()) , I hope you will get the desire output of String class equals() method.

    Hope you are clear now.If you have doubt mail me:mailtopravatkumar@gmail.com

  8. Swapnil G.
    Swapnil G. :
    7 years ago

    Priyanka Lokhande There is difference between
    String and StringBuffer
    both are different so
    output is different.


    Difference between String and StringBuffer :
    1) String class is immutable. StringBuffer class is mutable.
    2) String is slow and consumes more memory when you concat too many strings because every time it creates new instance. StringBuffer is fast and consumes less memory when you cancat strings.
    3) String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. StringBuffer class doesn't override the equals() method of Object class.

  9. Priyanka Lokhande
    Priyanka Lokhande :
    7 years ago

    I am not getting the answer of this question

Related Questions on Strings