Eclipse Assignment Operator Expression

Concluding thoughts and addressing complaints

After reading this, you might think that Eclipse is the best thing since sliced bread. While we like to be able to express our opinions, especially pointing out the things in Eclipse that we don’t always enjoy, it’s not our intention to waste your time with complaints. This report intends to be a source for new and experienced users alike to use their selected tool better, that’s all.

What devs often say when they complain about Eclipse

As part of this conclusion, we should acknowledge some of the more common complaints we hear about Eclipse from users, and some brief suggestions for improving upon them.

“Eclipse is slow.”

If you still use Juno, please upgrade asap. Other than that, configuring Eclipse to do less unnecessary things helps a bit. Playing with JVM arguments also gives it a boost, and uninstalling plugins that you don’t use or building your IDE from Runtime Binaries is a way to get noticeably better performance.

“The UI is clumsy and I don’t like it.”

Utilize different perspectives and customise them as you would like. If you want to get the maximum working area, you can use Ctrl+M on your currently open editor. Check out fullscreen plugin to enhance
that experience.

“Content assist and organise imports keep asking about awt and other irrelevant classes.”

Use type filters to prevent Eclipse from suggesting classes that you never use. Use Code Recommenders plugin to improve what content assist does for you.

Some things that Eclipse does that other IDEs don’t do

Not every IDE is created equally, and it’s worth more than half a groat to estimate the flamewar potential that this conversation brings along with it. But it turns out that Eclipse does a couple things that IntelliJ IDEA, NetBeans, and other IDEs do not.

For example, Eclipse is the only IDE that has it’s own compiler, and it’s pretty smart about code that contain errors. When your class file doesn’t compile, Eclipse inserts an unresolved compilation error, like this one:

Exception in thread “main” java.lang.Error: Unresolved compilation problems:

However, the class is compiled otherwise and is functional unless you’re reaching for the method in question.

This second one is questionable, since we aren’t 100% sure at the time of this writing if there isn’t some similar functionality hiding somewhere in NetBeans or IntelliJ IDEA (we’ll find out!), but we felt it important to mention Eclipse’s Scrapbook functionality. Scrapbook is a regular file, not related to any projects which can be used to inspect and evaluate Java code expressions. The cool thing is that with Scrapbook, you can paste pieces of code there and quickly see what that code actually does–essentially, it lets you inspect, evaluate and debug, all in a separate window and away from the larger code base.

Can emacs do that? ;-)

Just download it already! ;-)

« Previous123456789101112


Oleg Shelajev

Oleg Shelajev
Developer Advocate

Oleg Šelajev is an engineer, author, speaker, lecturer and advocate at ZeroTurnaround. He spends his time testing, coding, writing, giving conference talks, crafting blogposts and reports. He is pursuing a PhD on Dynamic System updates and code evolution. Oleg is a part-time lecturer at the University of Tartu and enjoys speaking and participating in Java/JVM development conferences such as JavaOne, JavaZone, JFokus and others. In his free time, Oleg plays chess at a semi-grandmaster level, loves puzzles and solving all kinds of problems.

More posts

Oliver White

Oliver White

The former Indian leg-wrestling champion of his home town, Oliver joined the ZeroTurnaround team in 2010 as the tiny startup's first marketing employee. While not a developer himself, he is inexplicably talented at finding code errors that somehow appear in RebelLabs articles. Oliver likes reading, writing and 'rithmatic, as well as music, films, dogs, steaks, gazing at world maps and strolling through the deeply-forested parks of the Czech Republic, where he lives. Say hi on Twitter @theotown

More posts

Summary: This tutorial shares examples of the Java ternary operator syntax.

Interested in saying a lot while writing a little? In a single line of code, the Java ternary operator let's you assign a value to a variable based on a boolean expression — either a boolean field, or a statement that evaluates to a boolean result.

At its most basic, the ternary operator, also known as the conditional operator, can be used as an alternative to the Java if/then/else syntax, but it goes beyond that, and can even be used on the right hand side of Java statements.

Back to top

Simple ternary operator examples

One use of the Java ternary operator is to assign the minimum (or maximum) value of two variables to a third variable, essentially replacing a or method call. Here’s an example that assigns the minimum of two variables, and , to a third variable named :

minVal = (a < b) ? a : b;

In this code, if the variable is less than , is assigned the value of ; otherwise, is assigned the value of . Note that the parentheses in this example are optional, so you can write that same statement like this:

minVal = a < b ? a : b;

I think the parentheses make the code a little easier to read, but again, they’re optional, so use whichever syntax you prefer.

You can take a similar approach to get the absolute value of a number, using code like this:

int absValue = (a < 0) ? -a : a; Back to top

General ternary operator syntax

Given those examples, you can probably see that the general syntax of the ternary operator looks like this:

result = testCondition ? value1 : value2

As described in the Oracle documentation, this statement can be read as “If is true, assign the value of to ; otherwise, assign the value of to .”

Here are two more examples that demonstrate this very clearly. To show that all things don’t have to be s, here’s an example using a float value:

// result is assigned the value 1.0 float result = true ? 1.0f : 2.0f;

and here’s an example using a String:

// result is assigned the value "Sorry Dude, it's false" String result = false ? "Dude, that was true" : "Sorry Dude, it's false";

As shown in these examples, the can either be a simple boolean value, or it can be a statement that evaluates to a boolean value, like the statement shown earlier.

Finally, here’s one more example I just saw in the source code for an open source project named Abbot:

private static final int subMenuDelay = Platform.isOSX() ? 100 : 0; Back to top

More power: Using the ternary operator on the right hand side of a Java statement

As Carl Summers wrote in the comments below, while the ternary operator can at times be a nice replacement for an if/then/else statement, the ternary operator may be at its most useful as an operator on the right hand side of a Java statement. Paraphrasing what Carl wrote:

The “IF (COND) THEN Statement(s) ELSE Statement(s)” construct is, itself, a statement.

The “COND ? Statement : Statement” construct, however, is an expression, and therefore it can sit on the right-hand side (rhs) of an assignment.

Carl then shared the following nice examples. Here’s his first example, where he showed that the ternary operator can be used to avoid replicating a call to a function with a lot of parameters:

myFunc( (COND ? defaultValue : getMyFuncParameter()) );

Next, here’s an example where the conditional operator is embedded into a , essentially used to construct the properly depending on whether is singular or plural:

returnString = "There " + (x > 1 ? " are " + x + " cookies" : "is one cookie") + " in the jar.";

And finally, here’s one more of his examples, showing a similar operation within a , this time to print the salutation properly for a person’s gender:

returnString = "Thank you " + (person.isMale() ? "Mr. " : "Ms. ") + person.getLastName() + ".";

(Obviously, many thanks to Carl Summers for these comments. He initially shared them as comments below, and I moved them up to this section.)

Back to top

Java ternary operator test class

As a final note, here’s the source code for a Java class that I used to test some of the examples shown in this tutorial:

public class JavaTernaryOperatorExamples { /** * Examples using the Java ternary operator * @author alvin alexander, */ public static void main(String[] args) { // min value example int minVal, a=3, b=2; minVal = a < b ? a : b; System.out.println("min = " + minVal); // absolute value example a = -10; int absValue = (a < 0) ? -a : a; System.out.println("abs = " + absValue); // result is assigned the value 1.0 float result = true ? 1.0f : 2.0f; System.out.println("float = " + result); // result is assigned the value "Sorry Dude, it's false" String s = false ? "Dude, that was true" : "Sorry Dude, it's false"; System.out.println(s); // example using the ternary operator on the rhs, in a string int x = 5; String out = "There " + (x > 1 ? " are " + x + " cookies" : "is one cookie") + " in the jar."; System.out.println(out); } } Back to top

0 Thoughts to “Eclipse Assignment Operator Expression

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *