OCA Points
OCA Points
Chapter 1
Creating Objects
———————————
There are two key points to note about
the constructor:
• the name of the constructor matches the
name of the class,
• and there’s no return type.
Order of Initialization
• Fields and instance initializer blocks are run in
the order in which they appear in the file.
• The constructor runs after all fields and
instance initializer blocks have run.
The last thing you need to know about
numeric literals is a feature added in Java 7.
You can have underscores in numbers to make
them easier to read:
Variable type
boolean - falsebyte, short, int, long - 0 (in the
type’s bit-length)float, double - 0.0 (in the
type’s bit-length)char - \u0000' (NUL)All
object references (everything else) - null
x^y
y= true y= false
(EXCLUSIV
E OR)
x= true true true
false
x= false true
(AND)
(INCLUSIVE y= true y= false
OR)
x= true false true
x= false true false
■■■
Here are some tips to help remember this
table:AND is only true if both operands are
true.Inclusive OR is only false if both
operands are false.
Exclusive OR is only true if the operands are
different.
we present the conditional operators, &&
and ||, which are often referred to as short-
circuit operators. The short-circuit operators
are nearly identical to the logical opera- tors,
& and |, respectively, except that the right-
hand side of the expression may never be
evaluated if the final result can be determined
by the left-hand side of the expression.
A more common example of where short-circuit operators are used is checking for
null objects before performing an operation, such as this:
} //ifwe used a logical &, then both sides would always be evaluated and when x was
null this would throw an exception:
Equality Operators
1. Comparing two numeric primitive types. If the numeric values are of different
data types, the values are automatically promoted as previously described. For
example, 5 == 5.00 returns true since the left side is promoted to a double.
The comparisons for equality are limited to these three cases, so you cannot mix and
match types. For example, each of the following would result in a compiler error:
//TODO
boolean x = true == 3; // DOES NOT COMPILEboolean y = false != "Giraffe"; // DOES NOT COMPILE
boolean z = 3 == "Kangaroo"; // DOES NOT COMPILE
For object comparison, the equality operator is applied to the references to the objects,
not the objects they point to. Two references are equal if and only if they point to
the same object, or both point to null.