【问题标题】:how can java return false when comparing 2 numbers which has exactly the same type and value? [duplicate]比较两个具有完全相同类型和值的数字时,java如何返回false? [复制]
【发布时间】:2014-12-13 11:16:48
【问题描述】:
<br><% if (overLimit != null) {%><%=overLimitItemNO.getClass().equals(shoppingVO.getItem_no().getClass())%><%}%>
<br><% if (overLimit != null) {%><%=overLimitItemNO.getClass()%> vs <%=shoppingVO.getItem_no().getClass()%><%}%>
<br><% if (overLimit != null) {%><%=overLimitItemNO%> vs <%=shoppingVO.getItem_no()%><%}%> 
<br><% if (overLimit != null) {%><%=overLimitItemNO == shoppingVO.getItem_no()%><%}%> 

它会在浏览器上显示以下内容

true 

class java.lang.Integer vs class java.lang.Integer 

300008 vs 300008 

false

【问题讨论】:

  • 使用Integer时,您需要使用.equals方法。

标签: java jsp


【解决方案1】:

你需要使用 obj1.equals(obj2)。 == on objects 只比较两者是否是同一个确切的实例

【讨论】:

    【解决方案2】:

    在比较整数时使用 .equals,否则您正在比较对象引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-25
      • 2019-05-10
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2022-01-18
      相关资源
      最近更新 更多