Home >Java >JavaBase >The difference between characters and bytes in java

The difference between characters and bytes in java

王林
王林Original
2019-11-12 14:37:372975browse

The difference between characters and bytes in java

byte (byte):

byte means byte. It is the basic data type in java and is used to declare bytes. Type variable, a byte contains 8 bits, so the value range of the byte type is -128 to 127.

Usually when reading non-text files (such as pictures, sounds, executable files), you need to use byte arrays to save the contents of the files. When downloading files, you also use byte arrays as temporary buffer reception. File content, so byte is essential in file operations.

In some programs (especially those related to hardware), certain data will be stored in byte type variables, such as 00110010, where each bit represents a parameter, and then bit operations are performed Method to obtain and assign values ​​to parameters.

Characters:

The machine only knows bytes, but characters are semantic units. They are encoded. One character may be encoded into 1, 2 Even 3 4 bytes. This is related to the character set encoding. English letters and numbers are single-byte, but characters in natural languages ​​such as Chinese characters are multi-byte. One byte can only represent 255 characters, and it cannot be used to process so many natural languages ​​around the world, so multi-byte storage is definitely needed.

So in the input and output of files, InputStream and OutputStream deal with byte streams, which means that everything is assumed to be binary bytes; while Reader and Writer are character streams, which involve characters. Set problem; according to the ANSI coding standard, punctuation marks, numbers, and uppercase and lowercase letters all occupy one byte, and Chinese characters occupy 2 bytes. According to the UNICODE standard, all characters occupy 2 bytes.

Recommended tutorial: Java tutorial

The above is the detailed content of The difference between characters and bytes in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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