• <bdo id='vBQVG'></bdo><ul id='vBQVG'></ul>
    <i id='vBQVG'><tr id='vBQVG'><dt id='vBQVG'><q id='vBQVG'><span id='vBQVG'><b id='vBQVG'><form id='vBQVG'><ins id='vBQVG'></ins><ul id='vBQVG'></ul><sub id='vBQVG'></sub></form><legend id='vBQVG'></legend><bdo id='vBQVG'><pre id='vBQVG'><center id='vBQVG'></center></pre></bdo></b><th id='vBQVG'></th></span></q></dt></tr></i><div id='vBQVG'><tfoot id='vBQVG'></tfoot><dl id='vBQVG'><fieldset id='vBQVG'></fieldset></dl></div>

      <tfoot id='vBQVG'></tfoot>
      <legend id='vBQVG'><style id='vBQVG'><dir id='vBQVG'><q id='vBQVG'></q></dir></style></legend>

        <small id='vBQVG'></small><noframes id='vBQVG'>

        使用 int 与 Integer

        Using int vs Integer(使用 int 与 Integer)

        <small id='KNW9Y'></small><noframes id='KNW9Y'>

          <tbody id='KNW9Y'></tbody>
        <tfoot id='KNW9Y'></tfoot>
            <i id='KNW9Y'><tr id='KNW9Y'><dt id='KNW9Y'><q id='KNW9Y'><span id='KNW9Y'><b id='KNW9Y'><form id='KNW9Y'><ins id='KNW9Y'></ins><ul id='KNW9Y'></ul><sub id='KNW9Y'></sub></form><legend id='KNW9Y'></legend><bdo id='KNW9Y'><pre id='KNW9Y'><center id='KNW9Y'></center></pre></bdo></b><th id='KNW9Y'></th></span></q></dt></tr></i><div id='KNW9Y'><tfoot id='KNW9Y'></tfoot><dl id='KNW9Y'><fieldset id='KNW9Y'></fieldset></dl></div>

                <legend id='KNW9Y'><style id='KNW9Y'><dir id='KNW9Y'><q id='KNW9Y'></q></dir></style></legend>
                • <bdo id='KNW9Y'></bdo><ul id='KNW9Y'></ul>
                • 本文介绍了使用 int 与 Integer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我遇到了一个类,它使用整数变量来捕获要在 for 循环中使用的大小.这是一种好的做法还是我们应该使用 int 原始数据类型?

                  I came across a class using Integer variables to capture size to be used in a for loop. Is this good practice or should we use the int primitive data type?

                  Integer size = something.getFields().size();
                  for (Integer j = 0; j < size - 1; ++j) 
                  

                  推荐答案

                  提供了 Integer 类,以便可以以纯 OO 方式对值进行装箱/拆箱.在适当的情况下使用 int ,除非您特别需要以 OO 方式使用它;在这种情况下,整数是合适的.

                  the Integer class is provided so that values can be boxed/unboxed in a pure OO manner. use int where appropriate unless you specifically need to use it in an OO way; in which case Integer is appropriate.

                  Java Int vs Integer

                  然而,这里的幕后却发生了截然不同的事情.int 是一个数字;an > Integer 是可以引用包含数字的对象的指针.

                  However, very different things are going on under the covers here. An int is a number; an > Integer is a pointer that can reference an object that contains a number.

                  ...

                  int 不是对象,不能传递给任何需要的方法对象.一个常见的情况是使用提供的集合类(List , Map , Set ) - 尽管可以编写这些版本提供与对象版本类似的功能的类.这包装类( Integer , Double 等)经常需要每当使用自省时(例如在反射 API 中).

                  An int is not an object and cannot passed to any method that requires objects. A common case is in using the provided collection classes ( List , Map , Set ) - though it is possible to write versions of these classes that provide similar capabilities to the object versions. The wrapper classes ( Integer , Double , etc) are frequently required whenever introspection is used (such as in the reflection API).

                  更好地描述何时使用一个与另一个:

                  A better description of when to use one vs. the other:

                  在 int 和 Integer 之间进行选择

                  Choosing between int and Integer

                  在进入之前,我会先介绍如何使用这些类型详细说明原因.

                  I'll start with how these types should be used before going into detail on why.

                  • 出于性能原因,首选 int
                  • 接受对象的方法(包括像 List<T> 这样的泛型类型)将隐式要求使用 Integer
                  • 使用 Integer 对于低值(-128 到127)因为实习 - 使用 Integer.valueOf(int) 而不是新的整数(int)
                  • 不要在整数类型中使用 ==!=
                  • 当您需要表示没有值(null)
                  • 注意将 Integer 值拆箱为具有 null 值的 int
                  • Prefer int for performance reasons
                  • Methods that take objects (including generic types like List<T>) will implicitly require the use of Integer
                  • Use of Integer is relatively cheap for low values (-128 to 127) because of interning - use Integer.valueOf(int) and not new Integer(int)
                  • Do not use == or != with Integer types
                  • Consider using Integer when you need to represent the absence of a value (null)
                  • Beware unboxing Integer values to int with null values

                  这篇关于使用 int 与 Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  <i id='RLFZv'><tr id='RLFZv'><dt id='RLFZv'><q id='RLFZv'><span id='RLFZv'><b id='RLFZv'><form id='RLFZv'><ins id='RLFZv'></ins><ul id='RLFZv'></ul><sub id='RLFZv'></sub></form><legend id='RLFZv'></legend><bdo id='RLFZv'><pre id='RLFZv'><center id='RLFZv'></center></pre></bdo></b><th id='RLFZv'></th></span></q></dt></tr></i><div id='RLFZv'><tfoot id='RLFZv'></tfoot><dl id='RLFZv'><fieldset id='RLFZv'></fieldset></dl></div>
                  <legend id='RLFZv'><style id='RLFZv'><dir id='RLFZv'><q id='RLFZv'></q></dir></style></legend>
                  • <bdo id='RLFZv'></bdo><ul id='RLFZv'></ul>

                  • <small id='RLFZv'></small><noframes id='RLFZv'>

                          <tbody id='RLFZv'></tbody>
                        <tfoot id='RLFZv'></tfoot>