Further research literal

Source: Internet
Author: User
Further study of the literal-Linux general technology-Linux programming and kernel information. The following is a detailed description. I briefly mentioned the literal in Chapter 2nd, and now I have already talked about the built-in types. Let's look at them further.

3.7.1 integer literal
Integers may be the most common type in typical programs. The value of any number is an integer literal. For example, 1, 2, 3, and 42. These are decimal values, which means that their description is based on the number 10. There are also two other hexadecimal values used by the integer literal, octal (octal, base is 8) and hexadecimal (hexadecimal, base is 16 ). In Java, the octal value is represented by adding a leading 0 before it. Normal decimal digits do not need leading zeros. In this way, it seems that the valid value 09 will generate an error from the compiler, because 9 is out of the octal range 0 ~ 7. A programmer is more commonly used for numbers in hexadecimal notation, which clearly matches the size of 8, such as 8, 16, 32, and 64-bit.

The leading 0x or 0X indicates a hexadecimal literal. The hexadecimal number ranges from 0 ~ 15. Use ~ F (or ~ F) to replace 10 ~ 15.

An integer literal generates an int value. In Java, It is a 32-bit integer. Since Java has strict type requirements, you may be wondering that assigning an integer literal to other integer types in Java, such as byte or long, does not produce Type Mismatch Errors, how is it possible. Fortunately, this problem is well solved. When a literal value is assigned to a byte or short variable, no error is generated if the literal value does not exceed the range of the corresponding type, A literal mentioned in Chapter 1 has now been described as a built-in type. Let's look at them further.

3.7.1 integer literal
Integers may be the most common type in typical programs. The value of any number is an integer literal. For example, 1, 2, 3, and 42. These are decimal values, which means that their description is based on the number 10. There are also two other hexadecimal values used by the integer literal, octal (octal, base is 8) and hexadecimal (hexadecimal, base is 16 ). In Java, the octal value is represented by adding a leading 0 before it. Normal decimal digits do not need leading zeros. In this way, it seems that the valid value 09 will generate an error from the compiler, because 9 is out of the octal range 0 ~ 7. A programmer is more commonly used for numbers in hexadecimal notation, which clearly matches the size of 8, such as 8, 16, 32, and 64-bit.

The leading 0x or 0X indicates a hexadecimal literal. The hexadecimal number ranges from 0 ~ 15. Use ~ F (or ~ F) to replace 10 ~ 15.

An integer literal generates an int value. In Java, It is a 32-bit integer. Since Java has strict type requirements, you may be wondering that assigning an integer literal to other integer types in Java, such as byte or long, does not produce Type Mismatch Errors, how is it possible. Fortunately, this problem is well solved. When a literal value is assigned to a byte or short type variable, no error is generated if the literal value does not exceed the range of the corresponding type. Therefore, an escape sequence is described.

Continue table

\ F form feed \ t horizontal tab \ B Return

3.7.5 string literal
The string literal in Java is the same as in most other languages-enclose a series of characters in double quotes. Example of a string:

"Hello World"
"Two \ nlines"
"\" This is in quotes \""

The character escape sequence defined for the string is the same as the octal/hexadecimal notation in the string. One important thing to note about Java strings is that they must start and end on the same line. Unlike other languages, there is a newline connection escape sequence.

Note: you may know that in most other languages, strings, including C/C ++, are implemented as character arrays. However, this is not the case in Java. In Java, strings are actually object types. Later in this book, you will see that Because Java implements strings as objects, it has extensive string processing capabilities and is both powerful and useful.
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.