Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How unicode characters are read in java comments

Tags:

java

unicode

class Why
{
    public static void main(String[]s)
    {
        String st2="A";
        System.out.println(st2);
        // String st4="MN3444\u000ar4t4";
        System.out.println(st4);

    }
}

please compile the above code, i am getting the error in the comment line.

I am unable to understand this behaviour of the compiler, and what does this error mean ?

like image 797
Gagan93 Avatar asked Feb 14 '23 12:02

Gagan93


1 Answers

Before compilation every Unicode character is replaced by its value and since \u000a represents new line code

// String st4="MN3444\u000ar4t4";

is same as this code (notice that text after \u000a will be moved to new line, which means it will no longer be part of commented)

// String st4="MN3444
r4t4";

You can test it with

//\u000a;System.out.println("hello comment");

which is equal to

//
System.out.println("hello comment");

and will give you as result output: hello comment

like image 172
Pshemo Avatar answered Feb 17 '23 02:02

Pshemo