# Thread: use boolean as 0 or 1

1. Member
Join Date
Apr 2010
Posts
8
Rep Power
0

## use boolean as 0 or 1

Hi,

I just started programming with Java, and I came from TI-basic (the language texas instruments' graphing calculators can be programmed on), and there is one thing I'd really like to know.

on TI-basic, if I wanted to use a boolean (like a=5) to change another, variable like b, so say I would like to make b 8 if a is 5, and 6 if a isnt 5. I would just type:
b = 6+2(a=5)
since a=5 outputs 1 if it's true and 0 if it isn't.

In java, I would have to type
Java Code:
if (a == 5)
{
b = 8;
}
else
{
b=6;
}
So my question is, is there any easy way to use booleans in equations like in TI-basic, or just any other way that needs less command lines?

2. Member
Join Date
Apr 2010
Posts
3
Rep Power
0
My name is Aaron, i am trying to do an Object Orientated program in java where you have to be able to enter a students information name id and number in an array like in a menu form and be able to add and delete new students.

Need Help!!

3. Member
Join Date
Apr 2010
Posts
8
Rep Power
0
Originally Posted by Boysie
My name is Aaron, i am trying to do an Object Orientated program in java where you have to be able to enter a students information name id and number in an array like in a menu form and be able to add and delete new students.

Need Help!!

If you send me an e-mail, I might be able to help you since I created a system like that a year ago. e-mail: Joost.meulenbeld[at]gmail[dot]com

4. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Originally Posted by joost_m
So my question is, is there any easy way to use booleans in equations like in TI-basic, or just any other way that needs less command lines?
Suprised to see the topic in the Applet thread. Anyway...

you can use easy boolean statement like a

Java Code:
int b=(a==5)?8:6;

Java Code:
if (a == 5)
{
b = 8;
}
else
{
b=6;
}

Good Java Coding
Last edited by Webuser; 04-12-2010 at 03:12 PM.

5. Originally Posted by Boysie
My name is Aaron, i am trying to do an Object Orientated program in java where you have to be able to enter a students information name id and number in an array like in a menu form and be able to add and delete new students.

Need Help!!
Welcome to forum. :)

Please don't post unrelated question next time in threads. It could mess-up lots of members. If your question is not related with the original please start a new thread, in relevant sub-forum. Good luck.

6. Originally Posted by Webuser
Suprised to see the topic in the Applet thread.
I'll move the thread to correct place.

@OP, please choose the correct sub-forum next time when you posting again.

7. Member
Join Date
Apr 2010
Posts
8
Rep Power
0
Sorry, I'll post in the right sub-forum next time.
to webuser: thanks for the post, this really helps.
The solution is useful in this case. However, if you use the (a=5) method a number of times in one expression, you can't use this way in java. That would mean you have to make one variable for each boolean you'd want to use as 1 or 0, or of course a method that will convert booleans to ints. Thanks, topic can be closed

8. Originally Posted by joost_m
However, if you use the (a=5) method a number of times in one expression, you can't use this way in java. That would mean you have to make one variable for each boolean you'd want to use as 1 or 0, or of course a method that will convert booleans to ints.
You mean the multiple nested if conditions?

Originally Posted by joost_m
Thanks, topic can be closed
It's better to mark thread solved by yourself. Since you are the person looking the solution, and satisfied on it.

9. Originally Posted by joost_m
Sorry, I'll post in the right sub-forum next time.
to webuser: thanks for the post, this really helps.
The solution is useful in this case. However, if you use the (a=5) method a number of times in one expression, you can't use this way in java. That would mean you have to make one variable for each boolean you'd want to use as 1 or 0, or of course a method that will convert booleans to ints. Thanks, topic can be closed
IMHO it was one of the stupidest mistakes to omit that 'feature' from C just because of an explicit boolean type, but that's just me; I love the fiddling with zero and non-zero values being implicitly considered false and true respectively. It allows for very terse code and booleans don't even exist as such in the JVM, it's all 0s and 1s anyway.

Nice question b.t.w. I happen to own a TI92+, a TI Voyage and an HP50G and none of my TIs can handle those tricks but they have a Motorola MC68000 on board so I can program them in C ;-) That HP50G is an entirely different story.

kind regards,

Jos

10. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23
Terse code is not all the descriptive, though.
Treating a numeric as a boolean never made sense to me, so the dropping of it from Java was no problem. This isn't coding to the metal, after all...and trying to code like that in Java doesn't make much sense to me.

11. Originally Posted by Tolls
Terse code is not all the descriptive, though.
Treating a numeric as a boolean never made sense to me, so the dropping of it from Java was no problem. This isn't coding to the metal, after all...and trying to code like that in Java doesn't make much sense to me.
Having programmed in C for more than thirty years leaves its marks, I know. Coincidentally a colleague and I had a (friendly) little discussion today about how to flip the value of a boolean variable; I did it like this:

Java Code:
boolean b= ...;
b^= true; // flip
he objected that many younger programmers might not understand this and coined this:

Java Code:
boolean b= ...;
b= !b; // flip
Look at the compiled code (javap) and see the generated byte code for the two variants.

kind regards,

Jos