Java Reference
In-Depth Information
"" instanceof Object
// False: null is never an instance of anything
null instanceof String
Object o = new int [] { 1 , 2 , 3 };
o instanceof int [] // True: the array value is an int array
o instanceof byte [] // False: the array value is not a byte array
o instanceof Object // True: all arrays are instances of Object
a x
// Use instanceof to make sure that it is safe to cast an object
if ( object instanceof Point ) {
Point p = ( Point ) object ;
Special Operators
Java has six language constructs that are sometimes considered operators and some‐
times considered simply part of the basic language syntax. These “operators” were
included in Table 2-4 in order to show their precedence relative to the other true
operators. The use of these language constructs is detailed elsewhere in this topic,
but is described briefly here so that you can recognize them in code examples:
Object member access ( . )
An object is a collection of data and methods that operate on that data; the data
fields and methods of an object are called its members. The dot (.) operator
accesses these members. If o is an expression that evaluates to an object refer‐
ence, and f is the name of a field of the object, o.f evaluates to the value con‐
tained in that field. If m is the name of a method, o.m refers to that method and
allows it to be invoked using the () operator shown later.
Array element access ( [] )
An array is a numbered list of values. Each element of an array can be referred
to by its number, or index . The [ ] operator allows you to refer to the individ‐
ual elements of an array. If a is an array, and i is an expression that evaluates to
an int , a[i] refers to one of the elements of a . Unlike other operators that
work with integer values, this operator restricts array index values to be of type
int or narrower.
Method invocation ( () )
A method is a named collection of Java code that can be run, or invoked , by fol‐
lowing the name of the method with zero or more comma-separated expres‐
sions contained within parentheses. The values of these expressions are the
arguments to the method. The method processes the arguments and optionally
returns a value that becomes the value of the method invocation expression. If
o.m is a method that expects no arguments, the method can be invoked with
o.m() . If the method expects three arguments, for example, it can be invoked
with an expression such as o.m(x,y,z) . Before the Java interpreter invokes a
method, it evaluates each of the arguments to be passed to the method. These
Search WWH ::

Custom Search