0% found this document useful (0 votes)
13 views2 pages

5 Literals

The document explains Java literals, identifiers, and their representations, including boolean, character, numeric, and string data. It details how to denote octal, hexadecimal, binary, and floating-point literals, as well as the specific rules for assigning these literals to variables. Additionally, it clarifies the nature of boolean values in Java and the distinction between strings and character arrays in other programming languages.

Uploaded by

lalitpatil784636
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

5 Literals

The document explains Java literals, identifiers, and their representations, including boolean, character, numeric, and string data. It details how to denote octal, hexadecimal, binary, and floating-point literals, as well as the specific rules for assigning these literals to variables. Additionally, it clarifies the nature of boolean values in Java and the distinction between strings and character arrays in other programming languages.

Uploaded by

lalitpatil784636
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Literals: Java Literals are syntactic representations of boolean, character,

numeric, or string data.


Example: 4 is an integer literal.

Identifiers: Identifiers are the names of variables, methods, classes, packages and
interfaces

Octal values are denoted in Java by a leading zero. Normal decimal numbers cannot
have a leading zero.
Thus, the seemingly valid value 09 will produce an error from the compiler, since 9
is outside of octal’s 0 to 7 range.

You signify a hexadecimal constant with a leading zero-x, (0x or 0X).


The range of a hexadecimal digit is 0 to 15, so A through F (or a through f ) are
substituted for 10 through 15.

When a literal value is assigned to a byte or short variable, no error is generated


if the literal value is within the
range of the target type. An integer literal can always be assigned to a long
variable. However, to specify a long
literal, you will need to explicitly tell the compiler that the literal value is of
type long. You do this by appending
an upper- or lowercase L to the literal.

Beginning with JDK 7, you can also specify integer literals using binary. To do so,
prefix the value with 0b or 0B.
For example, this specifies the decimal value 10 using a binary literal: int x =
0b1010;

int x = 123_456_789;
the value given to x will be 123,456,789. The underscores will be ignored.

Floating-point literals in Java default to double precision.


To specify a float literal, you must append an F or f to the constant.

Hexadecimal floating-point literals are also supported, but they are rarely used.
They must be in a form similar to scientific notation, but a P or p, rather than an
E or e, is used.
For example, 0x12.2P2 is a valid floating-point literal. The value following the P,
called the binary exponent,
indicates the power-of-two by which the number is multiplied. Therefore, 0x12.2P2
represents 72.5.

double num = 9_423_497_862.0;


the value given to num will be 9,423,497,862.0
double num = 9_423_497.1_0_9;
In this case, the fractional part is .109

Boolean literals are simple. There are only two logical values that a boolean value
can have, true and false.
The values of true and false do not convert into any numerical representation.
The true literal in Java does not equal 1, nor does the false literal equal 0.

As you may know, in some other languages, including C/C++, strings are implemented
as arrays of characters.
However, this is not the case in Java. Strings are actually object types.
Because Java implements strings as objects, Java includes extensive string-handling
capabilities that are both powerful
and easy to use.
The remainder a % b is defined such that (a / b) * b + a % b is always equal to a :
System.out.println(-15 % 2); // -1
System.out.println(15 % -2); // 1

You might also like