Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse ternary operator shortcut?

In Eclipse (3.4+ upwards), I was searching for shortcut which converts the if {} else {} condition block to the java ternary operator (or the ?: operator).

How can I do this?

like image 683
Narayan Avatar asked Aug 25 '10 13:08

Narayan


People also ask

How do you write a ternary operator in Java?

The syntax of the Java ternary operator is as follows: (condition) ? (return if true) : (return if false); You will often see the Java ternary operator symbols ( ? : ) used in texts and tutorials as an abbreviation for the construct.

What is the shortcut key for return type in Eclipse?

CTRL+2,L and ALT+CTRL+L can get the job done for you.

Which of these is a ternary operator in Java?

The ternary operator (? :) consists of three operands. It is used to evaluate Boolean expressions. The operator decides which value will be assigned to the variable. It is the only conditional operator that accepts three operands.


3 Answers

There is no such shortcut. Refactoring statements into expressions isn't a trivial process, and isn't always possible to begin with. The process would be too complicated to be automated.

That said, Ctrl + Shift + L will list ALL Eclipse shortcuts.

Related questions

  • Hidden features of Eclipse
  • What is your favorite hot-key in Eclipse
like image 134
polygenelubricants Avatar answered Oct 19 '22 23:10

polygenelubricants


Excuse me if this is not relevant anymore.

I have written an eclipse plug-in that does exactly this:

http://marketplace.eclipse.org/content/spartan-refactoring

Feel free to report back if you have problems with installation or using this plug-in.

Edit: Re-written version by other developers is available at https://marketplace.eclipse.org/content/spartan-refactoring-0

like image 25
Artium Avatar answered Oct 20 '22 00:10

Artium


Well, you could add a Template to Eclipse like this:

${condition:field(boolean)}? ${positive:field(void)}: ${negative:field(void)};

Name it something like tern and you can have it auto-create the ternary operator for you.

(Not sure if this is what you want)

like image 2
jjnguy Avatar answered Oct 19 '22 23:10

jjnguy