It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a simple quiz on string equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a simple quiz on string equals" Watch "a simple quiz on string equals" New topic
Author

a simple quiz on string equals

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
A simple quiz:
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 05, 2003
Posts: 1391
I also know that you, Valentin, know the answers without compiling and running the code.
Abhilash Vasudevan
Greenhorn

Joined: Mar 31, 2003
Posts: 19
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

Joined: Mar 05, 2003
Posts: 1391
Good for you abhilash_iv. You found the two that I stumbled on - the reason I made the test.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 31, 2003
Posts: 19
Thank you friends, is my display name now ok?
bye
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Abhilash,
Your displayed name is fine. Thank you for complying with our naming policy
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 639
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


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Feb 10, 2001
Posts: 639
Thanx a bunch
Valentin
cheers
Bruno Arantes
Greenhorn

Joined: Apr 30, 2003
Posts: 24
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


Bruno Arantes Bueno.<br />Certified Lotus Specialist, SCJP 1.4.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Valentin and Bruno.
 
 
subject: a simple quiz on string equals