Writing Literals – Building Blocks – 1Z0-829 Study Guide

Writing Literals

There are a few more things you should know about numeric primitives. When a number is present in the code, it is called a literal. By default, Java assumes you are defining an int value with a numeric literal. In the following example, the number listed is bigger than what fits in an int. Remember, you aren’t expected to memorize the maximum value for an int. The exam will include it in the question if it comes up.

long max = 3123456789; // DOES NOT COMPILE

Java complains the number is out of range. And it is—­for an int. However, we don’t have an int. The solution is to add the character L to the number:

long max = 3123456789L; // Now Java knows it is a long

Alternatively, you could add a lowercase l to the number. But please use the uppercase L.

The lowercase l looks like the number 1.

Another way to specify numbers is to change the “base.” When you learned how to count, you studied the digits 0–9. This numbering system is called base 10 since there are 10 pos-sible values for each digit. It is also known as the decimal number system. Java allows you to specify digits in several other formats:

Octal (digits 0–7), which uses the number 0 as a prefix—­for example, 017.

Hexadecimal (digits 0–9 and letters A–F/a–f), which uses 0x or 0X as a prefix—­for example, 0xFF, 0xff, 0XFf. Hexadecimal is case insensitive, so all of these examples mean the same value.

Binary (digits 0–1), which uses the number 0 followed by b or B as a prefix—­for example, 0b10, 0B10.

You won’t need to convert between number systems on the exam. You’ll have to recog-nize valid literal values that can be assigned to numbers.

Literals and the Underscore Character

The last thing you need to know about numeric literals is that you can have underscores in numbers to make them easier to read:

int million1 = 1000000;

int million2 = 1_000_000;

We’d rather be reading the latter one because the zeros don’t run together. You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point. You can even place multiple underscore charac-ters next to each other, although we don’t recommend it.

Let’s look at a few examples:

double notAtStart = _1000.00; double notAtEnd = 1000.00_; double notByDecimal = 1000_.00; double annoyingButLegal = 1_00_0.0_0; double reallyUgly = 1__________2;

  • DOES NOT COMPILE
  • DOES NOT COMPILE
  • DOES NOT COMPILE
  • Ugly, but compiles
  • Also compiles

Using Reference Types

A reference type refers to an object (an instance of a class). Unlike primitive types that hold their values in the memory where the variable is allocated, references do not hold the value of the object they refer to. Instead, a reference “points” to an object by storing the memory

address where the object is located, a concept referred to as a pointer. Unlike other lan-guages, Java does not allow you to learn what the physical memory address is. You can only use the reference to refer to the object.

Let’s take a look at some examples that declare and initialize reference types. Suppose we declare a reference of type String:

String greeting;

The greeting variable is a reference that can only point to a String object. A value is assigned to a reference in one of two ways:

A reference can be assigned to another object of the same or compatible type.

A reference can be assigned to a new object using the new keyword.

For example, the following statement assigns this reference to a new object:

greeting = new String(“How are you?”);

The greeting reference points to a new String object, “How are you?”. The String object does not have a name and can be accessed only via a corresponding reference.