Class Charset
A named mapping between sequences of sixteen-bit Unicode code units and sequences of bytes. This class defines methods for creating decoders and encoders and for retrieving the various names associated with a charset. Instances of this class are immutable.
This class also defines static methods for testing whether a particular charset is supported, for locating charset instances by name, and for constructing a map that contains every charset for which support is available in the current Java virtual machine. Support for new charsets can be added via the service-provider interface defined in the CharsetProvider class.
All of the methods defined in this class are safe for use by multiple concurrent threads.
Charset names
- The uppercase letters ‘A’ through ‘Z’ ( ‘\u0041’ through ‘\u005a’ ),
- The lowercase letters ‘a’ through ‘z’ ( ‘\u0061’ through ‘\u007a’ ),
- The digits ‘0’ through ‘9’ ( ‘\u0030’ through ‘\u0039’ ),
- The dash character ‘-‘ ( ‘\u002d’ , HYPHEN-MINUS),
- The plus character ‘+’ ( ‘\u002b’ , PLUS SIGN),
- The period character ‘.’ ( ‘\u002e’ , FULL STOP),
- The colon character ‘:’ ( ‘\u003a’ , COLON), and
- The underscore character ‘_’ ( ‘\u005f’ , LOW LINE).
Every charset has a canonical name and may also have one or more aliases. The canonical name is returned by the name method of this class. Canonical names are, by convention, usually in upper case. The aliases of a charset are returned by the aliases method.
Some charsets have an historical name that is defined for compatibility with previous versions of the Java platform. A charset’s historical name is either its canonical name or one of its aliases. The historical name is returned by the getEncoding() methods of the InputStreamReader and OutputStreamWriter classes.
If a charset listed in the IANA Charset Registry is supported by an implementation of the Java platform then its canonical name must be the name listed in the registry. Many charsets are given more than one name in the registry, in which case the registry identifies one of the names as MIME-preferred. If a charset has more than one registry name then its canonical name must be the MIME-preferred name and the other names in the registry must be valid aliases. If a supported charset is not listed in the IANA registry then its canonical name must begin with one of the strings «X-» or «x-» .
The IANA charset registry does change over time, and so the canonical name and the aliases of a particular charset may also change over time. To ensure compatibility it is recommended that no alias ever be removed from a charset, and that if the canonical name of a charset is changed then its previous canonical name be made into an alias.
Standard charsets
Every implementation of the Java platform is required to support the following standard charsets. Consult the release documentation for your implementation to see if any other charsets are supported. The behavior of such optional charsets may differ between implementations.
Description of standard charsets
Charset Description US-ASCII Seven-bit ASCII, a.k.a. ISO646-US , a.k.a. the Basic Latin block of the Unicode character set ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1 UTF-8 Eight-bit UCS Transformation Format UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark
The UTF-8 charset is specified by RFC 2279; the transformation format upon which it is based is specified in Amendment 2 of ISO 10646-1 and is also described in the Unicode Standard.
The UTF-16 charsets are specified by RFC 2781; the transformation formats upon which they are based are specified in Amendment 1 of ISO 10646-1 and are also described in the Unicode Standard.
- When decoding, the UTF-16BE and UTF-16LE charsets interpret the initial byte-order marks as a ZERO-WIDTH NON-BREAKING SPACE; when encoding, they do not write byte-order marks.
- When decoding, the UTF-16 charset interprets the byte-order mark at the beginning of the input stream to indicate the byte-order of the stream but defaults to big-endian if there is no byte-order mark; when encoding, it uses big-endian byte order and writes a big-endian byte-order mark.
Every instance of the Java virtual machine has a default charset, which may or may not be one of the standard charsets. The default charset is determined during virtual-machine startup and typically depends upon the locale and charset being used by the underlying operating system.
The StandardCharsets class defines constants for each of the standard charsets.
Terminology
The name of this class is taken from the terms used in RFC 2278. In that document a charset is defined as the combination of one or more coded character sets and a character-encoding scheme. (This definition is confusing; some other software systems define charset as a synonym for coded character set.)
A coded character set is a mapping between a set of abstract characters and a set of integers. US-ASCII, ISO 8859-1, JIS X 0201, and Unicode are examples of coded character sets.
Some standards have defined a character set to be simply a set of abstract characters without an associated assigned numbering. An alphabet is an example of such a character set. However, the subtle distinction between character set and coded character set is rarely used in practice; the former has become a short form for the latter, including in the Java API specification.
A character-encoding scheme is a mapping between one or more coded character sets and a set of octet (eight-bit byte) sequences. UTF-8, UTF-16, ISO 2022, and EUC are examples of character-encoding schemes. Encoding schemes are often associated with a particular coded character set; UTF-8, for example, is used only to encode Unicode. Some schemes, however, are associated with multiple coded character sets; EUC, for example, can be used to encode characters in a variety of Asian coded character sets.
When a coded character set is used exclusively with a single character-encoding scheme then the corresponding charset is usually named for the coded character set; otherwise a charset is usually named for the encoding scheme and, possibly, the locale of the coded character sets that it supports. Hence US-ASCII is both the name of a coded character set and of the charset that encodes it, while EUC-JP is the name of the charset that encodes the JIS X 0201, JIS X 0208, and JIS X 0212 coded character sets for the Japanese language.
The native character encoding of the Java programming language is UTF-16. A charset in the Java platform therefore defines a mapping between sequences of sixteen-bit UTF-16 code units (that is, sequences of chars) and sequences of bytes.
How to Get and Set Default Character Encoding or Charset in Java?
Character encoding basically interprets a sequence of bytes into a string of specific characters. The same combination of bytes can denote different characters in different character encoding.
In what encoding is a Java char stored in?
«Stored» where? All Strings in Java are represented in UTF-16. When written to a file, sent across a network, or whatever else, it’s sent using whatever character encoding you specify.
Edit: Specifically for the char type, see the Character docs. Specifically: «The char data type . are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities.» Therefore, casting char to int will always give you a UTF-16 value if the char actually contains a character from that charset. If you just poked some random value into the char , it obviously won’t necessarily be a valid UTF-16 character, and likewise if you read the character in using a bad encoding. The docs go on to discuss how the supplementary UTF-16 characters can only be represented by an int , since char doesn’t have enough space to hold them, and if you’re operating at this level, it might be important to get familiar with those semantics.
A Java char is conventionally used to hold a Unicode code unit; i.e. a 16 bit unit that is part of a valid UTF-16 sequence. However, there is nothing to prevent an application from putting any 16 bit unsigned value into a char , irrespective of what it actually means.
So you could say that a Unicode code unit can be represented by a char and a char can represent a Unicode code unit . but neither of these is necessarily true, in the general case.
Your question about how a Java char is stored cannot be answered. Simply said, it depends on what you mean by «stored»:
- If you mean «represented in an executing program», then the answer is JVM implementation specific. (The char data type is typically represented as a 16 bit machine integer, though it may or may not be machine word aligned, depending on the specific context.)
- If you mean «stored in a file» or something like that, then the answer is entirely dependent on how the application chooses to store it.
Is the Java char type guaranteed to be stored in any particular encoding?
In the light of what I said above the answer is «No». In an executing application, it is up to the application to decide what a char means / contains. When a char is stored to a file, the application decides how it wants to store it and what on-disk representation it will use.
What about char literals? For example, ‘c’ must have some value that is defined by the language.
Java source code is required (by the language spec) to be Unicode text, represented in some character encoding that the tool chain understands; see the javac -encoding option. In theory, a character encoding could map the c in ‘c’ in your source code to something unexpected.
In practice though, the c will map to the Unicode lower-case C code-point (U+0063) and will be represented as the 16-bit unsigned value 0x0063.
To the extent that char literals have a meaning ascribed by the Java language, they represent (and are represented as) UTF-16 code units. Note that they may or may not be assigned Unicode code points («characters»). Some Unicode code points in the range U+0000 to U+FFFF are unassigned.
Originally, Java used UCS-2 internally; now it uses UTF-16. The two are virtually identical, except for D800 — DFFF, which are used in UTF-16 as part of the extended representation for larger characters.
Java String Encoding, Character encoding is a technique to convert text data into binary numbers. We can assign unique numeric values to specific characters and convert those numbers
How to Get and Set Default Character Encoding or Charset in Java?
Default Character encoding or Charset in Java is used by Java Virtual Machine (JVM) to convert bytes into a string of characters in the absence of file.encoding java system property. During JVM start-up, Java gets character encoding by calling System.getProperty(“file.encoding”,”UTF-8″) . In the absence of file.encoding attribute, Java uses “UTF-8” character encoding by default.
Character encoding basically interprets a sequence of bytes into a string of specific characters. The same combination of bytes can denote different characters in different character encoding. Therefore, the specification of the right character encoding plays an important role. Java caches character encoding in most of its major classes which requires character encoding. Therefore, calling System.setProperty(“file.encoding” , “UTF-16”) may not have desire effect while using InputStreamReader and other Java packages.
Getting default character encoding or Charset
There are various ways of retrieving the default charset in Java namely as follows:
- Using “file.encoding” system property
- Using java.nio.Charset
- Using Charset.defaultCharset() method
- “file.encoding” system property
- java.nio.Charset
- Code InputStreamReader.getEncoding()
Now let us brief about them before invoking them in the implementation part in order to get default character encoding or Charset
Method 1: “file.encoding” system property
System.getProperty(“file.encoding”) in Java returns the default charset that is used in the application, in case either the JVM is started with the -Dfile.encoding property or the JavaScript has not explicitly invoked the System.setProperty(“file.encoding, encoding) method, where the type of encoding is specified.
Method 2: java.nio.Charset
The java package provides a static method to retrieve the default character encoding for translating between bytes and Unicode characters. Charset.defaultCharset() method returns the default charset that is being used.
Method 3: Code InputStreamReader.getEncoding()
The package InputStreamReader in Java uses a method getEncoding() which returns the name of the character encoding used by thi