识别 Java 中的语法错误

发布于 2024-09-28 05:27:52 字数 318 浏览 5 评论 0原文

鉴于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

千紇 2024-10-05 05:27:52

但是上面的代码没有语法错误:

它们涉及简单的实例变量

除非您将它们声明为类的一部分,在其方法之外,否则它们不是实例变量。如果它们是在方法内声明的,则它们被称为局部变量,其作用范围为声明它们的代码块。


尝试向未初始化的数组添加任何内容,如中所示

String[] names;
names[0] = new String("mary");
names[1] = "John";

仍然会导致编译时错误,但这不是由于语法不正确,而是由于尝试操作未初始化的变量。您需要初始化它,例如,正如其他人所说,使用:

String[] names = new String[2];

如果数组充满了字符串,您可以索引其中一个并对其使用 length() 方法吗?

当然,这一行是完全合法的:

j = names[0].length();

并且相当于(假设您修复了 yada yada 上面的未初始化数组错误):

String firstElement = names[0];
j = firstElement.length();

There are no syntax errors in the above code, however:

they involve simple instance variables

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

String[] names;
names[0] = new String("mary");
names[1] = "John";

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:

String[] names = new String[2];

If the array is full of strings, can you index one out and use the length() method on it?

Sure, this line is perfectly legal:

j = names[0].length();

And is equivalent to (assuming you fix the uninitialized-array error above yada yada):

String firstElement = names[0];
j = firstElement.length();
南冥有猫 2024-10-05 05:27:52

names 变量尚未初始化为包含两个元素的数组。

String[] names;

应该是

String[] names = new String[2];

其余的代码看起来不错。

一些小注释:

  • new String("mary"); 在几乎所有情况下都相当于 "mary"

  • 您也可以像这样初始化 names 数组

    String[] 名称 = { "玛丽", "约翰" };
    
  • 创建数组后,大小是固定的。您不能向数组“添加元素”。您只能更新其中的值。 (对于可以增长的数组,我建议您查看ArrayList。)

The names variable has not been initialized to an array of two elements.

String[] names;

should be

String[] names = new String[2];

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 this

    String[] names = { "mary", "John" };
    
  • Once 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.)

池木 2024-10-05 05:27:52

没有语法错误。这是编译器错误,因为您的数组未初始化。

进一步的一点 - 初始化数组时,请确保它的初始化大小可以容纳您稍后尝试放入其中的数据。

现在很抱歉问了这个明显的问题,但是是什么阻止了您尝试编译此代码并查看错误是什么?它们具有足够的描述性 - 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:

The local variable names may not have been initialized

So to answer your question title (assuming it should be read "how to identify syntax errors in Java"):

Let the compiler do it.

烟花易冷人易散 2024-10-05 05:27:52

名称数组永远不会被创建。您需要将第2行:String[]名称;更改为String[]名称=新String[2];

The names array is never created. You need to change line 2: String[] names; into String[] names = new String[2];

眸中客 2024-10-05 05:27:52

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.

七七 2024-10-05 05:27:52

您会注意到每个人都说这不是语法错误。他们是正确的。语法错误是破坏编程语言的语法或形式的错误。这里有一个语义错误,即代码含义的错误。

将两者视为英语,语法与语法和拼写有关,而语义则处理实际含义。

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.

为你鎻心 2024-10-05 05:27:52

试试这个:

int i,j; 
String[] names = new String[2]; 
names[0] = "Mary"; 
names[1] = "John"; 
i = names.length; 
j = names[0].length(); 

你的代码不起作用的原因是因为在说String[]名称;时,你只是声明将有一个名称数组,但在Java中你需要明确地说您的数组将保存多少项或立即将值分配给数组......

String[] names = {"Mary", "John"};

以便初始化数组。

Try this:

int i,j; 
String[] names = new String[2]; 
names[0] = "Mary"; 
names[1] = "John"; 
i = names.length; 
j = names[0].length(); 

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...

String[] names = {"Mary", "John"};

in order to initialize the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文