1. Overview
1.概述
The “illegal start of expression” is a common error we may face at the compile-time.
“表达式的非法开始 “是我们在编译时可能面临的一个常见错误。
In this tutorial, we’ll see examples that illustrate the main causes of this error and how to fix it.
在本教程中,我们将看到一些例子,说明这个错误的主要原因和如何修复它。
2. Missing Curly Braces
2.缺少大括号
Missing curly braces may lead to the “illegal start of expression” error. Let’s take a look at an example first:
缺少大括号可能会导致 “表达式的非法开始 “的错误。让我们先看看一个例子。
package com.baeldung;
public class MissingCurlyBraces {
public void printSum(int x, int y) {
System.out.println("Calculation Result:" + calcSum(x, y));
public int calcSum(int x, int y) {
return x + y;
}
}
If we compile the above class:
如果我们编译上述类。
$ javac MissingCurlyBraces.java
MissingCurlyBraces.java:7: error: illegal start of expression
public int calcSum(int x, int y) {
^
MissingCurlyBraces.java:7: error: ';' expected
public int calcSum(int x, int y) {
.....
Missing the closing curly brace of printSum() is the root cause of the problem.
缺少printSum()的结尾大括号是问题的根本原因。
The fix to the problem is simple — adding the closing curly brace to the printSum() method:
问题的解决方法很简单–在printSum()方法中加入闭合的大括号。
package com.baeldung;
public class MissingCurlyBraces {
public void printSum(int x, int y) {
System.out.println("Calculation Result:" + calcSum(x, y));
}
public int calcSum(int x, int y) {
return x + y;
}
}
Before we step forward to the next section, let’s review the compiler error.
在我们迈向下一节之前,让我们回顾一下编译器的错误。
The compiler reports that the 7th line is causing the “illegal start of expression” error. In fact, we know that the root cause of the problem is in the 6th line. From this example, we learn that sometimes the compiler errors don’t point to the line with the root cause, and we’ll need to fix the syntax in the previous line.
编译器报告说,第7行导致了 “表达式非法开始 “的错误。事实上,我们知道问题的根本原因在第6行。从这个例子中,我们了解到有时编译器的错误并不指向有根本原因的那一行,我们需要修正前一行的语法。
3. Access Modifier Inside Method
3.方法内的访问修改器
In Java, we can only declare local variables inside a method or constructor. We cannot use any access modifier for local variables inside a method because their accessibilities are defined by the method scope.
在Java中,我们只能在方法或构造函数中声明局部变量。我们不能为方法中的局部变量使用任何access modifier,因为它们的可访问性是由方法范围定义的。
If we break the rule and have access modifiers inside a method, the “illegal start of expression” error will be raised.
如果我们打破规则,在一个方法里面有访问修饰符,就会产生 “表达式的非法开始 “错误。
Let’s see this in action:
让我们看看这个行动。
package com.baeldung;
public class AccessModifierInMethod {
public void printSum(int x, int y) {
private int sum = x + y;
System.out.println("Calculation Result:" + sum);
}
}
If we try compiling the above code, we’ll see the compilation error:
如果我们尝试编译上述代码,我们会看到编译错误。
$ javac AccessModifierInMethod.java
AccessModifierInMethod.java:5: error: illegal start of expression
private int sum = x + y;
^
1 error
Removing the private access modifier easily solves the problem:
移除private访问修饰符可以很容易地解决这个问题。
package com.baeldung;
public class AccessModifierInMethod {
public void printSum(int x, int y) {
int sum = x + y;
System.out.println("Calculation Result:" + sum);
}
}
4. Nested Methods
4.嵌套方法
Some programming languages, such as Python, support nested methods. But, Java doesn’t support a method inside another method.
一些编程语言,如Python,支持嵌套方法。但是,Java不支持一个方法在另一个方法里面。
We’ll face the “illegal start of expression” compiler error if we create nested methods:
如果我们创建嵌套方法,我们将面临 “表达式非法开始 “的编译器错误。
package com.baeldung;
public class NestedMethod {
public void printSum(int x, int y) {
System.out.println("Calculation Result:" + calcSum(x, y));
public int calcSum ( int x, int y) {
return x + y;
}
}
}
Let’s compile the above source file and see what the Java compiler reports:
我们来编译上述源文件,看看Java编译器的报告。
$ javac NestedMethod.java
NestedMethod.java:6: error: illegal start of expression
public int calcSum ( int x, int y) {
^
NestedMethod.java:6: error: ';' expected
public int calcSum ( int x, int y) {
^
NestedMethod.java:6: error: <identifier> expected
public int calcSum ( int x, int y) {
^
NestedMethod.java:6: error: not a statement
public int calcSum ( int x, int y) {
^
NestedMethod.java:6: error: ';' expected
public int calcSum ( int x, int y) {
^
5 errors
The Java compiler reports five compilation errors. In some cases, a single error can cause multiple further errors during compile time.
Java编译器报告了五个编译错误。在某些情况下,一个错误会在编译时引起多个进一步的错误。
Identifying the root cause is essential for us to be able to solve the problem. In this example, the first “illegal start of expression” error is the root cause.
找出根本原因对我们能够解决问题至关重要。在这个例子中,第一个 “表达式非法开始 “的错误是根本原因。
We can quickly solve the problem by moving the calcSum() method out of the printSum() method:
我们可以通过将calcSum()方法移出printSum()方法来快速解决这个问题。
package com.baeldung;
public class NestedMethod {
public void printSum(int x, int y) {
System.out.println("Calculation Result:" + calcSum(x, y));
}
public int calcSum ( int x, int y) {
return x + y;
}
}
5. char or String Without Quotes
5.char或String不带引号
We know that String literals should be wrapped in double quotes, while char values should be quoted using single quotes.
我们知道,String字头应该用双引号包裹,而char值应该用单引号引出。
If we forget to enclose these in the proper quotes, the Java compiler will treat them as variable names.
如果我们忘记用适当的引号括住这些东西,Java编译器会把它们当作变量名。
We may see “cannot find symbol” error if the “variable” is not declared.
如果 “变量 “没有被声明,我们可能会看到 “找不到符号 “的错误。
However, if we forget to double-quote a String that is not a valid Java variable name, the Java compiler will report the “illegal start of expression” error.
然而,如果我们忘记对不是有效的Java变量名的字符串进行双引号,Java编译器将报告 “表达式非法开始 “错误。
Let’s have a look at it through an example:
让我们通过一个例子来了解一下。
package com.baeldung;
public class ForgetQuoting {
public int calcSumOnly(int x, int y, String operation) {
if (operation.equals(+)) {
return x + y;
}
throw new UnsupportedOperationException("operation is not supported:" + operation);
}
}
We forgot to quote the String + inside the call to the equals method, and + is obviously not a valid Java variable name.
我们忘了在调用equals方法时引用String,而+显然不是一个有效的Java变量名。
Now, let’s try compiling it:
现在,让我们试着编译它。
$ javac ForgetQuoting.java
ForgetQuoting.java:5: error: illegal start of expression
if (operation.equals(+)) {
^
1 error
The solution to the problem is simple — wrapping String literals in double-quotes:
解决问题的方法很简单–用双引号包裹String字面。
package com.baeldung;
public class ForgetQuoting {
public int calcSumOnly(int x, int y, String operation) {
if (operation.equals("+")) {
return x + y;
}
throw new UnsupportedOperationException("operation is not supported:" + operation);
}
}
6. Conclusion
6.结论
In this short article, we talked about five different scenarios that will raise the “illegal start of expression” error.
在这篇短文中,我们谈到了会引发 “表达式的非法开始 “错误的五种不同情况。
Most of the time, when developing Java applications, we’ll use an IDE that warns us as errors are detected. Those nice IDE features can go a long way towards protecting us from facing this error.
大多数时候,在开发Java应用程序时,我们会使用一个IDE,当检测到错误时,它会向我们发出警告。那些漂亮的IDE功能可以在很大程度上保护我们不面临这种错误。
However, we may still encounter the error from time to time. Therefore, a good understanding of the error will help us to quickly locate and fix the error.
然而,我们仍然可能不时地遇到这个错误。因此,对该错误的充分了解将有助于我们快速定位和修复该错误。