Java Reference
In-Depth Information
we would force its use as a raw type, losing type information.
4.3.1. Objects
An object is a class instance or an array .
The reference values (often just references ) are pointers to these objects, and a special null
reference, which refers to no object.
A class instance is explicitly created by a class instance creation expression (§ 15.9 ) .
An array is explicitly created by an array creation expression (§ 15.10 ) .
A new class instance is implicitly created when the string concatenation operator +
15.18.1 ) is used in a non-constant (§ 15.28 ) expression, resulting in a new object of type
String 4.3.3 ).
A new array object is implicitly created when an array initializer expression (§ 10.6 ) is eval-
uated; this can occur when a class or interface is initialized (§ 12.4 ) , when a new instance
of a class is created (§ 15.9 ), or when a local variable declaration statement is executed
14.4 ) .
New objects of the types Boolean , Byte , Short , Character , Integer , Long , Float , and Double may be
implicitly created by boxing conversion (§ 5.1.7 ) .
Example 4.3.1-1. Object Creation
Click here to view code image
class Point {
int x, y;
Point() { System.out.println("default"); }
Point(int x, int y) { this.x = x; this.y = y; }
/* A Point instance is explicitly created at
class initialization time: */
static Point origin = new Point(0,0);
/* A String can be implicitly created
by a + operator: */
public String toString() { return "(" + x + "," + y + ")"; }
}
class Test {
public static void main(String[] args) {
/* A Point is explicitly created
using newInstance: */
Point p = null;
try {
Search WWH ::




Custom Search