Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a simple quiz on string equals

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple quiz:
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile that code, the compiler complains:
Test.java:5: incomparable types: java.lang.String and java.lang.StringBuffer
System.out.println(s == sb);
1 error
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Now, comment out the ones that don't compile and carry on.
Thank you Valentin. You probably didn't know that I made it that way intentionally.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably right, I probably didn't know
Keep up the good work Marlene
[ May 11, 2003: Message edited by: Valentin Crettaz ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also know that you, Valentin, know the answers without compiling and running the code.
 
Abhilash Vasudevan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
s==sb will not compile because they are of different Class.
My only concern was why sb.equals(sb2) was returning false when both the objects are made with the same string.
A small peep into the API showed that StringBuffer doesn't override equals() and so it is returning false.
To check equals() between two StringBuffer Objects we may say sb.toString().equlas(sb2.toString())
Regards
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good for you abhilash_iv. You found the two that I stumbled on - the reason I made the test.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhilash_iv,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Abhilash Vasudevan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you friends, is my display name now ok?
bye
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhilash,
Your displayed name is fine. Thank you for complying with our naming policy
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I Think this discusion is going far away from it's ans !!!
can any one kindly tell y the o/p is false false true and false if error line is commented ??
cheers
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s.equals(sb): false
false because a String object can never be equal to a StringBuffer object, even if the StringBuffer contains the same character sequence as the String.
sb.equals(s): false
Same as above
s.equals(s2): true
String objects containing the same character sequence are considered equal.
sb.equals(sb2): false
The equals method is not overridden in the StringBuffer class, that is, an == comparison is performed. Thus, for this method invocation to return true, the object reference variables sb and sb2 must be referring to the same StringBuffer objetc on the heap, which is not the case here.
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a bunch
Valentin
cheers
 
Bruno Arantes
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The contentEquals(StringBuffer sb) method in String class compares a String object with a StringBuffer. Below is the javadoc for this method:
public boolean contentEquals(StringBuffer sb)
Returns true if and only if this String represents the same sequence of characters as the specified StringBuffer.
Parameters:
sb - the StringBuffer to compare to.
Returns:
true if and only if this String represents the same sequence of characters as the specified StringBuffer, otherwise false.
Throws:
NullPointerException - if sb is null.
Since:
1.4
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Valentin and Bruno.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic