This week's book giveaway is in the
General Computing
forum.
We're giving away four copies of
Arduino in Action
and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See
this thread
for details.
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Certification
»
Programmer Certification (SCJP/OCPJP)
Author
String Equals
Pinki Roy
Greenhorn
Joined: May 16, 2008
Posts: 20
posted
Feb 19, 2012 13:55:39
0
Hi All,
class StringEquals { public static void main(String[] args) { Boolean b = new Boolean(true); String str = new String(""+b); StringBuffer sb = new StringBuffer(str); System.out.println(b.equals(str)+"," +str.equals(sb)+","+sb.equals(b)); } }
In the above code I fail to understand why
str.equals(sb)
is displaying false.
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
Feb 19, 2012 14:27:27
0
Pinki Roy wrote:
In the above code I fail to understand why
str.equals(sb)
is displaying false.
Because str refers to a
String
object and sb refers to a
StringBuffer
object and a String is never equal to a
StringBuffer
Joanne
Zeeshan Sheikh
Ranch Hand
Joined: Nov 20, 2011
Posts: 143
I like...
posted
Feb 19, 2012 14:48:47
0
It will only return
true
if the given object represents a
String
equivalent to this string otherwise its going to return
false
So if you want the value to be true you need to override toString () method.
str.equals(sb.toString())
StringBuffer
is like a String, but can be modified (mutable) while String object can not be modified (immutable).
Hope this helps.
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: String Equals
Similar Threads
Please help and explain the output of following code.
Why it prints like this here?
Equal method
StringBuffer and String with equal method
StringBuffer
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter