TestNullOrEmpty.java
public class TestNullOrEmpty {
public static void main(String[] args) {
String value = null;
testNullOrEmpty(value);
value = "";
testNullOrEmpty(value);
value = " ";
testNullOrEmpty(value);
value = "hello me! ";
testNullOrEmpty(value);
}
static void testNullOrEmpty(String value) {
if (value == null ) { //正确的写法
System.out.println("value is null. ");
} else if ("".equals(value)) { //正确的写法
System.out.println("value is blank but not null. ");
} else {
System.out.println("value is /" " + value + "/" ");
}
if (value == "") { //NG 错误的写法
//别用这种写法
}
}
}
编译执行:
c:/>javac TestNullOrEmpty.java
c:/>java TestNullOrEmpty
value is null.
value is blank but not null.
value is " "
value is "hello me!"
java验证控制的方法
原文:http://www.cnblogs.com/wujixing/p/5200497.html