• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Boolean

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class BoolTest
{
public static void main(String[] args)
{
Boolean b = new Boolean("TRue");
Boolean c = new Boolean("NULL");
boolean d = true;
Boolean e =(b || c)&& d;
System.out.println(e);
}
}
Answer is true.

How can Null be given to Boolean.even if give null to boolean,how does the OR operation works.please explain the output
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boolean c = new Boolean("NULL");

the above code takes "NULL" as a String.
here you are not assigned a null (c = null)to c object reference.

if you pass any value , other than true,it will be taken as false.
 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class BoolTest
{
public static void main(String[] args)
{
Boolean b = new Boolean("TRue");
Boolean c = new Boolean(null);
boolean d = true;
System.out.println(b);
System.out.println(c);
Boolean e =(b || c)&& d;
System.out.println(e);
}
}

How is it possible.if we give "NULL",then it takes it as c=false and the output is true.but if we give it as null,how it is possible??.please explain.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pandoo pandoo:
public class BoolTest
{
public static void main(String[] args)
{
Boolean b = new Boolean("TRue");
Boolean c = new Boolean("NULL");
boolean d = true;
Boolean e =(b || c)&& d;
System.out.println(e);
}
}
Answer is true.

How can Null be given to Boolean.even if give null to boolean,how does the OR operation works.please explain the output




hi pandoo,

class Boolean has two constructors one yhat takes boolean primitive (true or false) and second that takes CASE-INSENCITIVE String ("TruE" or "FalsE").any String instead of "true" or "false" is treated as false.
see the code modified and you will understand.

class BoolTest{
public static void main(String[] args){
Boolean b = new Boolean("TRue");
Boolean c = new Boolean("NULL");
boolean d = true;
boolean e = (b.booleanValue() || c.booleanValue()) && d;
System.out.println(e); //true
System.out.println(b.booleanValue()); // true
System.out.println(c.booleanValue()); // false
}
}

please let me know if still confused...



regards
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What pandoo is talking about in the second case is: why do you still get true when you pass null instead of "NULL"?

The reason is because the constructor for Boolean will take a String, but if you pass null all it gets is a null String reference. What does it do with ANY String that isn't "true"? It makes it false. Therefore, passing any thing but "true" as a String will get you false, including when the String doesn't exist.
 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew.my doubt is cleared.Thanks group
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Matthew Alesi:
What pandoo is talking about in the second case is: why do you still get true when you pass null instead of "NULL"?

The reason is because the constructor for Boolean will take a String, but if you pass null all it gets is a null String reference. What does it do with ANY String that isn't "true"? It makes it false. Therefore, passing any thing but "true" as a String will get you false, including when the String doesn't exist.



Mr. Matthew,

sorry to say but you are wrong and very much confused ,what i said is very right . here String that is passed is "NULL" which is not a null in this case but a simple String object with value "NULL".And as i said previously any String instead of "true" or "false" is treated as false. so the value wrapped in Boolean object is false.

so the expression i wrote evaliates like this.

b.booleanValue() => true //as value passed is boolean primitive (true) that is true.
c.booleanValue() => false//as value passed is not a boolean primitive (NULL) that results false wrapped in object referenced by c.

and finally come to the expression ::

( true || false) && true

which evaluates to be true......

and hence the result true.

Again the expression given by pandoo pandoo (“Boolean e =(b || c)&& d;” is wrong .
As how can you perform logical operations on Boolean objects.I also removed that bug in my code given.


at last i want to say don't confuse. NULL is not null.

hope you understand.if any confusion please let me know...


Regards...
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THis means any method on Boolean does not throw any runtime exception unlike other wrappers.
Like Integer it = new Integer("12s");
it.intValue();//throws NumberFormatException
Boolean boo = new Boolean("FaLsE");
boo.booleanValue();//returns false
Boolean boo = new Boolean("TRue");
boo.booleanValue();//returns true
Boolean boo = new Boolean("JavaRanch");
boo.booleanValue();//returns false
even this also works.
Boolean boo = new Boolean(null);
boo.booleanValue();//returns false

Thats great.. .. worth reading this thread.
 
Seriously? That's what you're going with? I prefer this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic