How many bytes does char occupy in java?
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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

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
