java常見語法錯誤
#語法錯誤就是不符合文法規則出現的問題,比方說你拿兩個字串進行乘法運算,當然就不對了,就是通常所說的病句。
下面為各位整理了java常見的文法錯誤,希望對java初學者有所幫助。
1.變數可能未初始化
錯誤提示:
Abc.java:9: variable i might not have been initialized System.out.println(i);
中文說明:變數i可能沒有賦值就使用了。
範例說明:
int i; System.out.println(i);
2.變數重複定義
#錯誤提示:
Abc.java:9: i is already defined in main(java.lang.String[]) int i = 2;
中文說明:變數重複定義了
範例說明:
int i = 1; int i = 2;
3. 找不到符號:
Test6.java:26: cannot find symbol symbol : variable j location: class Test6 if(j < 0) {
4. 找不到類別的錯誤
Main.java:4: cannot find symbol symbol : class T1 location: class Main T1 a = new T1();
5. 找不到方法的錯誤
Main.java:5: cannot find symbol symbol : method a() location: class T a.a();
#6. 找不到類別
##錯誤提示Test.java:1: class Test1 is public, should be declared in a file named Test1.java public class Test1 {
public class Test11 { }; 就會報這個錯誤
7 找不到這個類別(類別名稱跟檔名不一致)
NoClassDefFoundError: asa (wrong name: ASA)
8. 陣列下標越界
java.lang.ArrayIndexOutOfBoundsException: 1 at Test2.test2(Test2.java:30) at Test2.main(Test2.java:6)
9. 字串下標越界
java.lang.StringIndexOutOfBoundsException: String index out of range: 6 at java.lang.String.charAt(String.java:558) at Test2.test3(Test2.java:41) at Test2.main(Test2.java:7)
10. 空指向
Exception in thread "main" java.lang.NullPointerException at Next.main(Next.java:31)
11空回傳值
錯誤提示Test1.java:54: 'void' type not allowed here System.out.println(a5.deleteOnExit());
Public static void edit() {} System.out.println(Test.edit());
12 缺少傳回值
asa.java:8: missing return statement int fan(){} ^ 1 error
13 沒有傳回值的方法不需要傳回值#
asa.java:10: cannot return a value from method whose result type is void return a; ^ 1 error
14. 引用的方法入參不對
Next.java:66: cannot find symbol symbol : method createTempFile(java.lang.String,java.lang.String,java.lang.String) location: class java.io.File File ll = f.createTempFile("let","java","aaa");
15. .缺少形參詞
del() in sms.service.Service cannot be applied to (int)
#16.無效的方法宣告(需要回傳型別)
invalid method declaration; return type required public byteValue(){
17. 要求傳入的是數組,卻傳入了字串
array required, but java.lang.String found ^
#18找不到建構方法
Main.java:4: cannot find symbol symbol : constructor T() location: class T new T();
19、數位格式化例外 too large 定義值(整數)過大
21. 非靜態方法不能在靜態上下文中引用
Exception in thread "main" java.lang.NumberFormatException: null 20. .不兼容的类型 错误提示Test1.java:41: incompatible types found : java.lang.String[] required: java.io.File[] File [] a3 = a11.list(); 中文说明 不兼容的类型
22.不是靜態方法而用靜態方式呼叫(類別名稱。方法)
non-static method cannot be referenced from a static context
23靜態存取非靜態(變數)
Main.java:5: non-static method fun1() cannot be referenced from a static context Test.fun1();
24. 靜態存取非靜態(方法)
## Test.java:5: non-static variable a cannot be referenced from a static context
a = 1000;
26.illegal start of expression 違反規則的表達(將for迴圈中第二表達放在for迴圈外或內部時出現的錯誤報告)
27 asa.java:6: unreachable statement 不能到達的語句(語句放於continue或break後出
現不能到達,及continue與break後不能有語句)
28 break置於迴圈語句外
Test.java:6: non-static method fun1() cannot be referenced from a static context
fun1(); // 静态的不能调用非静
asa.java:8: break outside switch or loop
break;
^
1 error
MyAbstract.java:6: missing method body, or declare abstract
31. 這個類別不是有抽象類別 或沒有覆寫 重寫方法fun1() 抽象的方法的就必須是抽象類別
asa.java:2: <identifier> expected int %%; ^ 1 error
32. Myabstract 它是屬於抽象類別的,不能產生物件。
MyAbstract.java:1: MyAdstract is not abstract and does not override abstract method fun1() in MyAdstract
33. 介面的方法不能有方法體
Main.java:6: Myabstract is abstract; cannot be instantiated
34, .它是屬於抽象類別的,不能產生實體
MyInterface.java:2: interface methods cannot have body
35. 介面的方法不能有方法體
Myabstract is abstract; cannot be instantiated
36. 此處不允許使用static修飾
interface methods cannot have body
asa.java:3: modifier static not allowed here public static void main(String []args){ ^ ^
asa.java:4: inconvertible types found : java.lang.String required: int int b=(int)a; ^ 1 error
asa.java:5: ';' expected
asa.java:8: '}' expected
java.lang.NegativeArraySizeException
php中文網,大量的免費Java入門教學,歡迎線上學習!
以上是java常見語法錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!