Скачать книгу

B as a prefix—for example, 0b10, 0B10.

      You won't need to convert between number systems on the exam. You'll have to recognize 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 characters next to each other, although we don't recommend it.

      Let's look at a few examples:

      double notAtStart = _1000.00; // DOES NOT COMPILE double notAtEnd = 1000.00_; // DOES NOT COMPILE double notByDecimal = 1000_.00; // DOES NOT COMPILE double annoyingButLegal = 1_00_0.0_0; // Ugly, but compiles double reallyUgly = 1__________2; // Also compiles

      Using Reference Types

      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.

      Distinguishing between Primitives and Reference Types

      There are a few important differences you should know between primitives and reference types. First, notice that all the primitive types have lowercase type names. All classes that come with Java begin with uppercase. Although not required, it is a standard practice, and you should follow this convention for classes you create as well.

      Next, reference types can be used to call methods, assuming the reference is not null. Primitives do not have methods declared on them. In this example, we can call a method on reference since it is of a reference type. You can tell length is a method because it has () after it. See if you can understand why the following snippet does not compile:

      4: String reference = "hello"; 5: int len = reference.length(); 6: int bad = len.length(); // DOES NOT COMPILE

      Line 6 is gibberish. No methods exist on len because it is an int primitive. Primitives do not have methods. Remember, a String is not a primitive, so you can call methods like length() on a String reference, as we did on line 5.

      Finally, reference types can be assigned null, which means they do not currently refer to an object. Primitive types will give you a compiler error if you attempt to assign them null. In this example, value cannot point to null because it is of type int:

      int value = null; // DOES NOT COMPILE String name = null;

      But what if you don't know the value of an int and want to assign it to null? In that case, you should use a numeric wrapper class, such as Integer, instead of int.

      Creating Wrapper Classes

Primitive type Wrapper class Wrapper class inherits Number? Example of creating
boolean Boolean No Boolean.valueOf(true)
byte Byte Yes Byte.valueOf((byte) 1)
short Short Yes Short.valueOf((short) 1)
int Integer Yes Integer.valueOf(1)
long Long Yes Long.valueOf(1)
float Float Yes Float.valueOf((float) 1.0)
double Double Yes Double.valueOf(1.0)
char Character No Character.valueOf('c')

      There is also a valueOf() variant that converts a String into the wrapper class. For example:

      int primitive = Integer.parseInt("123"); Integer wrapper = Integer.valueOf("123");

      The first line converts a String to an int primitive.

Скачать книгу