Logic of the computer program (8)-the true meaning of char

Source: Internet
Author: User

A seemingly simple char

In the first two sections, we should have a clear understanding of character and text encoding and garbled, but the first two sections are not related to programming languages, we still do not know how to process characters and text in the program.

This section discusses the basics of character processing in Java-Char,java also have character, String, StringBuffer, StringBuilder and other classes for text processing, all based on Char, which we'll cover in subsequent articles.

Char looks simple, as we said in section 2nd, char is used to represent a character, which can be either Chinese or English characters. When assigning a value, enclose the constant character in single quotation marks, for example:

char c = ' A ';  Char z = ' Medium '; 

But we've thrown a question in section 3rd, why are character types also available for arithmetic operations and comparisons? What is the nature of char?

The nature of Char

When character processing is performed inside Java, Unicode is used, and the specific encoding format is utf-16be. In a nutshell, UTF-16 uses two or four bytes to represent a character, the Unicode numbering range is 65536 or two bytes, the out of range is four bytes, the BE (Big Endian) is the first high byte output, and then output the low byte, which is consistent with the memory representation of the integer.

Char is essentially a fixed two-byte unsigned positive integer that corresponds to the Unicode number that represents the character that corresponds to that Unicode number.

Because a fixed occupancy is two bytes, char can only represent characters that are less than 65536 in Unicode and cannot represent out-of-range characters.

How does the out-of-range character show? use two char. Class string has some related methods, which are described in subsequent articles.

On the basis of this understanding, we look at some of the behavior of char, it is relatively easy to understand.

Assignment of Char

Char has several ways of assigning values:

    1. char c = ' A '
    2. char c = ' horse '
    3. char C = 39532
    4. char C = 0x9a6c
    5. char c = ' \u9a6c '

The 1th method of assignment is the most common, assigned a character that can be expressed in ASCII code to a char variable.

The 2nd kind is also very common, but here is the Chinese characters, need to note that the direct write constant should pay attention to the file encoding, for example, GBK encoded code file opens according to UTF-8, the character will become garbled, the assignment value is according to the current Code interpretation Way, The Unicode number value corresponding to this character is assigned to the variable, and the corresponding Unicode number for ' ma ' is 39532, so the 2nd assignment is the same as the 3rd.

The 3rd type is to assign decimal constants directly to characters, and the 4th is to assign 16 binary constants to characters, and 5th to Unicode characters.

Above, the 2,3,4,5 is the same, the essence is the Unicode number 39532 is assigned to the character.

Arithmetic of Char

Since char is essentially an integer, some of the operations that can be done with integers are treated as int, but because char accounts for two bytes, the result of the operation cannot be assigned directly to the char type, which requires a forced type conversion, and Byte, Short participates in integer operations that are similar.

A comparison of a char type is a comparison of its Unicode numbers.

The addition and subtraction of char is calculated by its Unicode number, which makes no sense to add and subtract characters, but ASCII code characters are meaningful. For example, case conversion, uppercase A-Z number is 65-90, lowercase A-Z number is 97-122, just 32, so uppercase to lowercase only add 32, and lowercase to uppercase only minus 32. Another application of the addition and subtraction operation is encryption and decryption, which can be added and decrypted by some kind of reversible mathematical operation.

The bitwise operation of Char can be considered as the bitwise operation of the corresponding integer, except that it is an unsigned number, that is, the signed right shift >> and the unsigned right shift >>> result is the same.

Binary binary of Char

Since char is essentially an integer, look at the binary representation of char, and you can also use the integer method as follows:

char c = ' horse '; System.out.println (Integer.tobinarystring (c));

Output is 1001101001101100

Summary

This section describes the nature of char, which is fixed at two bytes, is actually an integer that represents the Unicode number of the character, the character not within the 65536 number is not represented by a char, and requires two char.

Let's review all the previous chapters and sort out the ideas.

We say that the so-called program, is mainly to tell the computer what data to do what the operation. In the 1th section we describe how to define data through variables, section 2nd describes the first operation of the data-assignment, and section 3rd describes the basic operations of the data, and the 4th section describes the binary representation and bitwise operation of the data.

At this point, we can define the basic data types and basic operations of the basic data, but the actual operation is not only the operation itself, we need to have a similar "if"/"so" logical mechanism, that is, according to the specific circumstances of the choice of execution mechanism, that is, process control.

Logic of the computer program (8)-the true meaning of char

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.