Java uses UTF-16 encoding as the inner code, that is, within the JVM, the text is represented by a 16-bit code sequence, and the commonly used text is the contents of the character (char) and string literal constants. Note: UTF-16 is an encoding scheme for the Unicode character set.
Java characters and strings exist in the following places:
- Java source files, *.java, can be any character encoding, such as Gbk,utf-8
- class file, *.class, with an improved UTF-8 encoding (Modified UTF-8)
- JVM, using UTF-16 encoding in memory
The Java compiler needs to read the source code correctly, eliminate the coding differences, and then compile the UTF-8 encoded class file. For example Javac, by default it will take the operating system encoding, you can use the parameter-encoding specify the source code file character encoding. The JVM loads the class file and turns the character or string into a UTF-16 encoded sequence.
The classes involved in coding in Java are mainlyStringand the byte character conversion stream in the IO packet.String.getBytes()Encodes a string using the character set obtained at the start of the JVM, or you can use thegetBytes(charset)Specifies the character set; byte is pure 01, but the concept of character set must be in the characters, IO packetInputStreamReaderAndOutputStreamWriteris a bridge between byte stream and character stream, which is decoded and encoded by default using the JVM default character set, and can be specified by constructing a method.
"创";str.getBytes("UTF-8"// 3字节,0xE5889Bstr.getBytes("UTF-16"// 2字节,0x521BInputStreamReader(InputStream, charset);OutputStreamWriter(OutputStream, charset);
FAQ1. What are the main characters in Java?
The Java programming language mainly has the following kinds of characters:
- Whitespace characters: spaces, tabs, page breaks, and line terminators
- Note:/text/or//text
- Symbol
- Identifier: is the variable name and the class name, where the letters and numbers can be extracted from the Unicode character set, which means that identifiers can be written in the local language, such as
String 名字="cxcoder";
- Keywords: like class,new
- Literal constants: The representation of simple types, strings, and empty types in the source code
- Delimiter: Also known as punctuation,
() {} [] ; , . ... @
- Operators: Logical AND arithmetic operators
- Escape character
- Unicode escape character: Use any Unicode character by \u+4 hexadecimal number
- Literal constant escape character:
\b \t \n \f \r \" \‘ \\ It can also represent some special characters without using Unicode escape characters
2. Explain the output of the program
String Hello ="Hello", lo ="Lo";System out print(hello = = "Hello") ;//True A string literal constant always references the same instance of stringSystem out print(hello = = ("Hel" +"Lo")): //True constant expression, compile-time result, as literal constant treats System out print(hello = = ("Hel"+1o));//False run-time join operation produces new string objectSystem out println(hell0 = = ("Hel"+lo). Intern());//True to find if the constant pool has this string, there is a return, no put in, previously defined intern returns the same string instance
3. How is null in Java understood?
Null appears to be a keyword, but technically it is simply an empty literal constant that represents a null reference. Like true/false also just boolean literal constants.
?
How to understand that Java programs are written using the Unicode character set