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));
}
}
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
Join The Discussion
Comments ( 9 )
Related Questions on Strings
In Java, which class is used to represent a sequence of characters as a string?
A. String
B. StringBuilder
C. StringSequence
D. StringArray
What is the correct way to create a new empty String object in Java?
A. String emptyString = "";
B. String emptyString = new String();
C. String emptyString = " ";
D. String emptyString = null;
E. Both A and B
Which of the following methods is used to compare two strings for equality in Java?
A. equals()
B. compareTo()
C. equalsIgnoreCase()
D. compare()
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 .
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
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
String and StringBuffer cannot be compared.
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.
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.
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
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.
I am not getting the answer of this question