Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write nullable int in java?

Tags:

java

c#

nullable

I want to convert a web form to a model in Java.

In C# I can write this:

<input name="id" value="" type="text"/>   public class Test {     public int? Id{get;set;} } 

The id can be null.

But in Java when using struts2 it throws an exception:

Method "setId" failed 

So how to write this case in Java?

like image 493
Dozer Avatar asked Jan 14 '13 15:01

Dozer


People also ask

Is int nullable in Java?

Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.

How do you create a nullable integer?

You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.


1 Answers

Instead of using int you can use Integer (Integer javadoc) because it is a nullable Java class.

like image 56
Ivaylo Strandjev Avatar answered Oct 09 '22 22:10

Ivaylo Strandjev