编译结果:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable myString may not have been initialized
说myString没有初始化对象。myString.length();执行错误。
改正
- public static void main(String[] args)
- {
- String myString="";
- int x = 100;
-
- if (x < 100) myString = "x is less than 100";
- if (x > 100) myString = "x is greater than 100";
- System.out.println(myString.length());
- }
复制代码
执行结果:0,编译通过
|