1

I'm looking at a piece of Java code that contains:

User rv = tmp != null && tmp.size() > 0 ? tmp.get(0) : null;

I'm not very strong with Java syntax. My interpretation is that rv = tmp as long as tmp is null tmp's size is > 0, or else it equals null. Am I correct?

MadProgrammer
348k22 gold badges243 silver badges379 bronze badges
asked Oct 23, 2013 at 2:11
2
  • 3
    This is something you could test to find out on your own. Commented Oct 23, 2013 at 2:13
  • 1
    Probably if I wrote this I would use brackets to make it easier for a junior programmer to understand User rv = (tmp != null && tmp.size() > 0) ? tmp.get(0) : null; Commented Oct 23, 2013 at 2:16

4 Answers 4

7

Here is the "anatomy" of this expression:

rv = // assignment of a conditional expression
 (tmp != null && tmp.size() > 0) // condition
? tmp.get(0) // on true
: null; // on false

This is a common way of ensuring that there would be no exception accessing element zero of the list: the condition ensures that tmp is not null, and also that the size is one or more.

answered Oct 23, 2013 at 2:14
Sign up to request clarification or add additional context in comments.

2 Comments

Note that the code is almost illegible and needs parentheses at a minimum.
For added background information regarding ternary operators in java, click here (wikipedia article) and here (java tutorial)
3

It's a ternary conditional expression:

expr ? value-if-true : value-if-false

If expr is true, it evaluates to value-if-true, and otherwise it evaluates to value-if-valuse.

So, in this case, it's equivalent to:

 if (tmp != null && tmp.size() > 0) {
 rv = tmp.get(0);
 } else {
 rv = null;
 }
answered Oct 23, 2013 at 2:15

Comments

2
rv = ((tmp != null && tmp.size() > 0) ? tmp.get(0) : null); 

if expression inside the inner brackets return true then rv will hold the value tmp.get(0) else it will hold the value null.

variable = expression ? a : b ;

is similar to

if expression == true 
 variable = a;
else 
 variable =b ;
answered Oct 23, 2013 at 2:16

Comments

0

This is called a ternary statement.

Basically,

User rv = tmp != null && tmp.size() > 0 ? tmp.get(0) : null;

is a shorter way of writing:

 User rv;
 if (tmp != null && tmp.size() > 0)
 rv = tmp.get(0);
 else
 rv = null;

The ternary statement is not faster. Although, in some cases, using it makes your code more readable.

answered Oct 23, 2013 at 2:18

1 Comment

in some cases, using it makes your code more readable. - Not in this case though (if it had parentheses it would be better)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.