How to write nullable int in java?

前端 未结 3 1629
一向
一向 2020-12-29 19:14

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

In C# I can write this:




public class Test
{
           


        
相关标签:
3条回答
  • 2020-12-29 19:41

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

    0 讨论(0)
  • 2020-12-29 19:46

    You can use an Integer, which is a reference type (class)in Java and therefore nullable.

    Int32 (or int) is a struct (value type) in C#. In contrast, Integer in Java is a class which wraps an int. Instances of reference types can be null, which makes Integer an legit option.

    Nullable<T> in .NET gives you similar options because it enables you to treat a value type like a nullable type. However, it's still different from Java's Integer since it's implemented as a struct (value type) which can be compared to null, but cannot actually hold a genuine null reference.

    0 讨论(0)
  • 2020-12-29 20:02

    In Java, just use Integer instead of int. This is essentially a nullable int. I'm not too familiar with Struts, but using Integer should allow you to omit the value.

    0 讨论(0)
提交回复
热议问题