JavaScript是一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。在1995年时,由Netscape公司的Brendan Eich,在网景导航者浏览器上首次设计实现而成。因为Netscape与Sun合作,Netscape管理层希望它外观看起来像Java,因此取名为JavaScript。
1.“Unclosed String Literal”
当字符串结尾缺少引号时,会产生“unclosed string literal”错误消息,并且该消息就显示在出错的那一行上。
public abstract class NFLPlayersReference { private static Runningback[] nflplayersreference; private static Quarterback[] players; private static WideReceiver[] nflplayers; public static void main(String args[]){ Runningback r = new Runningback("Thomlinsion"); Quarterback q = new Quarterback("Tom Brady"); WideReceiver w = new WideReceiver("Steve Smith"); NFLPlayersReference[] NFLPlayersReference; Run();// { NFLPlayersReference = new NFLPlayersReference [3]; nflplayersreference[0] = r; players[1] = q; nflplayers[2] = w; for ( int i = 0; i < nflplayersreference.length; i++ ) { System.out.println("My name is " + " nflplayersreference[i].getName()); nflplayersreference[i].run(); nflplayersreference[i].run(); nflplayersreference[i].run(); System.out.println("NFL offensive threats have great running abilities!"); } } private static void Run() { System.out.println("Not yet implemented"); } }
通常,这种错误在以下这些情况下会产生:
- 字符串不是以引号结尾。这很容易修改,用指定的引号来结束字符串即可。
- 字符串超出一行。长字符串可以分成多个短串,并用加号(“+”)连接。
- 作为字符串一部分的引号没有使用反斜杠(“\”)来进行转义。
2.“Public Class XXX Should Be in File”
当XXX类和Java程序文件名不匹配时,就会产生“public class XXX should be in file”错误消息。 只有当类名和Java文件名相同时,才能编译代码。
package javaapplication3; public class Robot { int xlocation; int ylocation; String name; static int ccount = 0; public Robot(int xxlocation, int yylocation, String nname) { xlocation = xxlocation; ylocation = yylocation; name = nname; ccount++; } } public class JavaApplication1 { public static void main(String[] args) { robot firstRobot = new Robot(34,51,"yossi"); System.out.println("numebr of robots is now " + Robot.ccount); } }
要解决这个问题,可以:
- 把类和文件命名为相同的名字。
- 确保两个名称始终保持一致。
3.“Invalid Method Declaration; Return Type Required”
这个错误消息的意思是,在方法声明中未显示地声明方法的返回类型。
public class Circle{ private double radius; public CircleR(double r) { radius = r; } public diameter() { double d = radius * 2; return d; } }
有这几种情况会触发“invalid method declaration; return type required”错误:
- 忘记声明类型。
- 如果方法没有返回值,那么需要在方法声明中指定“void”作为返回类型。
- 构造函数不需要声明类型。但是,如果构造函数名称中存在错误,那么编译器会把构造函数看成是没有指定类型的方法。
4.“Variable Might Not Have Been Initialized”
在方法中声明的局部变量如果没有初始化,就会发生这种错误。如果在if语句中包含没有初始值的变量时,就会发生这种错误。
int x;if (condition) { x = 5; } System.out.println(x); // x可能尚未初始化
5.“Inconvertible Types”
当Java代码尝试执行非法转换时,会发生“Inconvertible Types”错误。
TypeInvocationConversionTest.java:12: inconvertible types found : java.util.ArrayList<java.lang.Class<? extends TypeInvocationConversionTest.Interface1>>required: java.util.ArrayList<java.lang.Class<?>> lessRestrictiveClassList = (ArrayList<Class<?>>) classList; ^
6.“Non-Static Variable … Cannot Be Referenced From a Static Context”
当编译器尝试在静态方法中访问非静态变量时,会发生此错误:
public class StaticTest { private int count=0; public static void main(String args[]) throws IOException { count++; //compiler error: non-static variable count cannot be referenced from a static context } }
要解决“Non-Static Variable … Cannot Be Referenced From a Static Context”这个错误,可以做两件事情:
- 可以将变量声明为静态。
- 可以在静态方法中创建非静态对象的实例。
7.和块作用域(block scope)有关的BUG
在大多数程序语言中,每一个函数块都有一个独立的新的作用域,但是在JavaScript中并不是。例如:
for (var i = 0; i < 10; i++) { /* ... */ } console.log(i); // 会输出什么呢?
通常在这种情况下,调用console.log()会输出undefined或则报错。不过呢,这里会输出10。在JavaScript中,即使for循环已经结束,变量i依然存在,并且记录最后的值。有些开发者会忘记这一点,然后导致许多bug。我们可以使用let而不是for来杜绝这一问题。
8.混淆的相等判断
JavaScript自动将所有在布尔环境下的变量类型转换为布尔类型,但是可能导致bug。举例:
// 所有都是true console.log(false == '0'); console.log(null == undefined); console.log(" \t\r\n" == 0); console.log('' == 0); // 注意:下面两个也是 if ({}) // … if ([]) // …
{}和[]都是对象,他们都会被转换为true。为了防止bug出现,推荐使用===和!==来做比较,因为不会隐式做类型转换。
总结: 在开发Java软件的时候经常会遇到各种各样的错误,但大多数可以避免。对待Bug 要有足够的耐心。对于Bug修复,每个人有自己的方式和做法,如果你有好的做法和建议,欢迎大家在评论区留言!