This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ternary operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ternary operation" Watch "ternary operation" New topic
Author

ternary operation

Pat Peg
Ranch Hand

Joined: Feb 04, 2005
Posts: 194
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

Joined: Dec 29, 2004
Posts: 1071
What are the errors you are getting?
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
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

Joined: Jun 09, 2005
Posts: 160
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

Joined: Feb 04, 2005
Posts: 194
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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Feb 18, 2005
Posts: 989
    
    1
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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");


[Jess in Action][AskingGoodQuestions]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Doh! *slaps forhead*

Can't believe I missed that. For some reason I was thinking void was ok in ternary.
Pat Peg
Ranch Hand

Joined: Feb 04, 2005
Posts: 194
Ah, thanks...That clears it up
samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160
thats cool ernest

A correct use of this operator would be

System.out.println(low > high ? "bad values" : "good values");
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 989
    
    1
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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

Joined: Jan 14, 2005
Posts: 323
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

Joined: Jul 11, 2001
Posts: 14112
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



The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ternary operation
 
Similar Threads
String issue in Java code
Ternary Operator behavior in JAVA
Confusing me the two question?please help ~~
Advantage of Ternary operator ?
random number gene betwn 2 no's