In Java, can I use conditional expressions to “select” reference types?

Can I use conditional expressions to "select" reference types, as shown below?

??? = isTrue() ? Integer : Double;

What can I put in "??" Make code snippets editable?

Editor: when I write integer and double, I don't mean instances of integer or double I mean reference types integer and double

Solution

I think you are trying to use ternary Boolean operators to select the reference type of variables

This is not possible in Java because Java is statically typed - which means that each type is explicitly defined at compile time Your expression wants to run a method and determine reference types that are impossible at compile time based on its output

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
分享
二维码
< <上一篇
下一篇>>