String sta_no=(String)request.getParameter("s_station");
out.print(sta_no);//结果都是aa,可是就是if判断却不等,去执行else的代码了?
if (sta_no.equals("aa"))
{ ..}
else
{..}
out.print(sta_no);//结果都是aa,可是就是if判断却不等,去执行else的代码了?
if (sta_no.equals("aa"))
{ ..}
else
{..}
调试欢乐多
“a == b”用于判断变量a和b是否引用到同一个对象,或者都是null。(Java里头除了内建类型的变量外,其他的都是引用)
“a.equals(b)”用于判断a所引用的串和b所引用的串是否包含相同的内容(只是内容相同,不一定是同一个串)。
不过,得到页面的参数后,最好是做必要的处理,以免不必要的麻烦
String sta_no=request.getParameter("s_station");
sta_no=(sta_no!=null&&!sta_no.equals(""))?sta_no.trim():"";if(sta_no.equals("aa")){
......
}else{
......
}
后面的版本不行了,不过得到参数后最好是做必要的处理,以免引起不必要的错误:
String sta_no=request.getParameter("s_station");
sta_no=(sta_no!=null&&!sta_no.equals(""))?sta_no.trim():"";
public static void main(String[] args){
if(a == b){
System.out.println("it's equal");
}
else
System.out.println("it's not equal");
}
private static String a="string";
private static String b="string";
}
所有相同的String指向同一地址。it's equal
btw:一般用常量去比,可以不用判断是否空值.
if ("aa".equals(sta_no))