【发布时间】:2012-02-02 11:36:17
【问题描述】:
这段代码怎么会显示编译错误-void 是变量 test 的无效类型
public class Tester{
public static void main(String[] args){
static void test(String str){
if (str == null | str.length() == 0) {
System.out.println("String is empty");
}
else {
System.out.println("String is not empty");
}}
test(null);
【问题讨论】:
-
将
test方法移出main方法 -
请勿将字符串与 == 进行比较,而应与 .equals() 进行比较。 Strings Re 对象,如果要比较对象的值就需要.equals(),否则就是比较引用。
-
如果其中一个操作数为 null == 有效,您应该使用它,因为如果您的 str 为 null,str.equals(null) 将抛出 NullPointerException -1 进行注释
标签: java static-methods