Did you know? Programmers convert coffee to code.

If you like my articles, sponsor me a coffee.

At the other end of the link I post some results of my other hobbies, like playing bass. It is worth checking it out ;)

A little Java trick

I accidentally run into this code and well, it gave me some headache.

You probably won’t meet such coding errors but it is good to see something like this too.

Take a look at the following simple Java code:

 * @author GHajba
public class ChangeCalculator {

    static int getChange(final int price, final int bankNote) {
        // example: \u000d price = 100;
        return bankNote - price;

Well, it does not seem peculiar but let’s compile it (any Java version should be fine):

ChangeCalculator.java:8: error: final parameter price may not be assigned
        // example: \u000d price = 100;
1 error

Not so good. What’s the matter with the compiler?

The solution

The problem is with the unicode character. If you search after it on the Internet you will find that it is the unicode carriage return character.

While Java is parsing your code it will see that “Hey, this is a CR, let’s start a new line!”. And because of this the code

price = 100;

will get on a new line outside of the comment and now you get the compile error because price is declared as a final argument.

Take case when you are using special unicode character sequences in your comments. They may lead to different errors you won’t think of.

Share the knowledge!

Senior developer, consultant, author, mentor, apprentice. I love to share my knowledge and insights what I achieve through my daily work which is not trivial -- at least not for me.

Click Here to Leave a Comment Below

%d bloggers like this: