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 new class instance is implicitly created when the string concatenation operator
+
String
(§
4.3.3
).
A new array object is implicitly created when an array initializer expression (§
10.6
) is eval-
of a class is created (§
15.9
), or when a local variable declaration statement is executed
New objects of the types
Boolean
,
Byte
,
Short
,
Character
,
Integer
,
Long
,
Float
, and
Double
may be
Example 4.3.1-1. Object Creation
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 {