What does the Java compiler error message “

2020-04-07 03:10发布

class if{
    public static void main (String args[]){
        int x = 9;
        if (x <= 9){
            System.out.println("Yay");
        }else{
            System.out.println("Yay");
            }
        }
    }

I'm running this from the compiler, using Notepad++ as the text editor. And I am getting an error in the compiler saying <identifier> expected class if. And another error saying illegal start of expression. As well as saying error ";" expected. I have a total of 9 errors.

I made sure to match all the {} and (). Even scraped the program and tried again with the same results.

5条回答
Animai°情兽
2楼-- · 2020-04-07 03:21

Also, it's (String[] args)

Not (String args[])

查看更多
Juvenile、少年°
3楼-- · 2020-04-07 03:33

You shouldn't call a class "if". It's a reserved Java keyword (that you're using in your program, BTW).

Furthermore, by convention, all classes start with an uppercase letter in Java.

查看更多
唯我独甜
4楼-- · 2020-04-07 03:35

You cannot name your class or even a variable with a keyword.

查看更多
做自己的国王
5楼-- · 2020-04-07 03:37

You can't name your class if, as it's a keyword. Check this for more examples.

查看更多
ら.Afraid
6楼-- · 2020-04-07 03:41

if is a reserved keyword in Java (as seen in your if statement), and is thus not an eligible class name. Choose another name for your class, like IfTesting.

By convention, all class names start with an upper-case letter. The full details for what is and isn't a valid Java identifier are found in the Java Language Specification. In short, it can't be a keyword, true, false, or null.

查看更多
登录 后发表回答