- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家可以向我解释一下 new 的性质以及 Integer 的使用
Integer i = new Integer(-10);
Integer j = new Integer(-10);
Integer k = -10;
Integer l=-10;
System.out.println(i==j);
System.out.println(k==l);
答案是假的
下一个
Integer a=128;
Integer b=128;
Integer c=127;
Integer d=127;
System.out.println(a==b);
System.out.println(c==d);
我得到的答案是假的。谁能解释一下这种性质。预先感谢:)
最佳答案
在第一个示例中,您总是创建新的Integer
对象并将这些引用分配给i
和j
.对于k
和l
,您使用的是自动装箱,它有时会创建新对象,而有时则不会。
在第二个示例中,您只是使用自动装箱 - 但具有不同的值,这演示了上面的“有时”。
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly. For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.
This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.
关于java - 使用新的与正常的减速和整数的性质来声明变量(装箱/拆箱),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11736047/
Eclipse 坚持在我的项目中启用 JSF 方面。我可以很容易地禁用它,但是当我通过 Eclipse 运行 Maven -> Update Project 时,facet(以及关联的 WEB-INF
我在 Linux 下从 eclipse 导出了一个 SWT 项目,并尝试在 Windows 下导入它。到目前为止,一切都工作正常,除了我似乎失去了该项目的“SWT 性质”。使用 SWT gui 运行项
我有以下项目结构: root-gradle (build.gradle) 项目组 1(无构建文件) project1 (build.gradle) project2 (build.gradle) ..
我正在K8s集群上试验Spark2.3。想知道检查点如何工作?它存储在哪里?如果主驱动程序死了,那么现有处理会怎样? 在从卡夫卡消费时,补偿如何保持?我试图在线查找,但找不到这些问题的任何答案。我们的
我有一个 springboot 项目,它被组织为多个模块。我正在使用 Eclipse 2019-06(版本 4.12)和 jdk 1.8 update 131 和 apache maven 3.3.9
我是一名优秀的程序员,十分优秀!