• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

ternary operator

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Just Look At The Code




See if getValue() method return true then i want to set the value "true"
to setValue() method.

But it is giving exception like not a statement after ?:

when the condition becomes true i want to set the
value to setVAlue method.

Can you correct me.

Thanks in Advance,
Raj.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't use the ? operator to choose between void methods. The methods need to return a value.

Use "if".

In general, don't over-use the ? operator. You'll end up writing code that looks like Perl (i.e. unreadable). The ? operator sometimes enhances readability but often reduces it. An "if" statement is very often more readable.
[ May 24, 2007: Message edited by: Peter Chase ]
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ternary operator represents an expression; the "branches" must be expressions (must have non-void type). You have to write this sort of code using an "if" test; in this case, though, you could just write:

setValue(String.valueOf(getValue()));

String.getValue(boolean) returns "true" or "false", as appropriate.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj:

the ternary operator is used as a short hand for a simple if-else statement, for example:

String result = "";
if ( getValue() == true ) {
result = "Value is true";
} else {
result = "Value is false";
}

could be written in a more compact format as:

String result = getValue() ? "Value is true" : "Value is false";

The key point to remember is that the ternary operator WILL return a value as a result of evaluating the condition. This value is normally assigned to a variable. In your case, you want to selectively execute logic based on the condition, so you need an if-else statement of the form:

if ( getValue() ) {
setValue("true");
} else {
setValue("false");
}

since "setValue" returns nothing (ie void).

Cheers,

Joe
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I once lived in fear of the ternary operator, then forced myself to use it twice, and now I love it. Overuse or long complex statements can still be a problem, but it's too cool to completely ban.

could be

or, just because of these specific values, probably
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One would think that if getValue() returns a boolean, setValue() should accept a boolean. Or both could use Strings. Either way,

would become

which in all likelihood could be simply deleted, with no effect.

But I agree, the ternary operator is too cool to avoid.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic