Share on Google+Share on Google+

Commenting out your code - Java Tutorials

commenting out java code

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 : 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 : unclosed character literal
^ <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 :


Reason :

To understand the reason, you need to look at the code just before the class Sample and Sample2 actually get compiled : Unicode newline character conversion)

public class Sample {
// Character a = new Character('
} Unicode newline character conversion)

public class Sample2{
// Character a = new Character('

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.


Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Commenting out your code - Java Tutorials  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image