简体   繁体   English

在Java中,运算符是否对原始类型和原始包装器类执行相同的操作?

[英]In Java, do operators perform identically on primitive types and primitive wrapper classes?

As we all know, primitives in java have mathematical and logical operators you can use on them. 众所周知,java中的原语具有数学和逻辑运算符,您可以在它们上使用它们。 My question is does the same operational logic apply to their cousins, the primitive wrapper class. 我的问题是,相同的操作逻辑是否适用于他们的表亲(原始包装器类)。

Integer a = new Integer(2);
Integer b = new Integer(2);

Integer c = a * b;  //Does c.integerValue() returns 4?
boolean d = a == b; //Is d true? 
Integer e = a | c;  //Does e.integerValue() return 6?
Integer f = c % a;  //Does f.integerValue() return 0?
a++;                //Does a.integerValue() return 3?

Will all operators perform identically on primitive types and primitive wrapper classes? 所有运算符在原始类型和原始包装器类上的执行方式都一样吗? If not, what subset of operators work on both primitives and their Object wrappers? 如果不是,运算符的哪些子集可同时在原语及其对象包装器上工作?

Equality operators ( == and != ) are not reliable when working with wrapper classes. 使用包装器类时,等号运算符( ==!= )不可靠。

First, generally they compare object references, not object values. 首先,通常它们比较对象引用,而不是对象值。 For example: 例如:

Integer a = new Integer(24);
Integer b = new Integer(24);
System.out.println(a == b); // Prints false
System.out.println(a != b); // Prints true

Second, how the wrapper class is created matters, for example: 其次,如何创建包装器类很重要,例如:

Integer a = 24;
Integer b = 24;
System.out.println(a == b); // Prints true
System.out.println(a != b); // Prints false

In this case, when unboxed, Integer uses Integer.valueOf that in turn uses a cache ( IntegerCache ) for numbers between -128 and 127. That implementation is responsible for this weird behavior. 在这种情况下,当取消装箱时,Integer使用Integer.valueOf ,然后对-128到127之间的数字使用Integer.valueOfIntegerCache )。该实现是造成这种奇怪行为的原因。

Actually, the implementation of the IntegerCache class allows the upper bound to be configurable via the property java.lang.Integer.IntegerCache.high when you run your program. 实际上,在运行程序时, IntegerCache类的实现允许通过属性java.lang.Integer.IntegerCache.high配置上限。

This applies to Long also. 这也适用于Long

Lesson learned, you better use the equals() method with wrappers. 从中学到的东西,您最好将equals()方法与包装一起使用。

The rest of the operators should work because the value in the object is autoboxed before the operator is applied. 其余的运算符应该起作用,因为在应用运算符之前,对象中的值已自动装箱。

No, in particular == and != compare references. 不,特别是==!=比较引用。

This means that new Integer(1) != new Integer(1) 这意味着new Integer(1) != new Integer(1)

All other operations are basically the same. 所有其他操作基本相同。

In addition to above given answer I would like to add the following: Be careful when you do math operations on wrapped types. 除了上面给出的答案外,我还要添加以下内容:对包装类型进行数学运算时要小心。 Because you can get NullPointerException . 因为您可以获取NullPointerException Consider this example: 考虑以下示例:

    Double a = 5.0;
    Double b = null;

    System.out.println(a + b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM