📄 4.doc.html
字号:
<a name="17747"></a>
<li>The <code>for</code> statement <a href="14.doc.html#24588">(§14.12)</a>
</ul><a name="48454"></a>
A <code>boolean</code> expression also determines which subexpression is evaluated in the
conditional <code>? :</code> operator <a href="15.doc.html#5257">(§15.24)</a>.
<p><a name="48458"></a>
Only <code>boolean</code> expressions can be used in control flow statements and as the first operand of the conditional operator <code>? :</code>. An integer <code>x</code> can be converted to a <code>boolean</code>, following the C language convention that any nonzero value is <code>true</code>, by the expression <code>x!=0</code>. An object reference <code>obj</code> can be converted to a <code>boolean</code>, following  the C language convention that any reference other than <code>null</code> is <code>true</code>, by the expression <code>obj!=null</code>.<p>
<a name="9299"></a>
A cast of a <code>boolean</code> value to type <code>boolean</code> is allowed <a href="5.doc.html#25209">(§5.1.1)</a>; no other casts on type <code>boolean</code> are allowed. A <code>boolean</code> can be converted to a string by string conversion <a href="5.doc.html#176921">(§5.4)</a>.<p>
<a name="9317"></a>
<h2>4.3 Reference Types and Values</h2>
<a name="9664"></a>
There are three kinds of <i>reference types</i>: class types <a href="8.doc.html#3857">(§8)</a>, interface types <a href="9.doc.html#238678">(§9)</a>, and
array types <a href="10.doc.html#27803">(§10)</a>.
<p><ul><pre>
<i>ReferenceType:<br>
ClassOrInterfaceType<br>
ArrayType
</i>
<i>ClassOrInterfaceType:<br>
ClassType<br>
InterfaceType
</i>
<i>ClassType:<br>
TypeName
</i>
<i>InterfaceType:<br>
TypeName
</i>
<i>ArrayType:<br>
</i> <i>Type</i><code> [ ]
</code></pre></ul><a name="9678"></a>
Names are described in <a href="6.doc.html#48086">§6</a>; type names in <a href="6.doc.html#20569">§6.5</a> and, specifically, <a href="6.doc.html#21721">§6.5.4</a>.
<p><a name="87599"></a>
The sample code:<p>
<pre><br><a name="10435"></a>class Point { int[] metrics; }
<br><a name="10414"></a>interface Move { void move(int deltax, int deltay); }
</pre><a name="11032"></a>
declares a class type <code>Point</code>, an interface type <code>Move</code>, and uses an array type <code>int[]</code>
(an array of <code>int</code>) to declare the field <code>metrics</code> of the class <code>Point</code>.
<p><a name="12028"></a>
<h3>4.3.1 Objects</h3>
<a name="86707"></a>
An <i>object</i> is a <i>class</i> <i>instance</i> or an array.
<p><a name="86710"></a>
The reference values (often just <i>references</i>) are <i>pointers </i>to these objects, and a special null reference, which refers to no object.<p>
<a name="49853"></a>
A class instance is explicitly created by a class instance creation expression <a href="15.doc.html#41147">(§15.8)</a>, or by invoking the <code>newInstance</code> method of class <code>Class</code> <a href="javalang.doc2.html#28532">(§20.3.8)</a>. An array is explicitly created by an array creation expression <a href="15.doc.html#41147">(§15.8)</a>.<p>
<a name="49899"></a>
A new class instance is implicitly created when the string concatenation operator + <a href="15.doc.html#39990">(§15.17.1)</a> is used in an expression, resulting in a new object of type <code>String</code> (<a href="4.doc.html#26992">§4.3.3</a>, <a href="javalang.doc11.html#14460">§20.12</a>). A new array object is implicitly created when an array initializer expression <a href="10.doc.html#11358">(§10.6)</a> is evaluated; this can occur when a class or interface is initialized <a href="12.doc.html#44557">(§12.4)</a>, when a new instance of a class is created <a href="15.doc.html#41147">(§15.8)</a>, or when a local variable declaration statement is executed <a href="14.doc.html#5920">(§14.3)</a>.<p>
<a name="49960"></a>
Many of these cases are illustrated in the following example:<p>
<pre><a name="49961"></a>
class Point {
<a name="51683"></a> int x, y;
<a name="86692"></a> Point() { System.out.println("default"); }
<a name="86696"></a> Point(int x, int y) { this.x = x; this.y = y; }
<a name="51733"></a>
// A Point instance is explicitly created at class initialization time:
<a name="51732"></a> static Point origin = new Point(0,0);
<a name="51734"></a>
// A String can be implicitly created by a + operator:
<a name="51682"></a> public String toString() {<br>
return "(" + x + "," + y + ")";<br>
}
<a name="51684"></a>}
<br><a name="51685"></a>
class Test {
<a name="51686"></a> public static void main(String[] args) {
<a name="51735"></a> // A Point is explicitly created using newInstance:
<a name="23341"></a> Point p = null;
<a name="23324"></a> try {
<a name="51687"></a> p = (Point)Class.forName("Point").newInstance();
<a name="23327"></a> } catch (Exception e) {
<a name="23350"></a> System.out.println(e);
<a name="23351"></a> }
<br><a name="51738"></a>
// An array is implicitly created by an array constructor:
<a name="51688"></a> Point a[] = { new Point(0,0), new Point(1,1) };
<br><a name="51757"></a>
// Strings are implicitly created by + operators:
<a name="51689"></a> System.out.println("p: " + p);
<a name="51690"></a> System.out.println("a: { " + a[0] + ", "<br>
 + a[1] + " }");
<br><a name="51745"></a>
// An array is explicitly created by an array creation expression:
<a name="51693"></a> String sa[] = new String[2];
<a name="51697"></a> sa[0] = "he"; sa[1] = "llo";
<a name="51698"></a> System.out.println(sa[0] + sa[1]);
<a name="51699"></a> }
<a name="49976"></a>}
</pre><a name="87328"></a>
which produces the output:
<p><pre><a name="87331"></a>
default
<a name="87332"></a>p: (0,0)
<a name="87333"></a>a: { (0,0), (1,1) }
<a name="87329"></a>hello
</pre><a name="49965"></a>
The operators on references to objects are:<p>
<ul><a name="31321"></a>
<li>Field access, using either a qualified name <a href="6.doc.html#33916">(§6.6)</a> or a field access expression <a href="15.doc.html#41267">(§15.10)</a>
<a name="31328"></a>
<li>Method invocation <a href="15.doc.html#20448">(§15.11)</a>
<a name="31378"></a>
<li>The cast operator (<a href="5.doc.html#176921">§5.4</a>, <a href="15.doc.html#238146">§15.15</a>)
<a name="31345"></a>
<li>The string concatenation operator <code>+</code> <a href="15.doc.html#39990">(§15.17.1)</a>, which, when given a <code>String</code> operand and a reference, will convert the reference to a <code>String</code> by invoking the <code>toString</code> method <a href="javalang.doc1.html#1152">(§20.1.2)</a> of the referenced object (using <code>"null"</code> if either the reference or the result of <code>toString</code> is a null reference), and then will produce a newly created <code>String</code> that is the concatenation of the two strings
<a name="31399"></a>
<li>The <code>instanceof</code> operator <a href="15.doc.html#80289">(§15.19.2)</a>
<a name="31306"></a>
<li>The reference equality operators <code>==</code> and <code>!=</code> <a href="15.doc.html#236163">(§15.20.3)</a>
<a name="19595"></a>
<li>The conditional operator <code>? :</code> <a href="15.doc.html#5257">(§15.24)</a>.
</ul><a name="28239"></a>
There may be many references to the same object. Most objects have state, stored in the fields of objects that are instances of classes or in the variables that are the components of an array object. If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the object, and then the altered state can be observed through the reference in the other variable.<p>
<a name="87600"></a>
The example program:<p>
<pre><a name="11036"></a>class Value { int val; }
</pre><pre><a name="87412"></a>
class Test {
<a name="87413"></a> public static void main(String[] args) {
<a name="87414"></a> int i1 = 3;
<a name="52134"></a> int i2 = i1;
<a name="52135"></a> i2 = 4;
<a name="50355"></a> System.out.print("i1==" + i1);
<a name="50357"></a> System.out.println(" but i2==" + i2);
<a name="50359"></a> Value v1 = new Value();
<a name="50361"></a> v1.val = 5;
<a name="52142"></a> Value v2 = v1;
<a name="52143"></a> v2.val = 6;
<a name="50363"></a> System.out.print("v1.val==" + v1.val);
<a name="50365"></a> System.out.println(" and v2.val==" + v2.val);
<a name="50367"></a> }
<a name="50369"></a>}
</pre><a name="11050"></a>
produces the output:
<p><pre><a name="11051"></a>
i1==3 but i2==4
<a name="45608"></a>v1.val==6 and v2.val==6
</pre><a name="11053"></a>
because <code>v1.val</code> and <code>v2.val</code> reference the same instance variable <a href="4.doc.html#28536">(§4.5.3)</a> in the
one <code>Value</code> object created by the only <code>new</code> expression, while <code>i1</code> and <code>i2</code> are different
variables.
<p><a name="17783"></a>
See <a href="10.doc.html#27803">§10</a> and <a href="15.doc.html#46168">§15.9</a> for examples of the creation and use of arrays.<p>
<a name="17053"></a>
Each object has an associated lock <a href="17.doc.html#28460">(§17.13)</a>, which is used by <code>synchronized</code> methods <a href="8.doc.html#78188">(§8.4.3)</a> and the <code>synchronized</code> statement <a href="14.doc.html#79287">(§14.17)</a> to provide control over concurrent access to state by multiple threads (<a href="17.doc.html#28457">§17.12</a>, <a href="javalang.doc18.html#2658">§20.20</a>).<p>
<a name="11055"></a>
<h3>4.3.2 The Class <code>Object</code></h3>
<a name="26999"></a>
The standard class <code>Object</code> is a superclass <a href="8.doc.html#15372">(§8.1)</a> of all other classes. A variable of
type <code>Object</code> can hold a reference to any object, whether it is an instance of a class
or an array <a href="10.doc.html#27803">(§10)</a>. All class and array types inherit the methods of class <code>Object</code>,
which are summarized here and completely specified in <a href="javalang.doc1.html#46442">§20.1</a>:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -