Commenting Erroneous Code & Unicode newline Correct
In this section, you will find an interesting problem related to commenting erroneous code which have Unicode in it. We will discuss about error's cause and what should be its possible solution.
First you need to get a look of the code:
public class Sample{ Character a= new Character('\u000d'); }
When you compile the above code, you will get an error something like this :
Sample.java:2: illegal line end in character
literal Character a = new Character('\u000d'); ^ 1 error |
Now, we will comment it so that at least it will get compile :
public class Sample { // Character a = new Character('\u000d'); }
When we try to compile it, we will get the following error :
Sample.java:3: unclosed character literal } ^ Sample.java:3: <identifier> expected } ^ 2 errors |
But the given below code compile :
public class Sample2 { // Character a = new Character('\u000d{System.out.println("Hello");} }
Amazingly the above code compile without any hassle. Now, we try to run it using another class :
public class Sample2Test { public static void main(String[] args) { new Sample2(); } }
The Sample2Test runs and compile successfully and gives the following output :
Hello |
Reason :
To understand the reason, you need to look at the code just before the class Sample and Sample2 actually get compiled :
Sample.java(After Unicode newline character conversion)
public class Sample { // Character a = new Character(' '); }
Sample2.java(After Unicode newline character conversion)
public class Sample2{ // Character a = new Character(' {System.out.println("Hello");} }
As you can see the Unicode newline character(\u000d) change the code by create a new line where it was placed. That's why it gives error. While in the case of Sample2 class it is still correct after creating new line (where it was placed).
I think it will give you a clear idea that the precision must be taken before using Unicode newline character.