Results 1 to 11 of 11
Thread: use boolean as 0 or 1
- 04-12-2010, 03:28 PM #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; }
- 04-12-2010, 03:31 PM #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!!
- 04-12-2010, 03:36 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 8
- Rep Power
- 0
- 04-12-2010, 04:10 PM #4
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
- 04-12-2010, 05:14 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 04-12-2010, 05:16 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 04-12-2010, 07:21 PM #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
- 04-12-2010, 07:58 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 04-12-2010, 09:50 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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
- 04-13-2010, 11:49 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.
- 04-13-2010, 12:22 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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
Java Code:boolean b= ...; b= !b; // flip
kind regards,
Jos
Similar Threads
-
Boolean method help
By syferite in forum New To JavaReplies: 6Last Post: 10-28-2009, 02:32 PM -
Boolean value not working?
By zerkz in forum New To JavaReplies: 3Last Post: 09-29-2009, 07:42 AM -
Boolean problems
By Chasingxsuns in forum New To JavaReplies: 2Last Post: 09-15-2009, 11:57 PM -
transfer boolean to 1's and 0's
By Nikohw in forum Java AppletsReplies: 5Last Post: 09-12-2009, 10:05 PM -
Boolean Expression
By ritwik07 in forum New To JavaReplies: 3Last Post: 07-11-2007, 05:11 AM
Bookmarks