在终端中编译 Java 会导致字符串中出现非法字符错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29719591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:47:09  来源:igfitidea点击:

Compiling Java in terminal gives illegal character errors within a string

javastring

提问by David Griffith

I am new and just trying to do the most basic of all, simple 'Hello World'

我是新手,只是想做最基本的,简单的“Hello World”

Here is the code, The same you find anywhere:

这是代码,您可以在任何地方找到相同的代码:

class HelloWorld {
    public static void main (String args[]) {
        System.out.println(“Hello World!”);
    }
}

When I run in the terminal (I use a Mac) I am posed with the following errors:

当我在终端中运行时(我使用 Mac),我遇到了以下错误:

HelloWorld.java:3: error: illegal character: '\u201c'
    System.out.println(“Hello World!”);
                       ^
HelloWorld.java:3: error: ';' expected
        System.out.println(“Hello World!”);
                        ^
HelloWorld.java:3: error: not a statement
        System.out.println(“Hello World!”);
                                  ^
HelloWorld.java:3: error: ';' expected
        System.out.println(“Hello World!”);
                                       ^
HelloWorld.java:3: error: illegal character: '\u201d'
        System.out.println(“Hello World!”);
                                        ^
5 errors

What is going on here? All the errors are the string!

这里发生了什么?所有的错误都是字符串!

回答by Mureinik

is the wrong character to use to denote a string - you should use ".

是用于表示字符串的错误字符 - 您应该使用".

回答by Masudul

“”is not valid String, use "". At your code, Change from

“”不是有效的字符串,请使用"". 在您的代码中,从

 System.out.println(“Hello World!”);

To,

到,

System.out.println("Hello World!");

回答by nipuna777

“ is different from " in Java.

“与 Java 中的”不同。

You need to replace “Hello World!” with "Hello World!"

您需要替换“Hello World!” 与“你好世界!”