File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ternary operator quicky Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ternary operator quicky" Watch "ternary operator quicky" New topic
Author

ternary operator quicky

Graham VMead
Ranch Hand

Joined: Sep 22, 2003
Posts: 154
Just for my interest could someone explain the following,

If I want to condition the execution of a method dependent on a condition, using the ternary operator.

If both methodA() and methodB() return a String and:

int x =2, y=1;

/* This is ok */

System.out.println(x>y?this.methodA():this.methodB());

/* So is this */

String b = x>y ? this.methodA():this.methodB();

/* and this */

return(x>y ? this.methodA():this.methodB());

But the compiler (in WSAD 5.0.1) doesn't like this as a standalone statement.

x>y ? this.methodA():this.methodB();

It appears that some form of assignment of the result is needed yet I can't find any documentation that says this is the case! Am I missing something??

TIA Graham
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

Originally posted by Graham VMead:

x>y ? this.methodA():this.methodB();

This is an expression and it can't stand alone. Try a simpler line of code like this:

and the compiler will reject it too. It doesn't really do anything (other than evaluate to some value), so it makes sense that the compiler should flag it.
A command to the Java interperter (which is what a line of code is) must be a statement. Statements include things like assignments, flow control and method invocations.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Graham VMead
Ranch Hand

Joined: Sep 22, 2003
Posts: 154
Hi Joe,
Thanks for the reply,

I think I might be being pedantic and its not that important but I can see that

x+y; is obviously non-sensical

but

x>y?methodA():methodB();

is equivalent really to

if(x>y){
this.methodA();
}else{
this.methodB();
}

Which I would think is a very common logical construct and actually does achieve something sensible.

Graham
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Originally posted by Graham VMead:
Hi Joe,
Thanks for the reply,

I think I might be being pedantic and its not that important but I can see that

x+y; is obviously non-sensical

but

x>y?methodA():methodB();

is equivalent really to

if(x>y){
this.methodA();
}else{
this.methodB();
}

Which I would think is a very common logical construct and actually does achieve something sensible.

Graham


Yes, they do appear equivalent, however, the JVM treates them differently. In the case of the if statement, the JVM will execute a block of code depending on boolean condition. However, with the ternary operator ?, the JVM will assign a value to a variable, so the compiler will complain if there is no direct (or indirect via parameter-passing) assignment.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

What i think it boils down to is that you can't do it because the language specification says you can't do it. If you read through the JLS (not a task i would neccesarily reccomend), it tells you exactly what can and can't be done. The trinary operator is something that can't stand alone - it must be part of something else.
[ June 03, 2004: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Graham VMead
Ranch Hand

Joined: Sep 22, 2003
Posts: 154
Thanks for the replies, its back to an if statement for me then
 
jQuery in Action, 2nd edition
 
subject: ternary operator quicky
 
Similar Threads
comparing the range of BigDecimal
Problem when using ternary operatory in For Each loop
ternary operation
Very Interesting Question
alternate code for if/else needed