Programming Geek
Rated 4.1/5 based on 446 reviews

Understanding Importance of Unicode in JAVA


Understanding Importance of Unicode in JAVA

Unicode is a character set which allows to represent character of almost all language. Unicode uses 16 bit to represent character of any language. It’s typical representation starts with’\u’. For an example, the Unicode representation of ‘\n’ is ‘\u000A’ .

In JAVA, char takes 2 bytes memory because JAVA supports Unicode and allows character of any language to be used in JAVA program.

Let us first understand the way java program works.

 The source file contains java program. We complie the program and get .class file containing the bytecode. This bytecode is plateform independent and architecture neutral and can be run by any device having  jvm.

 So what happens when we compile a java program?

When we write “javac  ClassName.java” then before compilation all Unicode codes are replaced by their equivalent characters i. e. like C the java program is pre-processed and during this pre-processing all Unicode code is replaced by equivalent character and then compilation phase starts.

The following programs illustrate the concept of Unicode in JAVA:



/**
 *
 * @author VIK 01:48 10 March 2013
 */
public class UnicodeDemo1 {
    public static void main(String[] args) {
    //This line causes problem \u000A because of unicode preprocessing
        System.out.println("This program won't compile!!!");
    }
    
}

The Detailed explanation of above program can be found here.
/**
 *
 * @author VIK VIKKU VIKASHVVERMA
 */
public class UnicodeDemo2 {
    public static void main(String[] args) {
        /*
         * This program also causes compilation error.
         * Becuse this file is saved in c:\units\vikashvverma\stuffs
         * and have improperly formatted unicode code.
         *So While using unicode remeber to format unicode properly
         *          * 
         */
        System.out.println("This is an example java program having improperly formatted unicode !!!");
    }
}

Ok now you are aware of using Unicode in java program.  Can you write a java program without using any keyword and print something e.g.  “Vikash verma” .
Getting problem…? Ok stay updated for next post.