Home > Articles

  • Print
  • + Share This

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 difference—the value is the same in decimal as it is in hexadecimal—but 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.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.