简体   繁体   English

带注释的代码在Java中导致编译错误?

[英]Commented code gives compilation error in Java?

Hi I was creating simple program and got unseen compilation error in commented code.My code is as below : 嗨,我正在创建一个简单的程序,并在注释代码中出现了看不见的编译错误。我的代码如下:

public class Static_Method_Call
{               
    public static Character character=getMe();

    public static void main(String[] args)
    {
        System.out.println("Inside main() 1 : "+character); 
        //Static_Method_Call.character=new Character('\u000d'); 
        //System.out.println("Inside main() 2 : "+character);
    }

    static
    {
        System.out.println("Inside static block : "+character);
        Static_Method_Call.character=new Character('\u003d');       
    }

    public static Character getMe()
    {
        System.out.println("Inside getMe() : "+character);
        return new Character('\u002d');
    }
}

Error is as below : 错误如下:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Invalid character constant

What does this error mean in Java ? 此错误在Java中意味着什么?

\ is a Unicode character that stands for the CR special character. \ 是Unicode字符,代表CR特殊字符。 Even before the compiler transforms the source code, this character is pre-processed and causes the source code to be invalid. 甚至编译器转换源代码之前,此字符都会被预处理并导致源代码无效。 So I guess at pre-processing, the commented line would look something like: 所以我想在预处理时,注释行看起来像:

//Static_Method_Call.character=new Character('
 ');

Hence the compiler error. 因此,编译器错误。 You can use \\r to add a carriage return. 您可以使用\\r添加回车符。

\ is a newline character, so next line is starting with ' which is unclosed that is what it is complaining. \\ u000d是换行符,所以下一行以'开头,这是它所抱怨的。 this is explained here A unicode newline character(\ ) in Java 这是在这里解释的Java中的Unicode换行符(\\ u000d)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM