Java基本类型:整数与整数 [英] Java Primitive Types: int vs. Integer

查看:180
本文介绍了Java基本类型:整数与整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑时,使用原始的与非原语(?)类型(即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?

***我的问题是非常类似<一个href="http://stackoverflow.com/questions/2509025/when-to-use-primitive-and-when-reference-types-in-java">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.

推荐答案

简短的回答:一个<​​code> 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).

这篇关于Java基本类型:整数与整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆