• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you translate this : ?  RSS feed

 
Bart Wilson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you translate these shortcuts of nonstandard java coding?
<%=house.getNumberOfOccupants() > 0 ?
house.getNumberOfOccupants() : 0%>
or
<%=house.getTypeOfOccupants() != null ?
house.getTypeOfOccupants() : ""%>
I'm trying to figure out how to do this for a return type boolean.
Thanks
[This message has been edited by Bart Wilson (edited August 14, 2001).]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<%=house.getNumberOfOccupants() > 0 ?
house.getNumberOfOccupants() : 0%>
or
<%=house.getTypeOfOccupants() != null ?
house.getTypeOfOccupants() : ""%>

Assuming you want to translate them to if...else

Similarly the other one....
regds.
- satya
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ternary conditional operator (I feel like a Borg when I say it) is standard Java, though I admit it isn't heavily used. Probably because it can become very confusing if used (in my opinion) where nested if-elses are more appropriate.

Here's a great example:
As for "trying to figure out how to do this with return type boolean". I got caught up one day thinking "jeez, this ternary operator is cool!" and I started using it like this:

I quickly realized that this is what the if statement was for.
As for why one would use it ... well specifically inside a scriplet tag, you can't use { }'s or semicolons, so it's a way to stuff a whole bunch of code into one simple line. This should probably indicate that you are trying to fit too much into the scriptlet, but I'm guilty of the usage myself.. especially for small details like "if it's positive, print it, if it's negative, print zero".
[This message has been edited by Mike Curwen (edited August 15, 2001).]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!