• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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.
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic