- Data Types and Other Tokens
- Working with Variables
- The boolean Primitive
- The Flavors of Integer
- Operators
- Character Variables
- Floating-Point Variables
- Literals: Assigning Values
- Integer Literals
- Character Literals
- Floating-Point Literals
- String Literals
- Arrays
- Non-Token Input Elements
- Troubleshooting
Literals: Assigning Values
When you learned about assigning a literal value to a boolean variable, there were only two possibilities: the reserved words true and false. For integers, the values are nearly endless. In addition, there are many ways an integer value can be represented using literals.
The easiest way to assign a value to an integer value is by specifying a traditional numeral:
int j = 3;
However, what happens when you want to assign a number that is represented in a different form, such as hexadecimal? To tell the computer that you are giving it a hexadecimal number, you need to use a hexadecimal integer literal. For a number such as 3, this doesn't make much differencethe value is the same in decimal as it is in hexadecimalbut consider the number 11. Interpreted as hexadecimal (0x11), it has a value of 17! Certainly, you need a way to make sure the computer understands which you mean.
The following statements contain examples of assignments using literals:
boolean systemReady = true; int j = 0; long grainsOfSandOnTheBeach = 1L; short mask1 = 0x007f; char TIBETAN_NINE = '\u1049'; float accountBalance = 101.23F; String buttonLabel = "Cancel";
Clearly, there are several types of literals. In fact, there are six major types of literals in the Java language:
-
Boolean
-
Integer
-
Character
-
Floating-point
-
String
-
Null
Note - Similar to the Boolean literals true and false, null is known as the null literal. It is a special value assigned to a reference type variable to indicate that the variable does not refer to any object.
You've already learned about the two Boolean literals, so take a look at the other types in the following sections.