Java Basics-Literals

Source: Internet
Author: User

In Java source code, literals are used to represent fixed values (fixed value). Numeric literals are the most common, and string literals can be considered as a type, and special nulls can also be used as literals. Literals can be broadly categorized as integer literals, floating-point literals, character and string literals, and special literals.

1. Integer literals

Formally, the literal value of an integer is categorized as an integer literal. For example: Ten, 100000L, ' B ', 0XFF these can all be called literal values. Integer literals can be expressed in decimal, 16, 8, and 2 notation. Decimal is very simple, 2, 8, 16 binary representation in the front plus 0B (0b), 0, 0X (0x) can, of course, the base can not go beyond the scope of the system, such as 09 is not legal, octal base only to 7. In general, literals create an int type, but int literals can be assigned to a byte short char long int, and as long as the literal value is within the target range, Java automatically completes the conversion. If you attempt to assign an out-of-range literal to a type (such as assigning 128 to a byte type), the compilation is not. If you want to create a long type that cannot be represented by an int type, you need to add either L or L to the literal value. It is generally recommended to use an easily distinguishable L. Therefore, integer literals include int literals and a long literal value of two.

2. Floating-point literals

A simple understanding of floating-point literals can be understood as decimals. is divided into float literal and double literal, if you add F or F after the decimal, it is a float literal, such as 11.8F. If the decimal is not followed by F (f), such as 10.4. Or a decimal followed by D (d) indicates that it is a double literal. In addition, floating-point literals support the representation of scientific and technical law. Here are some examples:

Double D1 = Ten ;  
double D2 = 11.4; double d3 = 1.23E3; double d4 = 10D; double d5 = 0.4D; float f1 = ten; float F2 = 11.1F; float f3 = 1.23e-4f; float f4 = 1.23e0f;

3 characters and string literals

Character literals in Java are enclosed in single quotes, such as ' @ ' 1 '. All UTF-16 character sets are included in the character literal. Characters that cannot be entered directly can use escape characters, such as ' \ n ' for newline characters. You can also use octal or hexadecimal notation for characters, and octal to use backslashes with 3-digit digits, such as ' \141 ' for letter A. Hexadecimal uses \u plus 4 for 16 decimal digits, such as ' \u0061 ' to denote character a. That is, by using the escape character, you can represent all or none of the characters on the keyboard. A common sequence of escape characters is:

\DDD (octal), \uxxxx (hexadecimal Unicode character), \ ' (single quotation mark), \ "(double quote), \ \ (backslash) \ r (carriage return) \ n (newline character) \f (page break) \ t (tab) \b (Back to Geff)

String literals use double quotation marks, which in string literals can also contain sequences of escape characters in character literals. The string must be on the same line or use the + operator because Java does not have a continuation escape sequence.

4 Special literals

Null is a special type that can be assigned to any reference type variable, indicating that the variable does not refer to anything. If a reference type variable is NULL, it indicates that the variable is not available.

There is also a special class literal, denoted by the type name plus a. class, such as String.class. First, string is an instance (object) of Class Java.lang.Class, and "This is a string" is an object of class string. Class literal is then used to represent an object of class classes, such as the object string String.class used to represent class classes. To put it simply, class literal is a literal value such as String.class, Integer.class, which represents the tired string, the class Integer. If the output integer.class, you will get class Java.lang.Integer. The output of the List.class is interface java.util.List. In summary, class literal is used to represent the type itself!

5 using underscores in numeric literals

Beginning with JDK7, you can insert one or more underscores in numeric literals, including integer literals and floating-point literals. However, the underscore can only be used to separate numbers, cannot delimit characters and characters, and cannot delimit characters and numbers. For example, int x = 123_456_789. When compiling, the underscore is automatically removed. You can use underscores continuously, such as float f = 1.22___33__44. The literal value of binary or hexadecimal can also be underlined, remembering that underscores can only be used between numbers and numbers, and are illegal for the first time. For example, 1._23 is illegal, _123 and 11000_l are illegal.

Java Basics-Literals

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.