Int,Long比较重使用equal替换==
发布时间:2020-12-14 06:21:57 所属栏目:Java 来源:网络整理
导读:首先,==有很多限制,如Integer 类型的值在[-128,127] 期间,Integer 用 “==”是可以的(),超过范围则不行,那么使用equal则代替则完全ok =4004L =4004l =4004 Integer prop3/span=4004span style="color: #000000;"gt;; /spanspan style="color: #0000ff;"g
首先,==有很多限制,如Integer 类型的值在[-128,127] 期间,Integer 用 “==”是可以的(),超过范围则不行,那么使用equal则代替则完全ok =4004L=4004l=4004
Integer prop3</span>=4004<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(prop2.equals(prop3)){
System.out.println(</span>"hi"<span style="color: #000000;">);
}</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(prop2==<span style="color: #000000;">prop3){
System.out.println(</span>"wrong"<span style="color: #000000;">);
}
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(prop1.equals(prop2.longValue())){
System.out.println(</span>"helloworld"<span style="color: #000000;">);
}</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(prop==<span style="color: #000000;">prop1){
System.out.println(</span>"you are wrong!"<span style="color: #000000;">);
}</span></pre>
返回结果 上面的示例说明使用"=="和equal还是有不小的区别的,equal可以替代== Long源码如下: true if and only if the argument is not
*
null and is a Long object that
* contains the same long value as this object.
*
* true if the objects are the same;
* false otherwise.
(obj value ==
Integer源码如下: true if and only if the argument is not
* null and is an Integer object that
* contains the same int value as this object.
*
* true if the objects are the same;
* false otherwise.
(obj value ==
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |