| Start | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Using underscore to improve readability Since Java 7 it has been possible to use one or more underscores (_) for separating groups of digits in a primitive number literal to improve their readability. For instance, these two declarations are equivalent: This can be applied to all primitive number literals as shown below: This also works using prefixes for binary, octal and hexadecimal bases: There are a few rules about underscores which forbid their placement in the following places:
|
||||||||||||||||||||
| Hexadecimal, Octal and Binary literals A An A All of these numbers result in the same value, Note that binary literal syntax was introduced in Java 7. The octal literal can easily be a trap for semantic errors. If you define a leading |
||||||||||||||||||||
| Boolean literals Boolean literals are the simplest of the literals in the Java programming language. The two possible |
||||||||||||||||||||
| Escape sequences in literals String and character literals provide an escape mechanism that allows express character codes that would otherwise not be allowed in the literal. An escape sequence consists of a backslash character ( The complete set of escape sequences is as follows:
The Note that a backslash followed by any other character is an invalid escape sequence. Invalid escape sequences are treated as compilation errors by the JLS. Reference: Unicode escapesIn addition to the string and character escape sequences described above, Java has a more general Unicode escaping mechanism, as defined in JLS 3.3. Unicode Escapes (opens new window). A Unicode escape has the following syntax: where A Unicode escape is mapped by the Java compiler to a character (strictly speaking a 16-bit Unicode code unit), and can be used anywhere in the source code where the mapped character is valid. It is commonly used in character and string literals when you need to represent a non-ASCII character in a literal. Escaping in regexesTBD |
||||||||||||||||||||
| Decimal Integer literals Integer literals provide values that can be used where you need a Ordinary integer literalsThe simplest and most common form of integer literal is a decimal integer literal. For example: You need to be careful with leading zeros. A leading zero causes an integer literal to be interpreted as octal not decimal. Integer literals are unsigned. If you see something like The range of integer literals of this form have an intrinsic type of Note that 231 is Long integer literalsLiterals of type Note that the distinction between |
||||||||||||||||||||
| String literals String literals provide the most convenient way to represent string values in Java source code. A String literal consists of:
For example: Note that a single string literal may not span multiple source code lines. It is a compilation error for a line-break (or the end of the source file) to occur before a literal's closing double-quote. For example: Long stringsIf you need a string that is too long to fit on a line, the conventional way to express it is to split it into multiple literals and use the concatenation operator ( An expression like the above consisting of string literals and Interning of string literalsWhen class file containing string literals is loaded by the JVM, the corresponding For more information on interning and the string pool, refer to the String pool and heap storage (opens new window) example in the Strings topic. |
||||||||||||||||||||
| The Null literal The Null literal (written as The null type is rather unusual. It has no name, so you cannot express it in Java source code. (And it has no runtime representation either.) The sole purpose of the null type is to be the type of Finally, |
||||||||||||||||||||
| Floating-point literals Floating point literals provide values that can be used where you need a
(The JLS syntax rules combine the two decimal forms into a single form. We treat them separately for ease of explanation.) There are distinct literal types for Simple decimal formsThe simplest form of floating point literal consists of one or more decimal digits and a decimal point ( For example In fact, decimal digits followed by a suffix is also a floating point literal. The meaning of a decimal literal is the IEEE floating point number that is closest to the infinite precision mathematical Real number denoted by the decimal floating point form. This conceptual value is converted to IEEE binary floating point representation using round to nearest. (The precise semantics of decimal conversion are specified in the javadocs for Scaled decimal formsScaled decimal forms consist of simple decimal with an exponent part introduced by an The size of a literal is limited by the representation ( Hexadecimal formsStarting with Java 6, it is possible to express floating point literals in hexadecimal. The hexadecimal form have an analogous syntax to the simple and scaled decimal forms with the following differences:
Here are some examples: Advice: since hexadecimal floating-point forms are unfamiliar to most Java programmers, it is advisable to use them sparingly. UnderscoresStarting with Java 7, underscores are permitted within the digit strings in all three forms of floating point literal. This applies to the "exponent" parts as well. See Using underscores to improve readability (opens new window). Special casesIt is a compilation error if a floating point literal denotes a number that is too large or too small to represent in the selected representation; i.e. if the number would overflow to +INF or -INF, or underflow to 0.0. However, it is legal for a literal to represent a non-zero denormalized number. The floating point literal syntax does not provide literal representations for IEEE 754 special values such as the INF and NaN values. If you need to express them in source code, the recommended way is to use the constants defined by the |
||||||||||||||||||||
| Character literals Character literals provide the most convenient way to express
For example: A line-break in a character literal is a compilation error: |
Read Article
https://develop.horje.com/learn/1434/reference