[英]Should I define class attributes primitives (when it's possible) or should I use a Wrapper class?
I am wondering if it's recommended always to use primitive types such as int when defining class attributes or should I wrapper classes instead of.我想知道在定义 class 属性时是否建议始终使用诸如 int 之类的原始类型,或者我应该包装类而不是。
class Test{
private final int id;
/**
* Class constructor
* @param id
*/
Test(int id) {
this.id = id;
}
}
It depends on what you need to do... If it's important to you to hold null values so you should use wrappers... Else you could as well use primitive... If you don't know if you need null or not then it's better to use primitives to avoid getting NPE.这取决于您需要做什么...如果保存 null 值对您很重要,那么您应该使用包装器...否则您也可以使用原始...如果您不知道是否需要 null那么最好使用原语来避免获得NPE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.