English 中文(简体)
3. Integer. ValueOf()
原标题:The value of Integer.valueOf()

是否有理由使用Integer. ValueOf(X)进行最后分类,具体如下:

public class MyClass
{
  public static final Integer DAY_1 = Integer.valueOf(1); // Why do it this way?
  public static final Integer DAY_2 = 2; // When it can be done this way?
}

我的理解是,在添加汽车箱之前,在老版本的 Java中就有必要这样做。 是否仍然有理由采用这种守则? 或者这只是一种坏的习惯吗?

最佳回答
  • There s a lot of code written before 1.5 came out. There s no need to update it all without bringing any benefit.
  • In some cases it makes it clearer that you are boxing. In the case you gave you can easily see the target type on the same line - but that s not always the case.
  • If you want to call a method which has overloads for both Integer and int and you want to call the Integer overload, this is an easy way to do it.
问题回答

方案主管可以选择这样写字,以直观地强调,第1会议室是煽动(客观的),而不是暗中。

我不说我建议,但我可以想象有人出于这一原因采取这种做法。

除了Jon有这样的原因外,有些人根本不喜欢汽车(un)箱,时间不长。 许多人选择通过选择性地汇编错误或警告来绝对避免使用这种错误(口头上可能会将其变成错误或警告,例如)

如果是这样的话,除了首先使用外,别无选择,即便在这样的情况下,这种选择并不真正获得很多。

就汇编者而言,不存在任何区别(但对于超负荷的论点,应谨慎行事)。 排在一旁,排在二的表格被汇编者简单地转换为一排所用的表格。

对人类而言,可能有所不同。 我通常避免将汽车(un)箱作为防御性方案规划的行为,因为我认为,这种做法使我很容易忘记这一无端案例。 但确实,这要靠你们。

Autoboxing can lead to very subtle bugs that may be very difficult to find. Because of this, some IDEs have ability to generate a warning when any kind of boxing/unboxing is used. If you want to silence this warnings option 1 will do it for you.

因此,归根结底,所有这些都属于个人选择和项目编码标准。

在这种情况下,没有使用自动箱子的危险。

Integer. ValueOf(1)允许对共同价值进行分类;对于价值,从-128到128,它总是将同一目标退回,而新的Integer(1)将永远退回新的物体。 我要说的是,尽管这可能是掩体下正在做的自动化工作,但对所有衍生类型(Integer、Long、BigDecimal等)都采用这种做法是良好做法。

Bart van Heukelom, the differences between list.remove(1) and list.remove (new Integer(1)) is this; list.remove(1)将把指数1中的物体从清单中删除。 认为收藏品不能储存价格,只能是物品。

它是一种坏的习惯,没有理由这样做,因为汇编将为你制作<条码>。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签