Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make non-nullable type in Java

Is it possible to make non-nullable type in Java? Objects of this type shouldn't can be null. How?

like image 804
slider Avatar asked Apr 24 '14 10:04

slider


People also ask

How do you make a variable non nullable?

Nullability of a type can be expressed by explicitly prefixing the type name with a modifier. A variable can be made explicitly nullable with the nullable keyword, or explicitly non-nullable with the not nullable keyword combination.

How do I stop null check in Java 8?

Java 8 introduced an Optional class which is a nicer way to avoid NullPointerExceptions. You can use Optional to encapsulate the potential null values and pass or return it safely without worrying about the exception. Without Optional, when a method signature has return type of certain object.

What is @nullable Java?

@Nullable The @Nullable annotation helps you detect: Method calls that can return null. Variables (fields, local variables, and parameters), that can be null.

Does Java have nullable types?

Yes you can. To do this sort of thing, java has a wrapper class for every primitive type. If you make your variable an instance of the wrapper class, it can be assigned null just like any normal variable.

What is @nullable annotation in Java?

The @Nullable annotation helps you detect: Method calls that can return null Variables (fields, local variables, and parameters), that can be null Methods with the @Nullable annotation in the parent method can have either @Nullable or @NotNull annotations in the child class method.

Which types are nullable by default?

By contrast, reference types (i.e. classes or interfaces) are considered nullable by default, and may be nil. Nullability of a type can be expressed by explicitly prefixing the type name with a modifier.

What is the difference between nullable and non-nullable types?

For value types (records, enums and simple numeric types), variables are assumed to be non-nullable by default, and always have a value (which might be 0, which is of course distinct from nil ). By contrast, reference types (i.e. classes or interfaces) are considered nullable by default, and may be nil.

What does it mean to mark a string as nullable?

For example, even though Strings are reference types, and nullable by default, marking a Method Parameter or a Method Result as nullable String can express the intention that the method will accept or potentially return a nil value.


1 Answers

It is a reasonably common practice to use an @NotNull annotation which is supported by some IDEs and maven plugins. In Java 8 you can write

@NotNull String text;
@NotNull List<@NotNull String> strings = ...;

This is not a language feature, but if you need this, it is available.

Note: there isn't a standard @NotNull annotation :( So the tools which support this allow you to configure which one(s) you want. I use the one which comes with IntelliJ. It gives you warnings in the editor with auto-fixes and add runtime checks for null arguments, return values and variables.

Note: IntelliJ is able to work out if a field is not nullable by usage as well.

like image 122
Peter Lawrey Avatar answered Oct 11 '22 01:10

Peter Lawrey