Skip to main content

If null operator

No replies
tiomalandra
Offline
Joined: 2006-06-25

Hi People.

I'm thinking, if Java 7 has a @NotNull annotation, to prevent null asignments, will be useful a new operator. For example, if you want to assign a nullable value to a @NotNull value, you need a kind of cast:

public void myMethod(Integer a){
@NotNull Integer b = a != null ? a: 0; // this can raise a compiler error
}

Suppose we have an new operator, for example:

public void myMethod(Integer a){
// if a is not null, then a will be stored in b. Else, b will be 0
@NotNull Integer b = a ?? 0;
}

Cheers