Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use chinese and japanese character as string in java?

Tags:

java

unicode

cjk

Hi
I am using java language. In this I have to use some chinese, japanese character as the string and print using System.out.println().

How can I do that?

Thanks

like image 868
sjain Avatar asked Nov 01 '10 09:11

sjain


People also ask

Can Java strings handle Unicode character strings?

Internally in Java all strings are kept in Unicode. Since not all text received from users or the outside world is in unicode, your application may have to convert from non-unicode to unicode.

Can UTF handle Japanese characters?

The Unicode Standard supports all of the CJK characters from JIS X 0208, JIS X 0212, JIS X 0221, or JIS X 0213, for example, and many more. This is true no matter which encoding form of Unicode is used: UTF-8, UTF-16, or UTF-32.

What is Unicode string in Java?

Unicode is a 16-bit character encoding system. The lowest value is \u0000 and the highest value is \uFFFF. UTF-8 is a variable width character encoding. UTF-8 has the ability to be as condensed as ASCII but can also contain any Unicode characters with some increase in the size of the file.

What encoding to use for Japanese characters?

Character encodings. There are several standard methods to encode Japanese characters for use on a computer, including JIS, Shift-JIS, EUC, and Unicode.


1 Answers

Java Strings support Unicode, so Chinese and Japanese is no problem. Other tools (such as text editors) and your OS shell probably need to be told about it, though.

When reading or printing Unicode data, you have to make sure that the console or stream also supports Unicode (otherwise it will likely be replaced with question marks).

Writer unicodeFileWriter = new OutputStreamWriter(
    new FileOutputStream("a.txt"), "UTF-8");
unicodeFileWriter.write("漢字");

You can embed Unicode literals directly in Java source code files, but you need to tell the compiler that the file is in UTF-8 (javac -encoding UTF-8)

String x = "漢字";

If you want to go wild, you can even use Chinese characters in method, variable, or class names. But that is against the naming conventions, and I would strongly discourage it at least for class names (because they need to be mapped to file names, and Unicode can cause problems there):

結果 漢字 = new 物().処理();
like image 69
Thilo Avatar answered Oct 31 '22 03:10

Thilo