Java 原始类型:int 与 Integer [英] Java Primitive Types: int vs. Integer

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

问题描述

我对在 Java 中何时使用原始类型与非原始(?)类型(即 int 与 Integer)感到困惑.我意识到在某些地方你不能使用原始类型(例如在使用 泛型).但是在正常"代码中呢?使用非原始类型会有性能损失吗?使用 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?

***我的问题与此非常相似 问题,这是由以下海报之一发现的.链接问题的答案提供了有关此问题的更多见解,以下未涵盖.

***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 是一个数字;Integer 是可以引用包含数字的对象的指针.使用 Integer 进行算术涉及更多 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 原始类型:int 与 Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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