This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

equals()

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class C
{
public static void main(String[] args)
{
Boolean b1 = Boolean.valueOf(true);//line 1
Boolean b2 = Boolean.valueOf(true);//line 2
Boolean b3 = Boolean.valueOf("TrUe");
Boolean b4 = Boolean.valueOf("tRuE");

System.out.print((b1==b2) + ",");
System.out.print((b1.booleanValue()==b2.booleanValue()) + ",");
System.out.println(b3.equals(b4));

Boolean f= new Boolean("TRUE");
Boolean h =new Boolean("true");
System.out.println(f.equals(h));//line 3

String s = new String("true");
String s1= new String("TRUE");
System.out.println(s.equals(s1));//line 4
}
}

output of this program is
true true true
true
flase

My question is how line 1 and line 2 are correct.
The valueOf() takes string as an argument.The line 1 and line 2 is not taking string as an argument so it should throw NumberFormatException.
Why this is not happening.

Then coming to line 3.I though the equals() is case-sensitive but it is not true in line 3 but it is true in line 4.

Can anybody clear all my doubts.
Thanks in advance.
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boolean has two methods
valueOf(boolean b)
valueOf(String s)

Check the API.
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David that helps...But what about my second part of question ...

Boolean f= new Boolean("TRUE");
Boolean h =new Boolean("true");
System.out.println(f.equals(h));//line 3

String s = new String("true");
String s1= new String("TRUE");
System.out.println(s.equals(s1));//line 4
Then coming to line 3.I though the equals() is case-sensitive but it is not true in line 3 but it is true in line 4.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 1.5 API for Boolean class specifies that :


Boolean(String s)
Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true".



And the API for String class specifies that there are two equals() functions available for comparing strings.
1. equals(Object o) -> compares the strings taking case into consideration
2. equalsIgnoreCase(String anotherString) -> compares the strings ignoring the case.

If you want to compare the contents of two strings, ignoring the case, you should use equalsIgnoreCase() function.

P.S -> I see that you are maintaining the code-output-question format while posting. keep it up. Put your properly indented code, within blocks, and the code will appear better.
 
reply
    Bookmark Topic Watch Topic
  • New Topic