Creating Nullables types in java

元气小坏坏 提交于 2019-12-01 15:15:25

I'm not entirely sure what you want, but if you want to have an integer value that also can be declared null, you probably want to use the Integer class:

Integer nullableInteger = 1;
nullableInteger = null;
System.out.println(nullableInteger); // "null"

There are corresponding classes for each primitive: Character, Long, Double, Byte, etc. The 'standard library' numeric classes all extend the Number class.

Note that Java autoboxes these objects automatically since JDK 1.5, so you can use and declare them just like the primitives (no need for e.g. "new Integer(1)"). So, although they are technically objects (and, therefore, extend the Object class, which the primitive int type does not), you can do basic arithmetics with them. They are converted to object operations at compile time.

Peter Recore

Java does not support nullable primitives. You can use the Integer type if you want the ability to store nulls.

(This is a duplicate of this post: How to present the nullable primitive type int in Java?)

Perhaps try the Integer type in Java.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!