Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ternary operation

 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this piece of code fail?


I'm just trying to understand how to use the ternary operator and I am getting errors.

Thanks for entertaining the easy questions.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the errors you are getting?
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you misspell "high" in the program as well, or just here?



If that's not it, what code do you have instead of the ...???
 
samdeep aarzoo
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the complete solution
ternary operator is use to check the condition
a>b ? c

a>b must be a boolean condition
so it will evalute true or false

if true c will be executed
if false d will be executed

this ternary operator is shortcut of if else statement

if(a>b)
{
print c
}
else
{
print d
}
i m giving u code in java to make understand u more easily
_____________________________________________________________
public class ternary
{
public static void main(String args[])
{
int low =1;
int high =2;

String status1 = (high>low) ? "hi" : "bye";
System.out.println(status1);

String status2 = (high<low) ? "hi" : "bye";
System.out.println(status2);

}
}

output will be

hi
bye
 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe...no, my spelling is bad but I did manage to spell it the same in the program so that isn't it.
the error I am getting says "syntax error on "?", "=","=="...etc...basically listing all operators.
I thought it should work like an if-else in that it should evaluate the realtionship between hign and low and then execute either the first bit of the second bit depending.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're ternary looks right. I'd guess that the problem lies higher up in the source. Maybe an out of place {,},(,or)
 
Ryan McGuire
Ranch Hand
Posts: 1068
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the ternary operator can only be used to select between two expressions that have values. System.err.println() "returns" a void, so you can't use those values (which aren't returned ) with the ternary operator.

What you could do use the ternary operator to select what String to print with that void-returning println():

System.err.println((low>high) ? "bad values" : "good values");

Ryan
[ June 14, 2005: Message edited by: Ryan McGuire ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ternary operator isn't quite the same as an if-then because it's an expression, not a statement. It returns a value (not void) and the value has to be used as part of a complete legal Java statement. Writing the ternary operator all by itself like this is like just writing

2 + 2;

which is also an expression rather than a valid statement, and is also not valid Java.

A correct use of this operator would be

System.out.println(low > high ? "bad values" : "good values");
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! *slaps forhead*

Can't believe I missed that. For some reason I was thinking void was ok in ternary.
 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thanks...That clears it up
 
samdeep aarzoo
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats cool ernest

A correct use of this operator would be

System.out.println(low > high ? "bad values" : "good values");
 
Ryan McGuire
Ranch Hand
Posts: 1068
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooh wait, upon rereading the original post, I see we missed a subtlety. The low>high comparison is also being used to determine the where the output goes.

How about


There, perfect.

Ryan
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan McGuire:

(low>high ? System.err : System.out).println(low>high ? "bad values" : "good values");
[/QB]


That's correct. OTOH, I truly hope no one reading this will use anything like this in real code when they could simply write



Newlines are, after all, made of 100% recyclable electrons!
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i second mr. Friedman's latest post — ternary operators often aren't worth their trouble. i have some experience of a language that doesn't have a ternary, and almost nobody misses it. there is a recommended work-around for this lack in that particular language; i think i've used it exactly once, maybe twice, in a year.

(that particular work-around doesn't work in Java, though, because logical operators can't be applied to dissimilar types in Java. no loss; the ternary and the work-around i'm thinking of are about equally as hard to read, anyway.)
[ June 14, 2005: Message edited by: M Beck ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If used reasonably, I find that the ternary operator can actually improve the readability of code.

At least I find



to be more expressive than

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic