- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public class Wrap
{
public static void main(String args[])
{
Integer a =1000;
Integer b =1000;
System.out.println(a == b); //line 7
Integer c = 10;
Integer d = 10;
System.out.println(c == d); // line 10
}
}
理解部分:第7行正在比较引用变量是否引用同一个对象。第 10 行比较了包装器对象所保存的实际原始数据。
我发现:如果值在 -128 到 127 之间,它将比较原始值,否则检查引用变量是否引用同一对象
我的问题:为什么会这样?这里面有什么逻辑吗?这样的逻辑还有用吗?或者是它只是用一种神谕的方式来说明 java 与其他语言的不同之处。
最佳答案
Understood part: In line 7 it is comparing whether reference variable referring to same object or not.
正确
And in line 10 its comparing the actual primitive data that wrapper object holds.
不,它的作用完全相同。
What I found: If value is between -128 to 127 it will compare primitive value or else check whether reference variable is referring to same object
这是整数缓存的默认范围。对于其他整数类型和 boolean 值也是如此。
My question: Why is it so?
提高性能。
Is there any logic involved?
参见 Integer.valueOf(int)
Is that logic of any use?
节省垃圾。
Or is it just an oracle way to say how java is different from other languages.
也许,Sun的想法确实如此。
关于java - 包装器自动装箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17766633/
请告诉我哪种算法适用于以下问题: 在给定的 3 个月内,我们的项目数量有限(通常 < 50)。每个项目都有一定的小时数。 我们在同一 3 个月内拥有有限数量的资源(通常 < 100)。 每个资源每个月
我了解装箱和拆箱是关于强制转换(将实型转换为对象...将对象转换为实型)的。但是我不明白MSDN对Nullable的评价。这是我不明白的文字: When a nullable type is boxe
据我了解,Java 具有装箱(堆)和未装箱(堆栈)变量,如果我将装箱类型分配给未装箱类型,反之亦然,则涉及 (n) (un) 装箱成本。 拆箱比分配一个新的装箱对象便宜吗?如果以只读方式使用,盒装对象
装箱/拆箱和类型转换之间有什么区别? 这些术语似乎经常可以互换使用。 最佳答案 装箱是指将不可空值类型转换为引用类型或将值类型转换为其实现的某个接口(interface)(例如 int 到 IComp
给定一组项目,每个项目都有一个值,确定要包含在集合中的每个项目的数量,以使总值小于或等于给定限制,并且总值尽可能大。 例子: Product A = 4Product B = 3Product C =
我正在编写一个一维装箱程序。我只想要一个可能的垃圾箱。所以它不包括很多垃圾箱,它是唯一的一个。该程序仅搜索四边形组,如果四边形组不等于搜索数量则爆炸。我想搜索大于四边形的每个可能的组。在示例中,我们有
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我最近的代码包括很多 boxing and unboxing ,因为我的许多变量都是在运行时解析的。但是我读到装箱和拆箱在计算上非常昂贵,所以我想问一下是否还有其他方法可以对类型进行装箱/拆箱?这甚至
我的作业是装箱问题,要求我展示如何从优化版本解决问题的决策版本,反之亦然。我知道要解决决策版本,您只需获取优化版本中使用的 bin 数量并将其与指定的最大 bin 数量进行比较,但我如何使用决策版本来
这是在不使用外部库的情况下在 Java 中装箱/拆箱多维原始数组的最有效方法吗? private Float[][] toFloatArray(float[][] values) { Float
我有一个头疼的问题,感觉类似于经典的装箱问题,但我无法确定。任何帮助表示赞赏... 问题: 我有一组尺寸相同的产品,但有不同颜色的款式和不同的订单数量要求。 我可以任意组合包装,但我只能有规定数量的不
我有一个由 df.column.value_counts().sort_index() 生成的 Pandas 系列。 | N Months | Count | |------|------| |
大家可以向我解释一下 new 的性质以及 Integer 的使用 Integer i = new Integer(-10); Integer j = new Integer(-10); Integer
如果我错了,请纠正我,将值类型装箱转换为引用类型,那么为什么以下代码给出 10 输出而不是 12? public static void fun(Object obj) { o
如果我理解 CLR 装箱和处理可空值的方式,如 Boxing / Unboxing Nullable Types - Why this implementation? 中所述,我仍然有一些困惑。例如,
我经常使用 div 来装箱信息,但我注意到包装箱有时可能不会填满空间,这会导致来自其他包装器的元素进入箱区域。 例如: my info1 my Info2 看看这个例子 .topic {
目前我有这门课 public class Currency { private int _Amount; public Currency(){... } public Curr
我收集了 43 到 50 个数字,范围从 0.133 到 0.005(但大部分偏小)。如果可能的话,我想找到 L 和 R 之间总和非常接近的所有组合。* 蛮力法需要 243 到 250 步,这是不可行
我试图从性能的角度了解两种解决方案中的哪一种是首选。比如我有两段代码: 1) 装箱/拆箱 int val = 5; Session["key"] = val; int val2 = (int)Sess
通过 C# 在装箱/拆箱值类型上从 CLR 中提取 ... 关于装箱:如果可为空的实例不是null,CLR 会从可为空的实例中取出值并将其装箱。换句话说,值为 5 的 Nullable 装箱到值为
我是一名优秀的程序员,十分优秀!