Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining new variable

Tags:

java

variables

I'm quite new at programming in Java and I've encountered something I don't really understand:

if (Object.getSomething() != null) {
        Long Size= null != Object.getSomething().getSomething2()
            ? Object.Something().getSomething2() : null;

I've been looking for the answer but I can't understand this way of defining a new variable, I mean, the '?' and the ': null' are the things I can't understand.

like image 963
Mr. Abat Avatar asked Sep 21 '26 06:09

Mr. Abat


1 Answers

Ternary conditionals take the following form:

condition ? value_if_true : value_if_false

Consider for instance the mathematical max function. Using regular conditional statements we could write:

int max = 0;
if (a > b) {
  max = a;
} else {
  max = b;
}

We can do the exact same thing using a ternary condition: max = a > b ? a: b;

like image 82
MrHug Avatar answered Sep 23 '26 21:09

MrHug