Java基本类型:整数与整数整数、类型、基本、Java

2023-09-05 11:06:49 作者:最好的疼爱丶手放开

我感到困惑时,使用原始的与非原语(?)类型(即INT与整数)在Java中。我认识到,在一些地方,你可以利用仿制药时,不使用基本类型(例如)。但是,我们在正常code?是否有使用非原始类型的性能损失?怎么样与Android合作时?

I am confused about when to use primitive vs. non-primitive(?) types (i.e. int vs. Integer) in Java. I realize that in some places you can't use primitive types (for example when making use of generics). But what about in "normal" code? Is there a performance penalty for using non-primitive types? What about when working with Android?

***我的问题是非常类似question,其中发现了下面的海报中的一个。这些问题的答案的链接的问题给予更多的洞​​察到这个问题,这是不低于覆盖。

***My question is very similar to this question, which was discovered by one of the posters below. The answers to the linked question give additional insights into this question, which are not covered below.

* 的非基本类型被正式称为引用类型。

* "non-primitive" types are officially referred to as reference types.

推荐答案

简短的回答:一个 INT 是一个数字;一个整数是一个指针,可以引用一个对象,包含了许多。使用整数算术涉及到更多的CPU周期和占用更多的内存。一个 INT 不是一个对象,不能传递给需要的对象(就像你说的关于仿制药什么的)任何方法。

Short answer: An int is a number; an Integer is a pointer that can reference an object that contains a number. Using Integer for arithmetic involves more CPU cycles and consumes more memory. An int is not an object and cannot passed to any method that requires objects (just like what you said about Generics).