Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean type from 0 length string  RSS feed

 
Ranch Hand
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
I wander say I like to convert some string length to Boolean value
for instance I like to check if string length is greater from 0 it will return me true as Boolean
and if its length is 0 it will return me false as Boolean
what I tried

if :
String str = "";
and I do :
if(!(Boolean)str.length){
it is working but when I do:
String str ="blah";
if(!(Boolean)str.length){
it gives me error , can it be done in java in some way ?
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the first one works?

I don't know what you're problem calls for, but there is a constructor of Boolean that accepts a String, and sets the wrapped boolean value to true if the String is equal, ignoring case, to "true", and otherwise sets the wrapped boolean value to false.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not just explictly do what you want rather than using casting?

(YourString.length() > 0) //this is true

and

(YourString.length() == 0) //this is false

You can either write a method or explictly test for the condition where applicable.

Even in old style C or C++, I dont think your approach is advisable or even correct. In the old school manner in which you are approaching this Boolean is equivalent to 1 and 0. Casting 0 to boolean false and 1 to boolean true works in C, but what does "4" mean? 4 is neither true nor false.

If indeed the first example above (casting zero to false) is working, which I am not sure would and Im not sure wouldnt, but am not surprised it would either, casting a 4 to a boolean (or any other value other than 1 or 0) isnt going to work because 4 is not in any representative of a boolean value.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make a Boolean object you need a boolean primitive. You can get one of those from any boolean expression. So a single line like this will do the job:

Boolean b = new Boolean( expression );

In your case, the expression will check the string length(). See if you can make that work. You may want to check for a null string and return false for null. Or you might just let the NullPointerException fly.

Show us what you make!
 
Java Cowboy
Sheriff
Posts: 16082
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Stan James:
To make a Boolean object you need a boolean primitive. You can get one of those from any boolean expression. So a single line like this will do the job:

Boolean b = new Boolean( expression );


Note that instead of this, it is better (more efficient) to use the method Boolean.valueOf(...) instead of constructing a new Boolean object:

Boolean b = Boolean.valueOf( expression );
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Brian Mozhdehi:

Even in old style C or C++, I dont think your approach is advisable or even correct. In the old school manner in which you are approaching this Boolean is equivalent to 1 and 0. Casting 0 to boolean false and 1 to boolean true works in C, but what does "4" mean? 4 is neither true nor false.



If I remember correctly, in C(++) everything but 0 is considered to mean true.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Boolean b = Boolean.valueOf( expression );



Thanks, I hadn't read the doc in any detail before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!