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

      <tfoot id='Zhsr5'></tfoot>
          <bdo id='Zhsr5'></bdo><ul id='Zhsr5'></ul>
        <legend id='Zhsr5'><style id='Zhsr5'><dir id='Zhsr5'><q id='Zhsr5'></q></dir></style></legend>

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

      1. new Integer(123)、Integer.valueOf(123) 和 just 123 之间的区别

        Differences between new Integer(123), Integer.valueOf(123) and just 123(new Integer(123)、Integer.valueOf(123) 和 just 123 之间的区别)

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

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

          <legend id='yqUct'><style id='yqUct'><dir id='yqUct'><q id='yqUct'></q></dir></style></legend>

              <tbody id='yqUct'></tbody>
                <bdo id='yqUct'></bdo><ul id='yqUct'></ul>
              • <tfoot id='yqUct'></tfoot>

                1. 本文介绍了new Integer(123)、Integer.valueOf(123) 和 just 123 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  最近我看到这样的代码(Java):

                  Recenlty I saw code (Java) like this:

                  myMethod(new Integer(123));
                  

                  我目前正在重构一些代码,Sonar 工具中有一个提示,使用这样的东西对内存更友好:

                  I am currently refactoring some code, and there is a tip in Sonar tool, that it's more memory friendly to use sth like this:

                  myMethod(Integer.valueOf(123));
                  

                  但是在这种情况下,我认为如果我会使用没有区别:

                  However in this case, I think that there is no difference if I would use:

                  myMethod(123);
                  

                  我可以理解,如果我将变量传递给方法,但硬编码 int?或者如果会有 Long/Double 等,我想要 Long 表示数字.但是整数?

                  I could understand that, if I would pass a variable to the method, but hard coded int? Or if there would be Long/Double etc and I want Long representation of number. But integer?

                  推荐答案

                  new Integer(123) 将为每个调用创建一个新的 Object 实例.

                  new Integer(123) will create a new Object instance for each call.

                  根据 javadoc, Integer.valueOf(123) 不同之处在于它缓存对象......所以如果你调用它更多,你可能(或可能不会)最终得到相同的 Object不止一次.

                  According to the javadoc, Integer.valueOf(123) has the difference it caches Objects... so you may (or may not) end up with the same Object if you call it more than once.

                  比如下面的代码:

                     public static void main(String[] args) {
                  
                          Integer a = new Integer(1);
                          Integer b = new Integer(1);
                  
                          System.out.println("a==b? " + (a==b));
                  
                          Integer c = Integer.valueOf(1);
                          Integer d = Integer.valueOf(1);
                  
                          System.out.println("c==d? " + (c==d));
                  
                      }
                  

                  有以下输出:

                  a==b? false
                  c==d? true
                  

                  至于使用 int 值,您使用的是原始类型(考虑到您的方法也在其签名上使用原始类型) - 它会使用更少的内存并且可能更快,但是您例如,不能将其添加到收藏中.

                  As to using the int value, you are using the primitive type (considering your method also uses the primitive type on its signature) - it will use slightly less memory and might be faster, but you won't be ale to add it to collections, for instance.

                  如果您的方法的签名,还请查看 Java 的 AutoBoxing使用 Integer - 使用时,JVM 会自动为您调用 Integer.valueOf()(因此也使用缓存).

                  Also take a look at Java's AutoBoxing if your method's signature uses Integer- when using it, the JVM will automatically call Integer.valueOf() for you (therefore using the cache aswell).

                  这篇关于new Integer(123)、Integer.valueOf(123) 和 just 123 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的默认语言环境设置以使其保持一致?)

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

                    1. <legend id='gqsNI'><style id='gqsNI'><dir id='gqsNI'><q id='gqsNI'></q></dir></style></legend>
                        <bdo id='gqsNI'></bdo><ul id='gqsNI'></ul>
                            <tbody id='gqsNI'></tbody>

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