- 相關(guān)推薦
java常見(jiàn)的語(yǔ)法錯(cuò)誤有哪些
Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。那么java常見(jiàn)的語(yǔ)法錯(cuò)誤有哪些?以下僅供參考!
具體如下:
1.變量可能未初始化
錯(cuò)誤提示:Abc.java:9: variable i might not have been initialized
System.out.println(i);
中文說(shuō)明:變量i可能沒(méi)有賦值就使用了。
例子說(shuō)明:
int i;
System.out.println(i);
2.變量重復(fù)定義
錯(cuò)誤提示:Abc.java:9: i is already defined in main(java.lang.String[]) int i = 2;
中文說(shuō)明:變量重復(fù)定義了
例子說(shuō)明:int i = 1; int i = 2;
3. 找不到符號(hào):
Test6.java:26: cannot find symbol
symbol : variable j
location: class Test6
if(j < 0) {
4. 找不到類(lèi)的錯(cuò)誤
Main.java:4: cannot find symbol
symbol : class T1
location: class Main
T1 a = new T1();
5. 找不到方法的錯(cuò)誤
Main.java:5: cannot find symbol
symbol : method a()
location: class T a.a();
6.找不到類(lèi)
錯(cuò)誤提示:Test.java:1: class Test1 is public, should be declared in a file named Test1.java public class Test1 {
中文說(shuō)明:test1是公共的,必須在文件中聲明
例子說(shuō)明 :建一個(gè)文件為T(mén)est;在工具中打開(kāi)這樣寫(xiě)public class Test11 {}; 就會(huì)報(bào)這個(gè)錯(cuò)誤
7 找不到這個(gè)類(lèi)(類(lèi)名跟文件名不一致)
NoClassDefFoundError: asa (wrong name: ASA)
8. 數(shù)組下標(biāo)越界
java.lang.ArrayIndexOutOfBoundsException: 1
at Test2.test2(Test2.java:30)
at Test2.main(Test2.java:6)
9. 字符串下標(biāo)越界
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. 空指向E
xception in thread "main" java.lang.NullPointerException
at Next.main(Next.java:31)
11.空返回值
錯(cuò)誤提示:Test1.java:54: 'void' type not allowed here
System.out.println(a5.OnExit());
中文說(shuō)明;此處不允許使用void返回值
例子說(shuō)明:如果聲明一個(gè)void的方法,那就不能直接輸出來(lái)
Public static void edit() {}
System.out.println(Test.edit());
12. 缺少返回值
asa.java:8: missing return statement
int fan(){}
^
1 error
13. 沒(méi)有返回值的方法中不需要返回值
asa.java:10: cannot return a value from method whose result type is void return a;
^
1 error
14. 引用的方法入?yún)⒉粚?duì)
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. 無(wú)效的方法聲明(需要返回類(lèi)型)
invalid method declaration; return type required
public byteValue(){
17. 要求傳入的是數(shù)組,卻傳入了字符串
array required, but java.lang.String found
18 找不到構(gòu)造方法
Main.java:4: cannot find symbol
symbol : constructor T()
location: class T n
ew T();
19、數(shù)字格式化異常
Exception in thread "main" java.lang.NumberFormatException: null
20. 不兼容的類(lèi)型
錯(cuò)誤提示:Test1.java:41: incompatible types
found : java.lang.String[]
required: java.io.File[]
File [] a3 = a11.list();
中文說(shuō)明:不兼容的類(lèi)型
21. 非靜態(tài)方法不能在靜態(tài)上下文中引用
non-static method cannot be referenced from a static context
22. 不是靜態(tài)方法而用靜態(tài)方式調(diào)用(類(lèi)名。方法)
Main.java:5: non-static method fun1() cannot be referenced from a static context
Test.fun1();
23. 靜態(tài)訪問(wèn)非靜態(tài)(變量)
Test.java:5: non-static variable a cannot be referenced from a static context
a = 1000;
24. 靜態(tài)訪問(wèn)非靜態(tài)(方法)
Test.java:6: non-static method fun1() cannot be referenced from a static context
fun1(); // 靜態(tài)的不能調(diào)用非靜
25. continue outside of loop
(將continue放在for循環(huán)外的時(shí)候出現(xiàn)的錯(cuò)誤報(bào)告)
26.illegal start of expression
違反規(guī)則的表達(dá)(將for循環(huán)中第二表達(dá)放置于for循環(huán)外或內(nèi)部時(shí)出現(xiàn)的錯(cuò)誤報(bào)告)
27. asa.java:6: unreachable statement
不能到達(dá)的語(yǔ)句(語(yǔ)句放于continue或break后出現(xiàn)不能到達(dá),及continue和break后不能有語(yǔ)句)
28 break置于循環(huán)語(yǔ)句外asa.java:8: break outside switch or loop
break; ^ 1 error
29- 標(biāo)識(shí)符錯(cuò)誤(標(biāo)識(shí)符不合法);
【java常見(jiàn)的語(yǔ)法錯(cuò)誤有哪些】相關(guān)文章:
Java常見(jiàn)誤區(qū)與細(xì)節(jié)有哪些呢08-05
Java常用的語(yǔ)法錯(cuò)誤08-18
Java語(yǔ)言常見(jiàn)的知識(shí)點(diǎn)混淆問(wèn)題有哪些08-28
java有哪些優(yōu)勢(shì)10-28
常用java的命令有哪些07-18
java基礎(chǔ)語(yǔ)言有哪些09-12
java的Class類(lèi)有哪些10-01