ТОП просматриваемых книг сайта:
OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн.Название OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Год выпуска 0
isbn 9781119864592
Автор произведения Jeanne Boyarsky
Жанр Программы
Издательство John Wiley & Sons Limited
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
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 languages, 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.
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
Each primitive type has a wrapper class, which is an object type that corresponds to the primitive. Table 1.7 lists all the wrapper classes along with how to create them.
TABLE 1.7 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.