System.out.println("i1 - i2 = " + i1 - i2);
时间: 2024-06-01 09:11:00 浏览: 7
This code will not compile as there is a missing "+" operator between the string literal and the subtraction expression. The correct code should be:
System.out.println("i1 - i2 = " + (i1 - i2));
This will print the result of subtracting i2 from i1 after the string "i1 - i2 = ".
相关问题
Integer i1 = 40; Integer i2 = new Integer(40); System.out.println(i1==i2);
False.
Explanation:
The == operator in Java checks if two objects have the same memory address, meaning they are the exact same object. In this case, i1 and i2 are both Integer objects with the same value (40), but they are not the same object in memory.
When you use the new keyword to create an Integer object (as in i2 = new Integer(40)), Java creates a new object in memory with its own unique memory address.
Therefore, i1 and i2 have different memory addresses and are not the same object, so the == operator returns false.
分析下为什么,public Class Main{ public static void main(String[] args){ Integer i1 = 100; Integer i2 = 100; Integer i3 = 200; Integer i4 = 200; System.out.println(i1 == i2); //true System.out.println(i3 == i4); //false; } }
这是因为Java对于Integer类型的缓存机制,对于-128到127之间的整数,会进行缓存,所以i1和i2的值都在缓存范围内,所以它们的引用地址相同,而i3和i4的值超出了缓存范围,所以它们的引用地址不同。