Java – define new variables

I'm a novice in Java programming. I met something I don't quite understand:

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

I've been looking for answers, but I can't understand this way of defining new variables. I mean '?': 'null' is something I can't understand

Solution

Let's look at a simple ternary operation:

Object object;
int i = object == null ? 1 : 2;

In Java code, it is the same as writing standard if else statements

Object object;
int i;

if (object == null) {
    i = 1;
} else {
    i = 2;
}

As you can see, traditional methods require more lines This is one of the main advantages of ternary operators

In natural language, the ternary operation is as follows: "is the object equal to null? Then 1 is 2"

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>