<commit_before>---
layout: post
title: "What are Unicode escapes and how the Java compiler handles them"
excerpt:
modified: 2016-08-11 09:33:29 +0300
categories: articles
tags: [java, unicode]
image:
  feature:
  credit:
  creditlink:
comments: true
share: true
published: false
aging: true
---
Without saying anything else, I'm going to start this post with a simple code example.

{% highlight java %}
public class HelloUnicode {
    public static void main(String[] args) {
        // \u0055 is a Unicode escape for the capital U character (U)
        System.out.println("Hello \u0055nicode".length());
    }
}
{% endhighlight %}

Take a moment to think about what will be printed out? If you want, copy and paste the code to a new file, compile and run it. At first glance it looks like the program prints out `18`. There's 18 characters between the double quotes, so the length of the string should be 18. After reading the comment it should be reasonable to think that the Unicode escape will be replaced with a single character. If you run the code, you will see that the program prints out `13`. This proves the hypothesis that Unicode escape sequences are replaced with a single character inside a string literal.

Now equipped with the knowledge that Unicode escapes are replaced with their respective Unicode characters, let's look at the following example.

{% highlight java %}
public class NewLine {
    public static void main(String[] args) {
        // \u000A is a unicode escape for the line feed (LF)
        // \u0055 is a Unicode escape for the capital U character (U)
        System.out.println("Hello \u0055nicode".length());
    }
}
{% endhighlight %}

Can you guess what will be printed out now? The answer should be the same as before, right?. I'm sure some of you might suspect that this is a trick question and as a matter of fact, it is. This example will not compile at all.

{% highlight bash %}
$ javac NewLine.java
NewLine.java:3: error: ';' expected
        // \u000A is a unicode escape for the line feed (LF)
                      ^
NewLine.java:3: error: ';' expected
        // \u000A is a unicode escape for the line feed (LF)
                                     ^
NewLine.java:3: error: '(' expected
        // \u000A is a unicode escape for the line feed (LF)
                                         ^
NewLine.java:3: error: ';' expected
        // \u000A is a unicode escape for the line feed (LF)
                                                  ^
NewLine.java:3: error: ';' expected<commit_msg>improve readability in the first paragraph<commit_after>---
layout: post
title: "What are Unicode escapes and how the Java compiler handles them"
excerpt:
modified: 2016-08-11 09:33:29 +0300
categories: articles
tags: [java, unicode]
image:
  feature:
  credit:
  creditlink:
comments: true
share: true
published: false
aging: true
---
Without saying anything else, I'm going to start this post with a simple code example.

{% highlight java %}
public class HelloUnicode {
    public static void main(String[] args) {
        // \u0055 is a Unicode escape for the capital U character (U)
        System.out.println("Hello \u0055nicode".length());
    }
}
{% endhighlight %}

Take a moment to think about what will be printed out? If you want, copy and paste the code to a new file, compile and run it. At first glance it looks like the program prints out `18`. There's 18 characters between the double quotes, so the length of the string should be 18. But if you run the program, the output is `13`. As the comment suggests, the Unicode escape will be replaced with a single character.

Equipped with the knowledge that Unicode escapes are replaced with their respective Unicode characters, let's look at the following example.

{% highlight java %}
public class NewLine {
    public static void main(String[] args) {
        // \u000A is a unicode escape for the line feed (LF)
        // \u0055 is a Unicode escape for the capital U character (U)
        System.out.println("Hello \u0055nicode".length());
    }
}
{% endhighlight %}

Can you guess what will be printed out now? The answer should be the same as before, right?. I'm sure some of you might suspect that this is a trick question and as a matter of fact, it is. This example will not compile at all.

{% highlight bash %}
$ javac NewLine.java
NewLine.java:3: error: ';' expected
        // \u000A is a unicode escape for the line feed (LF)
                      ^
NewLine.java:3: error: ';' expected
        // \u000A is a unicode escape for the line feed (LF)
                                     ^
NewLine.java:3: error: '(' expected
        // \u000A is a unicode escape for the line feed (LF)
                                         ^
NewLine.java:3: error: ';' expected
        // \u000A is a unicode escape for the line feed (LF)
                                                  ^
NewLine.java:3: error: ';' expected
{'fraction_non_alphanumeric': 0.07993507810914993, 'fraction_numerical': 0.025157232704402517, 'mean_word_length': 2.68185212845407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7155101', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1259, 'n_words': 660}