• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

ternary operator

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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
  • 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
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic