java if语句不一致

| 我在bean中有一个“ 0”语句,当我创建一个测试java类时,该语句似乎可以正常运行,但是当由jsp调用bean时,它不能正常工作。 我的代码,让我向您展示: 一,测试类:
package com.serco.inquire;
import java.util.*;
import java.text.*;

public class TestCollection {
    public static void main(String[] args) {
        IrCollection myCollection = new IrCollection();
        myCollection.setSort(\"none\");
        myCollection.setMgrid(\"none\");
        int endpoint = myCollection.getSize();
        for (int i=0;i<endpoint;i++) {
            InquireRecord curRec = myCollection.getCurRecords(i);
            Long milis = new Long(curRec.getSubmitDate());
            Date theDate = new Date(milis);
            Format formatter = new SimpleDateFormat(\"dd MMM yyyy\");
            String s = formatter.format(theDate);
            System.out.println(\"ID: \" + curRec.getID() + \" | Subject: \" + curRec.getSubject());
        }
    }
}
它调用的IrCollection类中的代码段:
private void processSort(String datum) {
    int LastChar = datum.length()-1;
    String colName = datum.substring(0, LastChar);
    if (datum==\"none\") {
        this.fullSort  = \" ORDER BY lastUpdated DESC\";
    } else {
        if (datum.endsWith(\"2\")) {
            this.fullSort = \" ORDER BY \" + colName + \" ASC\";
        } else {
            this.fullSort = \" ORDER BY \" + colName + \" DESC\";
        }
    }
}
另一个方法中还有更多代码可使用以下方式调用此特定方法:
this.processSort(this.sort);
但是问题是第二个代码样本中的“ 4”部分。假定第一类的第10行将成员变量sort设置为
\"none\"
,则
processSort()
方法应将成员变量
fullSort
设置为
\" ORDER BY lastUpdated DESC\"
。 如果我在第一个示例中使用该类,它就会这样做。 然而 我有这个自定义标签:
<%@ tag body-content=\"scriptless\" import=\"com.serco.inquire.*\" %>
<%@ taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\" %>
<%@ attribute name=\"mgr\" required=\"true\" %>
<%@ attribute name=\"mkind\" required=\"false\" %>
<%@ attribute name=\"sort\" required=\"false\" %>
<c:if test=\"${empty mkind}\">
    <c:set var=\"mkind\" value=\"manager\" />
</c:if>
<c:if test=\"${empty sort}\">
    <c:set var=\"sort\" value=\"none\" />
</c:if>
<jsp:useBean id=\"irc\" scope=\"page\" class=\"com.serco.inquire.IrCollection\">
    <jsp:setProperty name=\"irc\" property=\"mgrtype\" value=\"${mkind}\" />
    <jsp:setProperty name=\"irc\" property=\"sort\" value=\"${sort}\" />
    <jsp:setProperty name=\"irc\" property=\"mgrid\" value=\"${mgr}\" />
</jsp:useBean>
${irc.fullsort}
.jsp文件通过以下哪个调用:
<c:set var=\"user\" value=\"none\" />    
<c:set var=\"sort\" value=\"none\" />
<inq:displayCollection>
    <jsp:attribute name=\"mgr\">${user}</jsp:attribute>
    <jsp:attribute name=\"mkind\">cotr</jsp:attribute>
    <jsp:attribute name=\"sort\">${sort}</jsp:attribute>
</inq:displayCollection>
换句话说,将完全相同的数据馈送到IrCollection bean。所以我应该得到相同的数据,对吗? 除了我得到这个:   WHERE COTR = \'none \'由非DES订购 因此,当Java调用它时,它会认为
\"none\" == \"none\"
,而当jsp调用它时,它会认为
\"none\" != \"none\"
。     
已邀请:
        您必须使用
equals
而不是
==
datum.equals(\"none\")
请参阅“ .equals和==”之间的区别是什么     
        基准== \“ none \”是错误的。你要
datum.equals(\"none\");
字符串==运算符仅比较指针位置,而不比较字符串的实际值。因此,它在某些情况下会起作用(如果基准面设置为常量字符串),但在动态创建时则无效。     
        使用equals进行字符串比较     
        我将更喜欢使用“ 17”方法,而不是比较参考变量,在相同情况下它可能会给出正确的结果,但是应该检查字符串在逻辑上是否相等。     
        我认为您的问题是Java中的对象相等性。就像其他人提到的那样,您应该使用
equals()
。您的类的编译器可能会为文字的每个实例创建一个共享字符串(例如\“ none \”)。这将导致
==
,如果从类中分配了字符串,它将检查两个变量是否指向同一对象。然后,当您使用JSP页面时,外部类会提供\“ none \”字符串,从而使它成为另一个对象。 这个故事的寓意是使用'equals()\'比较字符串。     

要回复问题请先登录注册