3.16 Other Operators: new , , instanceof


3.16 Other Operators: new , [] , instanceof

The new operator is used to create objects, that is, instances of classes and arrays. It is used with a constructor call to instantiate classes (see Section 4.4, p. 117), and with the [] notation to create arrays (see Section 4.1, p. 100). It is also used to instantiate anonymous arrays (see Section 4.1, p. 104), and anonymous classes (see Section 7.5, p. 308).

 Pizza onePizza = new Pizza();       // Create an instance of Pizza class. 

The [] notation is used to declare and construct arrays and also to access array elements (see Section 4.1, p. 100).

 int[] anArray = new int[5];// Declare and construct an int array of 5 elements. anArray[4] = anArray[3];   // Element at index 4 gets value of element at index 3. 

The boolean, binary, and infix operator instanceof is used to test an object's type (see Section 6.6, p. 264).

 Pizza myPizza = new Pizza(); boolean test1 = myPizza instanceof Pizza;  // True. boolean test2 = "Pizza" instanceof Pizza;  // Compile error. String is not Pizza. boolean test3 = null instanceof Pizza;     // Always false. null not an instance. 


A Programmer[ap]s Guide to Java Certification
A Programmer[ap]s Guide to Java Certification
ISBN: 201596148
EAN: N/A
Year: 2003
Pages: 284

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net