• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Boolean

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code can some one explain why b3.equals(b4) ?
import java.awt.*;
import java.lang.Math.*;
class Math_func extends Object {
public static void main( String args[]) {
Boolean b1 = new Boolean("TRUE");
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("JUNK");
Boolean b4 = new Boolean("SOME");
Boolean b5 = new Boolean("TrUe ");
if( b1.equals(b2) )
System.out.println ( "hey b1 equals b2");
if( b1.equals(b5) )
System.out.println ( "hey b1 equals b5");
if( b1 == b2 )
System.out.println ( "hey b1 == b2");
if( b3.equals(b4) )
System.out.println ( "hey b3 equals b4");
if( b3 == b4 )
System.out.println (" hey b3 == b4");
System.out.println("" + b1 + b2 + b3);
}
}
Thanks,
SriniRenga
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Boolean class overrides the equals method and it will return true if an argument is not null.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Val Dra - where in the world did you come up with that?
The result is true because both Boolean objects have the value false, thus they are identical.
Here is the actual equals method from java.lang.Boolean:
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof Boolean)) {
return value == ((Boolean)obj).booleanValue();
}
return false;
}
As you can see, the result will be false if the obj is null or not a Boolean - next the contained values are compared.
Boolean objects constructed with String values other than "true"
(converted to lower case) always have the value "false"
Bill

------------------
author of:
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
layback a little there. I read the description so teh first part of what i said was true. It does check to see if the value is not null. Other part is obviouse because it deals only with booleans and nothing else. If he tries later to convert it to pmitive value he get's an exception. I didn't post a good message sorry.
 
Sri Enga
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to William Brogden and Val Dra. I got it.
SriniRenga
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
I couldn't find a smilies to represent a good chortle.
Val, don't take it personally. Bill didn't mean any harm with his question. Take it in good stride. If you're talking about your statement, "Because Boolean class overrides the equals method" being true, then you are correct. The rest of your statement is incorrect.
-Peter
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic