简体   繁体   中英

Using a unicode character in a .java file?

I want to set a unicode character in a class file like this:

TextView tv = ...;
tv.setText("·");

is there anything potentially wrong with using a unicode character in a .java file?

Thanks

No. Java strings support Unicode so you shouldn't run into any problems. You might have to check that the TextView class handles all the Unicode characters (which it should), but Java itself will handle the unicode characters.

You should also ensure that the file is saved with the correct encoding settings. Essentially this means that your editor should save the java file as UTF-8 encoded Unicode. See the comments to this answer for more details on this.

Is there anything potentially wrong with using a unicode character in a .java file?

As you know, Strings within the JVM are stored as Unicode - so the question is how to deal with Unicode in Java source files ...

In short, using Unicode is fine. There are a few ways to approach it ...

By default, the javac compiler expects the source file to be in the platform default encoding. This can be overridden using the -encoding flag:

-encoding encoding
Sets the source file encoding name, such as EUCJIS/SJIS/ISO8859-1/UTF8. If -encoding is not specified, the platform default converter is used.

Alternatively, if it's a single character (like it appears to be), you can keep your source file in your platform default encoding, and specify the character using the Unicode escape sequence:

tv.setText("\u1234");

... where '1234' is the Unicode value for the character you want.

Another alternative is to first save your file in your Unicode-compatible encoding (say UTF-8), then use native2ascii to convert that file to your native encoding (it will convert any out of range characters to the corresponding Unicode escape sequence).

NAME
native2ascii - native to ASCII converter

SYNOPSIS
native2ascii [ options ] [ inputfile [outputfile]]

DESCRIPTION
The Java compiler and other Java tools can only process files that contain Latin-1 or Unicode-encoded ( \? notation) characters. native2ascii converts files that contain other character encoding into files containing Latin-1 or Unicode-encoded charaters.

If outputfile is omitted, standard output is used for output. In addition, if inputfile is omitted, standard input is used for input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM