识别 Java 中的语法错误
鉴于 Java 中的这段代码:
int i,j;
String[] names;
names[0] = new String("mary");
names[1] = "John";
i = names.length;
j = names[0].length();
我需要找到错误。据我所知,第 1、2、4 和 5 行是正确的,因为它们涉及简单的实例变量、向数组添加元素以及查找数组的长度。然而第3行和第6行很奇怪。
你能像第 3 行那样将字符串添加到数组中吗?如果数组充满了字符串,您可以索引其中一个并对其使用 length() 方法吗?
Given this code in Java:
int i,j;
String[] names;
names[0] = new String("mary");
names[1] = "John";
i = names.length;
j = names[0].length();
I need to find the error. As far as I can tell, lines 1, 2, 4, and 5 are correct because they involve simple instance variables, adding elements to arrays, and finding the length of an array. However lines 3 and 6 are weird.
Can you add a string to an array like the way done in line 3? If the array is full of strings, can you index one out and use the length() method on it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
但是上面的代码没有语法错误:
除非您将它们声明为类的一部分,在其方法之外,否则它们不是实例变量。如果它们是在方法内声明的,则它们被称为局部变量,其作用范围为声明它们的代码块。
尝试向未初始化的数组添加任何内容,如中所示
仍然会导致编译时错误,但这不是由于语法不正确,而是由于尝试操作未初始化的变量。您需要初始化它,例如,正如其他人所说,使用:
当然,这一行是完全合法的:
并且相当于(假设您修复了 yada yada 上面的未初始化数组错误):
There are no syntax errors in the above code, however:
They're not instance variables unless you declare them as part of a class, outside its methods. If they're declared within methods, they're called local variables to the scope of whichever code blocks they're declared.
Trying to add anything to an uninitialized array, as in
Will still cause a compile-time error, however it's not due to incorrect syntax but an attempt to manipulate an uninitialized variable. You need to initialize it, so for example as others have said, use:
Sure, this line is perfectly legal:
And is equivalent to (assuming you fix the uninitialized-array error above yada yada):
names
变量尚未初始化为包含两个元素的数组。应该是
其余的代码看起来不错。
一些小注释:
new String("mary");
在几乎所有情况下都相当于"mary"
您也可以像这样初始化
names
数组创建数组后,大小是固定的。您不能向数组“添加元素”。您只能更新其中的值。 (对于可以增长的数组,我建议您查看
ArrayList
。)The
names
variable has not been initialized to an array of two elements.should be
The rest of the code looks good.
Some minor remarks:
new String("mary");
is in almost all situations equivalent to just"mary"
You could also initialize the
names
-array like thisOnce the array is created, the size is fixed. You can't "add elements" to the array. You can only update values in it. (For an array that can grow, I suggest you have a look at
ArrayList
.)没有语法错误。这是编译器错误,因为您的数组未初始化。
进一步的一点 - 初始化数组时,请确保它的初始化大小可以容纳您稍后尝试放入其中的数据。
现在很抱歉问了这个明显的问题,但是是什么阻止了您尝试编译此代码并查看错误是什么?它们具有足够的描述性 - eclipse 编译器说:
所以回答你的问题标题(假设应该阅读“如何识别Java中的语法错误”):
让编译器来做。
There is no syntax error. It's a compiler error, because your array is not initialized.
A further point - when initializing the array make sure it is initialized with a size that can hold the data you are trying to put in it later.
Now sorry for asking the obvious question, but what stopped your from trying to compile this code and see what the errors are? They are descriptive enough - the eclipse compiler says:
So to answer your question title (assuming it should be read "how to identify syntax errors in Java"):
Let the compiler do it.
名称数组永远不会被创建。您需要将第2行:
String[]名称;
更改为String[]名称=新String[2];
The names array is never created. You need to change line 2:
String[] names;
intoString[] names = new String[2];
names
变量永远不会被初始化。尝试为其元素赋值将导致编译器错误。但严格来说,这不是语法错误。The
names
variable is never initialized. Attempting to assign values to its elements will cause a compiler error. But it's not a syntax error, strictly speaking.您会注意到每个人都说这不是语法错误。他们是正确的。语法错误是破坏编程语言的语法或形式的错误。这里有一个语义错误,即代码含义的错误。
将两者视为英语,语法与语法和拼写有关,而语义则处理实际含义。
You'll notice that everyone says that this isn't a syntax error. And they are correct. A syntax error is an error that breaks, well, the syntax or form of the programming language. What you have here is a SEMANTIC error, an error in the meaning of the code.
Think of the two as in English, syntax has to do with grammar and spelling, whereas semantics deal with the actual meaning.
试试这个:
你的代码不起作用的原因是因为在说
String[]名称;
时,你只是声明将有一个名称数组,但在Java中你需要明确地说您的数组将保存多少项或立即将值分配给数组......以便初始化数组。
Try this:
The reason your code wasn't working was because in saying
String[] names;
you were only declaring that there was going to be an array of names but in Java you need to either explicitly say how many items your array will hold or immediately assign the values to the array as such...in order to initialize the array.