📄 4.doc.html
字号:
<p><pre><a name="12035"></a>package java.lang;
</pre><pre><a name="45609"></a>
public class Object {
<a name="51634"></a> public final Class getClass() { . . . }
<a name="51635"></a> public String toString() { . . . }
<a name="51636"></a> public boolean equals(Object obj) { . . . }
<a name="51637"></a> public int hashCode() { . . . }
<a name="51638"></a> protected Object clone()
<a name="86088"></a> throws CloneNotSupportedException { . . . }
<a name="87378"></a> public final void wait()<br>
throws IllegalMonitorStateException,<br>
InterruptedException { . . . }
<a name="51641"></a> public final void wait(long millis)
<a name="87381"></a> throws IllegalMonitorStateException,
<a name="87383"></a> InterruptedException { . . . }
<a name="51642"></a> public final void wait(long millis, int nanos) { . . . }
<a name="87385"></a> throws IllegalMonitorStateException,
<a name="87386"></a> InterruptedException { . . . }
<a name="87373"></a> public final void notify() { . . . }
<a name="87374"></a> throws IllegalMonitorStateException
<a name="87387"></a> public final void notifyAll() { . . . }
<a name="87389"></a> throws IllegalMonitorStateException
<a name="51644"></a> protected void finalize()
<a name="86090"></a> throws Throwable { . . . }
<a name="51645"></a>}
</pre><a name="45610"></a>
The members of <code>Object</code> are as follows:
<p><ul><a name="12048"></a>
<li>The method <code>getClass</code> returns the <code>Class</code> <a href="javalang.doc2.html#14342">(§20.3)</a> object that represents the class of the object. A <code>Class</code> object exists for each reference type. It can be used, for example, to discover the fully qualified name of a class, its members, its immediate superclass, and any interfaces that it implements. A class method that is declared <code>synchronized</code> <a href="8.doc.html#55408">(§8.4.3.5)</a> synchronizes on the lock associated with the <code>Class</code> object of the class.
<a name="12066"></a>
<li>The method <code>toString</code> returns a <code>String</code> representation of the object.
<a name="12104"></a>
<li>The methods <code>equals</code> and <code>hashCode</code> are declared for the benefit of hashtables such as <code>java.util.Hashtable</code> <a href="javautil.doc6.html#7569">(§21.7)</a>. The method <code>equals</code> defines a notion of object equality, which is based on value, not reference, comparison.
<a name="12115"></a>
<li>The method <code>clone</code> is used to make a duplicate of an object.
<a name="12116"></a>
<li>The methods <code>wait</code>, <code>notify</code>, and <code>notifyAll</code> are used in concurrent programming using threads, as described in <a href="17.doc.html#26250">§17</a>.
<a name="12117"></a>
<li>The method <code>finalize</code> is run just before an object is destroyed and is described in <a href="12.doc.html#44748">§12.6</a>.
</ul><a name="26992"></a>
<h3>4.3.3 The Class <code>String</code></h3>
<a name="27000"></a>
Instances of class <code>String</code> <a href="javalang.doc11.html#14460">(§20.12)</a> represent sequences of Unicode characters.
A  <code>String</code> object has a constant (unchanging) value. String literals <a href="3.doc.html#101083">(§3.10.5)</a> are
references to instances of class <code>String</code>.
<p><a name="52188"></a>
The string concatenation operator <code>+</code> <a href="15.doc.html#39990">(§15.17.1)</a> implicitly creates a new <code>String</code> object.<p>
<a name="52197"></a>
<h3>4.3.4 When Reference Types Are the Same</h3>
<a name="22319"></a>
Two reference types the <i>same type</i> if:
<p><ul><a name="86567"></a>
<li>They are both class or both interface types, are loaded by the same class loader, and have the same fully-qualified name <a href="6.doc.html#33916">(§6.6)</a>, in which case they are sometimes said to be the <i>same class</i> or the <i>same interface</i>.
<a name="86576"></a>
<li>They are both array types, and have the same component type <a href="10.doc.html#27803">(§10)</a>.
</ul><a name="25948"></a>
<h2>4.4 Where Types Are Used</h2>
<a name="52309"></a>
Types are used when they appear in declarations or in certain expressions.
<p><a name="85893"></a>
The following code fragment contains one or more instances of each kind of usage of a type:<p>
<pre><br><a name="52319"></a>import java.util.Random;
<br></pre><pre><a name="52320"></a>
class MiscMath {
<br><a name="19644"></a> int divisor;
<br><a name="85892"></a>
MiscMath(int divisor) {
<a name="85931"></a> this.divisor = divisor;
<a name="85932"></a> }
<br><a name="19646"></a>
float ratio(long l) {
<a name="52325"></a> try {
<a name="52326"></a> l /= divisor;
<a name="52327"></a> } catch (Exception e) {
<a name="52328"></a> if (e instanceof ArithmeticException)
<a name="52329"></a> l = Long.MAX_VALUE;
<a name="52330"></a> else
<a name="52331"></a> l = 0;
<a name="52332"></a> }
<a name="52333"></a> return (float)l;
<a name="52334"></a> }
<br><a name="19664"></a>
double gausser() {
<a name="19665"></a> Random r = new Random();
<a name="19666"></a> double[] val = new double[2];
<a name="19667"></a> val[0] = r.nextGaussian();
<a name="19668"></a> val[1] = r.nextGaussian();
<a name="19669"></a> return (val[0] + val[1]) / 2;
<a name="19670"></a> }
<br><a name="52342"></a>}
</pre><a name="53860"></a>
In this example, types are used in declarations of the following:
<p><ul><a name="53864"></a>
<li>Imported types <a href="7.doc.html#26656">(§7.5)</a>; here the type <code>Random</code>, imported from the type<code> java.util.Random</code> of the package <code>java.util</code>, is declared
<a name="49983"></a>
<li>Fields, which are the class variables and instance variables of classes <a href="8.doc.html#40898">(§8.3)</a>, and constants of interfaces <a href="9.doc.html#78642">(§9.3)</a>; here the field <code>divisor</code> in the class <code>MiscMath</code>  is declared to be of type <code>int</code>
<a name="49990"></a>
<li>Method parameters <a href="8.doc.html#38698">(§8.4.1)</a>; here the parameter <code>l</code> of the method <code>ratio</code> is declared to be of type <code>long</code>
<a name="52378"></a>
<li>Method results <a href="8.doc.html#40420">(§8.4)</a>; here the result of the method <code>ratio</code> is declared to be of type <code>float</code>, and the result of the method <code>gausser</code> is declared to be of type <code>double</code>
<a name="38145"></a>
<li>Constructor parameters <a href="8.doc.html#29488">(§8.6.1)</a>; here the parameter of the constructor for<code> MiscMath</code> is declared to be of type <code>int</code>
<a name="12250"></a>
<li>Local variables (<a href="14.doc.html#5920">§14.3</a>, <a href="14.doc.html#24588">§14.12</a>); the local variables <code>r</code> and <code>val</code> of the method <code>gausser</code> are declared to be of types <code>Random</code> and <code>double[]</code> (array of <code>double</code>)
<a name="52411"></a>
<li>Exception handler parameters <a href="14.doc.html#79311">(§14.18)</a>; here the exception handler parameter <code>e</code> of the <code>catch</code> clause is declared to be of type <code>Exception</code>
</ul><a name="25979"></a>
and in expressions of the following kinds:
<p><ul><a name="52468"></a>
<li>Class instance creations <a href="15.doc.html#41147">(§15.8)</a>; here a local variable <code>r</code> of method <code>gausser</code> is initialized by a class instance creation expression that uses the type <code>Random</code>
<a name="87680"></a>
<li>Array creations <a href="15.doc.html#46168">(§15.9)</a>; here the local variable <code>val</code> of method <code>gausser</code> is initialized by an array creation expression that creates an array of <code>double</code> with size 2
<a name="52427"></a>
<li>Casts <a href="15.doc.html#238146">(§15.15)</a>; here the <code>return</code> statement of the method <code>ratio</code> uses the <code>float</code> type in a cast
<a name="25987"></a>
<li>The <code>instanceof</code> operator <a href="15.doc.html#80289">(§15.19.2)</a>; here the <code>instanceof</code> operator tests whether <code>e</code> is assignment compatible with the type <code>ArithmeticException</code>
</ul><a name="18470"></a>
<h2>4.5 Variables</h2>
<a name="10872"></a>
A variable is a storage location and has an associated type, sometimes called its
<i>compile-time type</i>, that is either a primitive type <a href="4.doc.html#85587">(§4.2)</a> or a reference type <a href="4.doc.html#9317">(§4.3)</a>.
A variable always contains a value that is assignment compatible <a href="5.doc.html#170768">(§5.2)</a> with its
type. A variable's value is changed by an assignment <a href="15.doc.html#5281">(§15.25)</a> or by a prefix or
postfix <code>++</code> (increment) or <code>--</code> (decrement) operator (<a href="15.doc.html#39438">§15.13.2</a>, <a href="15.doc.html#4987">§15.13.3</a>, <a href="15.doc.html#39547">§15.14.1</a>,
<a href="15.doc.html#239136">§15.14.2</a>).
<p><a name="24555"></a>
Compatibility of the value of a variable with its type is guaranteed by the design of the Java language. Default values are compatible <a href="4.doc.html#10931">(§4.5.4)</a> and all assignments to a variable are checked for assignment compatibility <a href="5.doc.html#170768">(§5.2)</a>, usually at compile time, but, in a single case involving arrays, a run-time check is made <a href="10.doc.html#11430">(§10.10)</a>.<p>
<a name="28344"></a>
<h3>4.5.1 Variables of Primitive Type</h3>
<a name="17088"></a>
A variable of a primitive type always holds a value of that exact primitive type.
<p><a name="28345"></a>
<h3>4.5.2 Variables of Reference Type</h3>
<a name="10877"></a>
A variable of reference type can hold either of the following:
<p><ul><a name="52494"></a>
<li>A null reference
<a name="52496"></a>
<li>A reference to any object <a href="4.doc.html#9317">(§4.3)</a> whose class <a href="4.doc.html#24887">(§4.5.5)</a> is assignment compatible <a href="5.doc.html#170768">(§5.2)</a> with the type of the variable
</ul><a name="28536"></a>
<h3>4.5.3 Kinds of Variables</h3>
<a name="10883"></a>
There are seven kinds of variables:
<p><ol>
<a name="10884"></a>
<li>A <i>class variable</i> is a field declared using the keyword <code>static</code> within a class declaration <a href="8.doc.html#37544">(§8.3.1.1)</a>, or with or without the keyword <code>static</code> within an interface declaration <a href="9.doc.html#78642">(§9.3)</a>. A class variable is created when its class or interface is loaded <a href="12.doc.html#44459">(§12.2)</a> and is initialized to a default value <a href="4.doc.html#10931">(§4.5.4)</a>. The class variable effectively ceases to exist when its class or interface is unloaded <a href="12.doc.html#44850">(§12.8)</a>, after any necessary finalization of the class or interface <a href="12.doc.html#44748">(§12.6)</a> has been completed.
<a name="51516"></a>
<li>An <i>instance variable</i> is a field declared within a class declaration without using the keyword <code>static</code> <a href="8.doc.html#37544">(§8.3.1.1)</a>. If a class <code>T</code> has a field <i>a</i> that is an instance variable, then a new instance variable <i>a</i> is created and initialized to a default value <a href="4.doc.html#10931">(§4.5.4)</a> as part of each newly created object of class <code>T</code> or of any class that is a subclass of <code>T</code> <a href="8.doc.html#21723">(§8.1.3)</a>. The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object <a href="12.doc.html#44748">(§12.6)</a> has been completed.
<a name="10895"></a>
<li><i>Array components</i> are unnamed variables that are created and initialized to default values <a href="4.doc.html#10931">(§4.5.4)</a> whenever a new object that is an array is created <a href="15.doc.html#46168">(§15.9)</a>. The array components effectively cease to exist when the array is no longer referenced. See <a href="10.doc.html#27803">§10</a> for a description of arrays.
<a name="24632"></a>
<li><i>Method parameters</i> <a href="8.doc.html#38698">(§8.4.1)</a> name argument values passed to a method. For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked <a href="15.doc.html#20448">(§15.11)</a>. The new variable is initialized with the corresponding argument value from the method invocation. The method parameter effectively ceases to exist when the execution of the body of the method is complete.
<a name="24657"></a>
<li><i>Constructor parameters</i> <a href="8.doc.html#29488">(§8.6.1)</a> name argument values passed to a constructor. For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression <a href="15.doc.html#41147">(§15.8)</a> or explicit constructor invocation <a href="8.doc.html#78435">(§8.6.5)</a> invokes that constructor. The new variable is initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist when the execution of the body of the constructor is complete.
<a name="10903"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -