The bytes occupied by char in java: 1. The char in the internal code in java is encoded in UTF16, and one char occupies two bytes; 2. The char in the foreign code in java is encoded in UTF8. One character occupies [1~6] bytes.
#Before discussing this issue, we need to distinguish between unicode and UTF.
unicode: A unified character number, which only provides mapping between characters and numbers. The number of symbols is constantly increasing and has exceeded one million. Details: [https://zh.wikipedia.org/zh-cn/Unicode]
UTF: unicode transformation format. Defines the encoding method of numbers in unicode. UTF8 and UTF16 are two of the implementation methods. Among them, utf8 is a variable-length representation, and the length may be 1 to 6 bytes; utf16 is a variable-length representation, and the length may be 2 or 4 bytes. Details: UTF8 [https://zh.wikipedia.org/zh-cn/UTF-8] UTF16 [https://zh.wikipedia.org/zh-cn/UTF-16]
Next, we need to distinguish between internal encoding and external encoding.
Inner code: The encoding method of char and string in memory when a certain language is running.
Outer code: Except for the inner code, all are outer codes.
It should be noted that the encoding method in the object code file (executable file or class file) generated by source code compilation belongs to foreign code.
Let’s take a look at the internal code first
The internal code in JVM uses UTF16. In the early days, UTF16 was encoded using a fixed-length 2-byte encoding. Two bytes can represent 65536 symbols (in fact, it can actually represent less than this), which was enough to represent all characters in Unicode at that time. However, with the increase of characters in Unicode, 2 bytes cannot represent all characters. UTF16 uses 2 bytes or 4 bytes to complete the encoding. To deal with this situation, Java uses a pair of char to represent characters that require 4 bytes, taking into account forward compatibility requirements. Therefore, char in Java takes up two bytes, but some characters require two chars to represent them.
Foreign code
Java's class file uses UTF8 to store characters, that is to say, the characters in the class occupy 1 to 6 bytes.
During Java serialization, characters are also encoded in UTF8, accounting for 1 to 6 characters.
Summary:
The char in the internal code (running memory) of Java is encoded using UTF16. One char occupies two bytes, but some characters require Represented by two chars. So, one character will occupy 2 or 4 bytes.
char in java Chinese and foreign code is encoded using UTF8, and one character occupies 1 to 6 bytes.
In UTF16 encoding, English characters occupy two bytes; most Chinese characters (especially commonly used Chinese characters) occupy two bytes, and individual Chinese characters (unicode-encoded Chinese characters will be added later) , usually rare words that are rarely used) occupy four bytes.
In UTF8 encoding, English characters occupy one byte; most Chinese characters occupy three bytes, and some Chinese characters occupy four bytes.
EOF
Related free learning recommendations: java basic tutorial
The above is the detailed content of How many bytes does char occupy in java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
