在这段代码(story * 2) == tail中得到了True
并false为distance + 1 != tail.
== 检查参考,因为Long是不可变的,它对于两个不同的对象将是假的,
这里story * 2引用的值相等tail,但它们是两个不同的对象,而不是池的编译时常量.
public class Test2
{
public static void main(String [] args) {
Long tail = 2000L;
Long distance = 1999L;
Long story = 1000L;
System.out.println(tail > distance);
System.out.println((story * 2) == tail);
if((tail > distance) ^ ((story * 2) == tail))
System.out.print("1");
System.out.println(distance + 1 != tail);
System.out.println((story * 2) == distance);
if((distance + 1 != tail) ^ ((story * 2) == distance))
System.out.print("2");
}
Run Code Online (Sandbox Code Playgroud)
当您对包装的基元(例如Long)执行算术运算时,它们会自动解包为原始基元(例如long).
考虑以下:
(story * 2) == tail
Run Code Online (Sandbox Code Playgroud)
首先,story是自动拆箱成long,并乘以二.要将结果long与右侧进行比较Long,后者也会自动取消装箱.
这里没有比较参考文献.
以下代码演示了这一点:
public static void main(String[] args) {
Long tail = 2000L;
Long story = 1000L;
System.out.println((story * 2) == tail); // prints true
System.out.println(new Long(story * 2) == tail); // prints false
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
185 次 |
| 最近记录: |