Java Fundamentals  «Prev 

Literals as Tokens

Literals are tokens that represent constant values such as numbers and strings. Literals are the counterpart of variables, which act as storage containers for data and have a particular name associated with them. Literal data appears directly in code and does not have any name associated with it.
Consider the following code:

int age = 55;
String name = "Ernie";

In this example, 55 is a literal because it represents a constant number.
The constant string "Ernie" is also a literal.
Here is some code that assigns literal values to primitive numeric variables within their acceptable ranges.
byte num = 100;
short sum = 1240;
int total = 48764;
long population = 214748368;

The default type of a nondecimal number is int. To designate an integer literal value as a long value, add the suffix L or l (L in lowercase), as follows:
long fishInSea = 764398609800L;

Integer literal values

Integer literal values come in four flavors: binary, decimal, octal, and hexadecimal.
  1. Binary number system: A base-2 system, which uses only 2 digits, 0 and 1.
  2. Octal number system: A base-8 system, which uses digits 0 through 7 (a total of 8 digits). Here the decimal number 8 is represented as octal 10, decimal 9 as 11, and so on.
  3. Decimal number system: The base-10 number system that you use every day. It is based on 10 digits, from 0 through 9 (a total of 10 digits).
  4. Hexadecimal number system: A base-16 system, which uses digits 0 through 9 and the letters A through F (a total of 16 digits and letters).
    Here the number 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.
Converting an integer from decimal to octal
1) Converting an integer from decimal to octal

Converting an integer from decimal to hexadecimal
2) Converting an integer from decimal to hexadecimal

Converting an integer from decimal to binary
3) Converting an integer from decimal to binary