Home 类库下载 java类库 Details and usage notes of char and String in Java

Details and usage notes of char and String in Java

Nov 07, 2016 pm 05:18 PM
java string

Note on the use of char data type

In Java, the char data type is used to represent characters, but the char type cannot represent all characters.

Unicode character set

First of all, we need to know that we use the Unicode character set in Java. Before its appearance, there were many character sets, such as ANSI, GB2312, etc. Since there are many character sets with different standards, this leads to two problems:

  • For any given code value, different characters may correspond to different characters in different character sets;

  • Using large character sets Languages ​​may have different encoding lengths, such as single-byte encoding for common characters and multi-byte encoding for other characters.

The emergence of the Unicode character set is to unify encoding and eliminate the above problems. The so-called character set is a collection composed of many different characters. The Unicode character set assigns a unique code point to each character to identify the character itself. The so-called code point is a hexadecimal integer with the U+ prefix added. For example, the code point of the letter A is U+0041.

With the Unicode character set, what we have to consider is how to transmit and store these characters. This is how Unicode encoding is implemented, which we call Unicode Transformation Format (UTF). The familiar UTF-8, UTF-16, etc. are different Unicode encoding implementation methods.

At the beginning of the birth of the Unicode character set, the fixed-length encoding method UCS-2 (2-byte Universal Character Set) was used to encode the Unicode character set. This method uses a length of 16 bits for character encoding, so Up to 2^16 = 65536 characters can be encoded (encoding range from U+0000 ~ U+FFFF). Under the circumstances, the designers used less than half of the number to encode all characters, and believed that the remaining space was sufficient for encoding new characters in the future.

Unfortunately, with the continuous addition of Chinese, Japanese, Korean and other ideographic characters, the number of characters in the Unicode character set quickly exceeded the maximum number of characters that can be encoded by 16 bits, so designers made changes to the Unicode character set. New design.

The new design divides all characters in the character set into 17 code planes. Among them, the code point range U+0000 ~ U+FFFF is designated as the Basic Multilingual Plane (abbreviated as BMP). The remaining characters are divided into 16 auxiliary planes (Supplementary Plane). The code point range is U +10000 ~ U+10FFFF, these characters in the auxiliary plane are called supplementary characters.

After the characters in the Unicode character set are reclassified into different planes, you need to pay attention to the following two aspects:

  • The characters in the BMP range are basically the same as the character encoding under UCS-2, but the U+ in BMP The D800 ~ U+DFFF part is left blank and is not assigned to any characters. It is used to encode characters in the auxiliary plane.

  • Not every position in each plane is assigned to the specified character, the reason is:

  • Special purposes, such as the U+D800 ~ U+DFFF part in BMP;

  • As reserved space

  • There are not enough characters

UTF-16

UTF-16 also uses 16 bit encoding to represent Unicode characters, that is to say UTF-16 code unit (code unit) is 16 bits. Code unit refers to the most basic unit of character encoding, that is, any character must be composed of n (n≥1) code units.

Under UTF-16, since the 16-bit length can only represent 65536 characters, all characters within the BMP range are mapped by default, so the U+D800 ~ U+DFFF part is left blank, then the auxiliary plane Characters can also be expressed with the help of this blank part. This is the ingenuity of UTF-16 design, which solves the encoding problem of all characters without wasting space.

So how to express the characters of the auxiliary plane? In fact, the code points of the auxiliary plane characters are encoded into a pair of 16-bit long code units, called a surrogate pair, and the surrogate pair must fall in the U+D800 ~ U+DFFF part of the BMP. This solves the problem of encoding the entire Unicode character set with 16-bit code units. It should be noted that the part U+D800 ~ U+DFFF can be called the agent area, among which the part U+D800 ~ U+DBFF is called the high-level agent area (leading agent area), and the part U+DC00 ~ U+DFFF It is called the low-level agent area (the rear agent area).

The following explains the encoding method of auxiliary plane characters through an example of UTF-16 encoding U+64321, a character in the auxiliary plane.

First subtract 0x10000 from the code point of this character to obtain a value with a length of 20 bits. The range of this value must be within 0x0000 ~ 0xFFFF.

Details and usage notes of char and String in Java

Use the value of the high-order 10 bits of Vx as the operation base Vh of the high-order agent, and use the value of the low-order 10 bits as the operation base Vl of the low-order agent. The value range of these two 10-bit values ​​must be between 0x0000 ~ 0x3FF.

Details and usage notes of char and String in Java

Perform a bitwise OR operation on Vh and Vl with the code points at the starting position of the high-order surrogate area and low-order surrogate area respectively. The result is the UTF-16 encoding of the character U+64321 in the auxiliary plane.

Details and usage notes of char and String in Java

So in the end the character U+64321 is encoded into a surrogate pair consisting of a high-order surrogate and a low-order surrogate. We need to use both 0xD950 and 0xDF21 to represent this character.

Through the above example, we can see that any character in the auxiliary plane will be encoded as a surrogate pair consisting of two 16-bit surrogate codes under UTF-16. When representing this character in the program, you need It no longer takes up 16 bits of space, but 32 bits.

It is not recommended to use the char data type in Java programs

After the above explanation of the Unicode character set and UTF-16, we will now discuss why it is not recommended to use the char data type in Java programs.

Since Java uses a 16-bit Unicode character set, that is, UTF-16, the char data type in Java is fixed-length, and its length is always only 16 bits. The char data type can only represent code points in U+ Characters between 0000 ~ U+FFFF, that is, characters in BMP. If the code point exceeds this range, even if supplementary characters are used, the char data type will not be supported, because the supplementary characters require a length of 32 bits to store, and we can only use String to store this character.

Details and usage notes of char and String in Java

The code written above uses the char data type to save the characters of the auxiliary plane, and the compiler will report an error of Invalid character constant.

With the continuous increase of Internet users and the continuous enrichment of Internet languages, users are increasingly using some special characters on the Internet to express rich semantics, and these characters are likely to be supplementary characters in the auxiliary plane. , so if we use the char type for processing, it is likely to reduce the robustness of our program.

String details

Get the string length

String is a data type that we use a lot in programming, it is used to represent a string. Looking at the source code of String, we can see that the underlying layer actually uses a char type array to store our characters.

Details and usage notes of char and String in Java

We also know that calling its length() method can get the length of the string, that is, the number of characters in the string. Its implementation is to directly return the length of the underlying value array. The code is as follows:

Details and usage notes of char and String in Java

Combined with our knowledge of character encoding above, we know that the length of char in Java is always 16 bits. If we use supplementary characters in the string, it means that we need 2 char type lengths to store. For the String bottom layer For the array value that stores characters, 2 array element positions are needed. So in the following program we will get an unexpected result:

Details and usage notes of char and String in Java

According to our idea, there should be only 8 characters in the string tt, but the actual output is 9. We have already mentioned above that Java uses a 16-bit Unicode character set, so the length of a code unit in Java is also 16 bits. A supplementary character requires two code units to represent, so the tt character in the string

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles