tag:blogger.com,1999:blog-150471662008-03-12T23:37:46.625-07:00Java, SCJP, Core Java Programmingshabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.comBlogger23125tag:blogger.com,1999:blog-15047166.post-1143919253919087392006-04-01T11:19:00.000-08:002006-04-01T11:21:03.060-08:00Declarations and Access Control<a href="file:///C:/temp/java/d0e34.htm"></a><a href="file:///C:/temp/java/d0e409.htm"></a><p><a name="d0e115"><hr /> </a> <table bg border="0" cellpadding="5" cellspacing="0" width="100%" style="color:black;"> <tbody><tr> <td class="objh"><span style="color:white;"><a name="d0e118"></a>Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms both for declaration and for initialization. </span></td> </tr> </tbody></table> </p><h3><a name="d0e120"></a>Declaring arrays: </h3> <p>For example,use either </p><pre> int[] x;</pre><p>or</p><pre> int x[];</pre><p>Both are legal positions for the brackets. To declare a multidimensional array, use multiple sets of brackets, e.g. <code>int i[][];</code> declares a two dimensional array. Note that, for example, <code>int[]i[];</code> is also legal, and means the same thing. In Java, multidimensional arrays can be "not square" i.e. they are just arrays of arrays, and each of those constituent arrays can be of a different size. </p> <h3><a name="d0e138"></a>Construct arrays: </h3> <p>Arrays are objects. Use the <code>new</code> keyword to construct them. For example having declared an array i: </p><pre>int[] i;</pre><p>you then construct the array and assign it to i as follows:</p><pre>i = new int[10];</pre><p>The size of the array is in the brackets (in this example 10). It is common to do both operations in one statement:</p><pre>int i[] = new int[10];</pre><h3><a name="d0e155"></a>To initialize an array using loop iteration: </h3> <p>An example:</p><pre> int array[] = new int[15]; for(int j=0; j<array.length;j++){ array[j]=j; }</pre><h3><a name="d0e161"></a>Write code to initialize an array using the combined declaration and initialization format: </h3> <p>An example:</p><pre>char c[]= new char[] {'a','b','c','d','e'};</pre><p>or you can use</p><pre>char c[]= {'a','b','c','d','e'};</pre> <table bg border="0" cellpadding="5" cellspacing="0" width="100%" style="color:black;"> <tbody><tr> <td class="objh"><span style="color:white;"><a name="d0e172"></a>Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as <code>public</code>, <code>final</code>, <code>static</code>, <code>abstract</code> etc.). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers. </span></td> </tr> </tbody></table> <p>This is fundamental Java. If you are unsure of this topic, see a textbook.</p> <p>A good but technical reference for these kinds of issues is the Java Language Specification (not for beginners). It is available at the following URL: <a href="http://java.sun.com/docs/books/jls/html/index.html">http://java.sun.com/docs/books/jls/html/index.html</a></p> <h3><a name="d0e193"></a>Some terms and their synonyms: </h3> <p><u>Scope/Visibility:</u> Where something can be seen / is accessable from. </p> <p><u>Nested/Inner Class:</u> A class whose code sits inside the body of another 'outer' class. It exists 'inside' the class in that it can see the private methods and variables. </p> <p><u>Instance/Member:</u> Means the method/variable/nested class belongs to each object which is an instance of the class. The term 'a member' is often used to refer to both methods and variables of this type. Cannot be accessed by statically i.e. without having an instance of the class. </p> <p><u>Class/Static:</u> The method/variable/nested class belongs to the class as opposed to the instances of the class. Can be used without creating an instance of the class, but static methods/nested class cannot use instancts variables/methods. </p> <p><u>Local/Automatic variable:</u> A variable which is declared within a method or as a parameter to the method. Cannot be seen outside the method. </p> <h3><a name="d0e215"></a>Scoping Types </h3> <p>"default" or "friendly" - this is where no modifier is used. Means something is only visible to classes in the same package.</p> <p><code>protected</code> - can only be accessed by classes in the same package or subclasses of this class. This frequently surprises experienced developers, especially those with a prior backround in the mutant hybrid of object orientated programming and assembly language known as 'C++' :-) To fair the name is misleading, as it sounds like it restricts access, where as it in fact it adds subclasses outside the package to the list of things that can access the item in question, as compared to using "default" access. </p> <p><code>public</code> - can be accessed by any other class. </p> <p><code>private</code> - can only be accessed from inside the class. </p> <h3><a name="d0e231"></a>Declare classes using the modifiers <code>public</code>, <code>abstract</code> or <code>final</code>: </h3> <p><code>public</code> - is visible outside of its package. Without this modifier, the class cannot be accessed outside its package. </p> <p><code>abstract</code> - cannot be instantiated, is allowed to contain <code>abstract</code> methods. </p> <p><code>final</code> - cannot be subsclassed. </p> <h3><a name="d0e257"></a>Using the modifiers <code>private</code>, <code>protected</code>, <code>public</code>, <code>static</code>, <code>final</code>, <code>native</code> or <code>abstract</code>: </h3> <p><code>private</code> - can only be accessed from inside the class. Private members are not inherited by subclasses. Inner classes can be declared <code>private</code>. </p> <p><code>protected</code> - can only be accessed by classes in the same package or subclasses of this class. </p> <p><code>public</code> - can be accessed by any other class. </p> <p><code>static</code> - belongs to the class rather than any particular instance of the class. For variables, effectively, there is just one copy of this variable for all instances of the class, and if an instance changes the value, the other instances see that new value. For methods, it means the method can be called without having created an instance, but within the methodd you cannot use the <code>this</code> keyword, or refer to instance variables and methods directly (without creating an instance and referring to the variable/class in that instance). For inner classes, it means they can be instantiated without having an instance of the enclosing class, but as with static methods, the methods of the inner class cannot refer to instance variables or methods of the enclosing class directly. </p> <p><code>final</code> - cannot be changed. Variables are constants, methods cannot be overridden, classes cannot be subclassed. Since Java1.1 you can declare the variable without assigning a value. Once you assign a value, you cannot change it. The are known as 'blank' finals, are frequently used for things like constants you wish to initialise from a configuration file. </p> <p><code>native</code> - a <b>method</b> which is not written in java and is outside the JVM in a library. </p> <p><code>abstract</code> - a <b>method</b> which is not implemented. Must be implemented in a subclass if that subclass is to be 'concrete' and allow people to instantiate it. </p> <h2><a name="d0e320"></a>Nested Classes </h2> <h3><a name="d0e322"></a>To define a non-static nested class either in a class or method scope: </h3> <p>Place the class definition (for the nested class) inside another class definition (the outer class) or a method.</p> <h3><a name="d0e326"></a>To define, in method scope, an anonymous nested class that implements a specified interface: </h3> <p>An anonymous nested class is defined where is it instantiated (in a method). An anonymous nested class must either implement an interface or extend a class, but the <code>implements</code> or <code>extends</code> keywords are not used. For example the following line causes the method to return an object which is an instance of an anonymous nested class: </p><pre>return new SomeClass() { /*body of the anonymous class goes here*/ };</pre><p>You might like to think of an anonymous nested class as part of a really long <code>new</code> statement, which happens to contain a class definition, and which is why it has a ";" at the end. The following example calls <code>someMethod()</code>, passing an instance of the anonymous nested class: </p><pre>someMethod(new SomeClass() { /*body of the anonymous class goes here*/ });</pre><p>In both cases <code>SomeClass()</code> is not the name of the anonymous class (anonymous means it has no name) rather is it the name of the class that you are extending or the interface you are implementing. These classes cannot define a constructor, as they do not have a name that you can use to declare the constructor method. If SomeClass() is a class, the default constructor of that class is called, if you want to use a non-default constructor instead, you supply arguments e.g.: </p><pre>return new SomeClass(12) { /*body of the anonymous class goes here*/ };</pre><p>will call the SomeClass constructor which takes an int.</p> <h3><a name="d0e357"></a>Write code in a non-static method of the outer class to construct an instance of the nested class. </h3><code>Inner x = new Inner();</code> constructs an instance of Inner where Inner is a nested class defined in the current class. <h3><a name="d0e362"></a>Write code to construct an instance on a nested class where either no <code>this</code> object exists, or the current <code>this</code> object is not an instance of the outer class. </h3> You must create an instance of the outer class first. Given a class, Outer, containing a nested class Inner: <pre>Outer.Inner y = new Outer().new Inner();</pre><p>The above creates an instance of Inner called y, but it had to construct an instance of Outer first. The following example creates the Outer instance on a separate line, the syntax in the second line is the one you use when you already have an instance of the outer class. </p><pre>Outer x = new Outer(); Outer.Inner y = x.new Inner();</pre> If Inner is static, you can use: <pre>Outer.Inner I= new Outer.Inner();</pre><h3><a name="d0e380"></a>State which variables and methods in enclosing scopes are accessible from methods of the inner class. </h3> <p> A non-static inner class has access to all member variables and methods of the containing class. If the inner class is defined inside a method, it has access to those method (a.k.a. automatic or local) variables which are declared <code>final</code>, in addition to the above. </p> <p>A static inner class is restricted in the same way as a static method: it cannot refer to instance variables and methods of the containing class directly (without creating an instance and referring to the variable/class in that instance). </p> <table bg border="0" cellpadding="5" cellspacing="0" width="100%" style="color:black;"> <tbody><tr> <td class="objh"><span style="color:white;"><a name="d0e390"></a>For a given class, determine if a default constructor will be created and if so state the prototype of that constructor. </span></td> </tr> </tbody></table> <p>The default constructor takes no arguments e.g. <code>classname()</code> where classname is the name of you class. A default constructor is automatically created only if you do not create <b>any</b> constructors in your class. If you create a non-default constructor (i.e. one that takes an argument), then you may have to create a default one yourself, if you want there to be one. </p> <p>All constructors call the default constructor of its parents class (if there is one), and so on up the hierarchy to Object, unless you specify a different constructor using <code>super(...)</code> (to use a constructor in the parent) or <code>this(...)</code>. If you use such calls, they must be the first thing in the constructor. </p> <p align="center"> </p><hr /><a href="file:///C:/temp/java/d0e409.htm"></a>©1999, 2000, 2002 Dylan Walsh. Under free documentation liscence.shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.comtag:blogger.com,1999:blog-15047166.post-1140297496319692872006-02-18T13:14:00.000-08:002006-02-18T13:18:19.253-08:00This link is to get listed in a directory.<a href="http://www.human-anatomy.net">Human-Anatomy.net Software</a>shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.comtag:blogger.com,1999:blog-15047166.post-1135824718370346122005-12-28T18:43:00.000-08:002005-12-28T18:51:59.086-08:00<a name="d0e115"><h2> </h2>Written by <small>Dylan Walsh. Released under FDL.</small> <h2>Declarations and Access Control</h2> </a> <table bg border="0" cellpadding="5" cellspacing="0" width="100%" style="color:black;"> <tbody> <tr> <td class="objh"><span style="color:white;"><a name="d0e118"></a>Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms both for declaration and for initialization. </span></td></tr></tbody> </table> <h3><a name="d0e120"></a>Declaring arrays: </h3> <p>For example,use either </p> <pre> int[] x;</pre> <p>or</p> <pre> int x[];</pre> <p>Both are legal positions for the brackets. To declare a multidimensional array, use multiple sets of brackets, e.g. <code>int i[][];</code> declares a two dimensional array. Note that, for example, <code>int[]i[];</code> is also legal, and means the same thing. In Java, multidimensional arrays can be "not square" i.e. they are just arrays of arrays, and each of those constituent arrays can be of a different size. </p> <h3><a name="d0e138"></a>Construct arrays: </h3> <p>Arrays are objects. Use the <code>new</code> keyword to construct them. For example having declared an array i: </p> <pre>int[] i;</pre> <p>you then construct the array and assign it to i as follows:</p> <pre>i = new int[10];</pre> <p>The size of the array is in the brackets (in this example 10). It is common to do both operations in one statement:</p> <pre>int i[] = new int[10];</pre> <h3><a name="d0e155"></a>To initialize an array using loop iteration: </h3> <p>An example:</p> <pre> int array[] = new int[15]; for(int j=0; j<array.length;j++){ array[j]=j; }</pre> <h3><a name="d0e161"></a>Write code to initialize an array using the combined declaration and initialization format: </h3> <p>An example:</p> <pre>char c[]= new char[] {'a','b','c','d','e'};</pre> <p>or you can use</p> <pre>char c[]= {'a','b','c','d','e'};</pre> <table bg border="0" cellpadding="5" cellspacing="0" width="100%" style="color:black;"> <tbody> <tr> <td class="objh"><span style="color:white;"><a name="d0e172"></a>Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as <code>public</code>, <code>final</code>, <code>static</code>, <code>abstract</code> etc.). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers. </span></td></tr></tbody> </table> <p>This is fundamental Java. If you are unsure of this topic, see a textbook.</p> <p>A good but technical reference for these kinds of issues is the Java Language Specification (not for beginners). It is available at the following URL: <a href="http://java.sun.com/docs/books/jls/html/index.html">http://java.sun.com/docs/books/jls/html/index.html</a></p> <h3><a name="d0e193"></a>Some terms and their synonyms: </h3> <p><u>Scope/Visibility:</u> Where something can be seen / is accessable from. </p> <p><u>Nested/Inner Class:</u> A class whose code sits inside the body of another 'outer' class. It exists 'inside' the class in that it can see the private methods and variables. </p> <p><u>Instance/Member:</u> Means the method/variable/nested class belongs to each object which is an instance of the class. The term 'a member' is often used to refer to both methods and variables of this type. Cannot be accessed by statically i.e. without having an instance of the class. </p> <p><u>Class/Static:</u> The method/variable/nested class belongs to the class as opposed to the instances of the class. Can be used without creating an instance of the class, but static methods/nested class cannot use instancts variables/methods. </p> <p><u>Local/Automatic variable:</u> A variable which is declared within a method or as a parameter to the method. Cannot be seen outside the method. </p> <h3><a name="d0e215"></a>Scoping Types </h3> <p>"default" or "friendly" - this is where no modifier is used. Means something is only visible to classes in the same package.</p> <p><code>protected</code> - can only be accessed by classes in the same package or subclasses of this class. This frequently surprises experienced developers, especially those with a prior backround in the mutant hybrid of object orientated programming and assembly language known as 'C++' :-) To fair the name is misleading, as it sounds like it restricts access, where as it in fact it adds subclasses outside the package to the list of things that can access the item in question, as compared to using "default" access. </p> <p><code>public</code> - can be accessed by any other class. </p> <p><code>private</code> - can only be accessed from inside the class. </p> <h3><a name="d0e231"></a>Declare classes using the modifiers <code>public</code>, <code>abstract</code> or <code>final</code>: </h3> <p><code>public</code> - is visible outside of its package. Without this modifier, the class cannot be accessed outside its package. </p> <p><code>abstract</code> - cannot be instantiated, is allowed to contain <code>abstract</code> methods. </p> <p><code>final</code> - cannot be subsclassed. </p> <h3><a name="d0e257"></a>Using the modifiers <code>private</code>, <code>protected</code>, <code>public</code>, <code>static</code>, <code>final</code>, <code>native</code> or <code>abstract</code>: </h3> <p><code>private</code> - can only be accessed from inside the class. Private members are not inherited by subclasses. Inner classes can be declared <code>private</code>. </p> <p><code>protected</code> - can only be accessed by classes in the same package or subclasses of this class. </p> <p><code>public</code> - can be accessed by any other class. </p> <p><code>static</code> - belongs to the class rather than any particular instance of the class. For variables, effectively, there is just one copy of this variable for all instances of the class, and if an instance changes the value, the other instances see that new value. For methods, it means the method can be called without having created an instance, but within the methodd you cannot use the <code>this</code> keyword, or refer to instance variables and methods directly (without creating an instance and referring to the variable/class in that instance). For inner classes, it means they can be instantiated without having an instance of the enclosing class, but as with static methods, the methods of the inner class cannot refer to instance variables or methods of the enclosing class directly. </p> <p><code>final</code> - cannot be changed. Variables are constants, methods cannot be overridden, classes cannot be subclassed. Since Java1.1 you can declare the variable without assigning a value. Once you assign a value, you cannot change it. The are known as 'blank' finals, are frequently used for things like constants you wish to initialise from a configuration file. </p> <p><code>native</code> - a <b>method</b> which is not written in java and is outside the JVM in a library. </p> <p><code>abstract</code> - a <b>method</b> which is not implemented. Must be implemented in a subclass if that subclass is to be 'concrete' and allow people to instantiate it. </p> <h2><a name="d0e320"></a>Nested Classes </h2> <h3><a name="d0e322"></a>To define a non-static nested class either in a class or method scope: </h3> <p>Place the class definition (for the nested class) inside another class definition (the outer class) or a method.</p> <h3><a name="d0e326"></a>To define, in method scope, an anonymous nested class that implements a specified interface: </h3> <p>An anonymous nested class is defined where is it instantiated (in a method). An anonymous nested class must either implement an interface or extend a class, but the <code>implements</code> or <code>extends</code> keywords are not used. For example the following line causes the method to return an object which is an instance of an anonymous nested class: </p> <pre>return new SomeClass() { /*body of the anonymous class goes here*/ };</pre> <p>You might like to think of an anonymous nested class as part of a really long <code>new</code> statement, which happens to contain a class definition, and which is why it has a ";" at the end. The following example calls <code>someMethod()</code>, passing an instance of the anonymous nested class: </p> <pre>someMethod(new SomeClass() { /*body of the anonymous class goes here*/ });</pre> <p>In both cases <code>SomeClass()</code> is not the name of the anonymous class (anonymous means it has no name) rather is it the name of the class that you are extending or the interface you are implementing. These classes cannot define a constructor, as they do not have a name that you can use to declare the constructor method. If SomeClass() is a class, the default constructor of that class is called, if you want to use a non-default constructor instead, you supply arguments e.g.: </p> <pre>return new SomeClass(12) { /*body of the anonymous class goes here*/ };</pre> <p>will call the SomeClass constructor which takes an int.</p> <h3><a name="d0e357"></a>Write code in a non-static method of the outer class to construct an instance of the nested class. </h3> <code>Inner x = new Inner();</code> constructs an instance of Inner where Inner is a nested class defined in the current class. <h3><a name="d0e362"></a>Write code to construct an instance on a nested class where either no <code>this</code> object exists, or the current <code>this</code> object is not an instance of the outer class. </h3> You must create an instance of the outer class first. Given a class, Outer, containing a nested class Inner: <pre>Outer.Inner y = new Outer().new Inner();</pre> <p>The above creates an instance of Inner called y, but it had to construct an instance of Outer first. The following example creates the Outer instance on a separate line, the syntax in the second line is the one you use when you already have an instance of the outer class. </p> <pre>Outer x = new Outer(); Outer.Inner y = x.new Inner();</pre> If Inner is static, you can use: <pre>Outer.Inner I= new Outer.Inner();</pre> <h3><a name="d0e380"></a>State which variables and methods in enclosing scopes are accessible from methods of the inner class. </h3> <p>A non-static inner class has access to all member variables and methods of the containing class. If the inner class is defined inside a method, it has access to those method (a.k.a. automatic or local) variables which are declared <code>final</code>, in addition to the above. </p> <p>A static inner class is restricted in the same way as a static method: it cannot refer to instance variables and methods of the containing class directly (without creating an instance and referring to the variable/class in that instance). </p> <table bg border="0" cellpadding="5" cellspacing="0" width="100%" style="color:black;"> <tbody> <tr> <td class="objh"><span style="color:white;"><a name="d0e390"></a>For a given class, determine if a default constructor will be created and if so state the prototype of that constructor. </span></td></tr></tbody> </table> <p>The default constructor takes no arguments e.g. <code>classname()</code> where classname is the name of you class. A default constructor is automatically created only if you do not create <b>any</b> constructors in your class. If you create a non-default constructor (i.e. one that takes an argument), then you may have to create a default one yourself, if you want there to be one. </p> <p>All constructors call the default constructor of its parents class (if there is one), and so on up the hierarchy to Object, unless you specify a different constructor using <code>super(...)</code> (to use a constructor in the parent) or <code>this(...)</code>. If you use such calls, they must be the first thing in the constructor. </p>shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.comtag:blogger.com,1999:blog-15047166.post-1130258235893900092005-10-25T09:28:00.000-07:002005-10-25T09:37:17.623-07:00Should I use an Ide while learning java.Hmmmmmm. IDE's making coding really easy. And they save a lot of time. So If you are a serious Java developer coding without an IDE is realy foolish. But if you are trying to learn Java, and SCJP is an entry level certification, then dont even think about using an advanced IDE like Eclipse. In fact, I gave my SCJP exam after I had learnt a lot of Java and was using Eclipse for some time. I really had forgotten how to use the basic features, the features you are tested on in SCJP. So I had to leave using Eclipse for sometime. But a basic IDE with syntax highlighting, code explorer but certainly without code completion is ok. I would personaly recommend JCreator Lite. I did use it when I was beginning to use java.shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.comtag:blogger.com,1999:blog-15047166.post-1130256038416251652005-10-25T08:45:00.000-07:002005-10-25T09:00:38.466-07:00<a href=http://bobcat.webappcabaret.net/javachina/faq/01.htm> This site</a> has some extremely good java resources. And it is completely free.shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.comtag:blogger.com,1999:blog-15047166.post-1125480825973718182005-08-31T02:32:00.000-07:002005-08-31T02:33:46.020-07:00SCJP study notes:Chapter 8 java.lang package<h1> </h1> Originally written by Velmurugan Periasamy. Copyright belongs to him. <h1>Chapter 8 java.lang package</h1> <p class="Preformatted" style="text-align: center;" align="center"><b style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></b></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Object class is the ultimate ancestor of all classes. If there is no extends clause, compiler inserts ‘extends object’. The following methods are defined in Object class. All methods are public, if not specified otherwise.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <table class="MsoNormalTable" style="border: medium none ; margin-left: -30.6pt; border-collapse: collapse;" border="1" cellpadding="0" cellspacing="0"> <tbody><tr style="height: 11.65pt;"> <td style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 139.5pt; height: 11.65pt;" valign="top" width="186"> <p class="Preformatted" style="text-align: center;" align="center"><b style=""><span style="font-family: &quot;Times New Roman&quot;;">Method<o:p></o:p></span></b></p> </td> <td style="border-style: solid solid solid none; border-color: windowtext windowtext windowtext -moz-use-text-color; border-width: 1pt 1pt 1pt medium; padding: 0cm 5.4pt; width: 378pt; height: 11.65pt;" valign="top" width="504"> <p class="Preformatted" style="text-align: center;" align="center"><b style=""><span style="font-family: &quot;Times New Roman&quot;;">Description<o:p></o:p></span></b></p> </td> </tr> <tr style="height: 14.35pt;"> <td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 139.5pt; height: 14.35pt;" valign="top" width="186"> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">boolean equals(Object o)<o:p></o:p></span></p> </td> <td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 378pt; height: 14.35pt;" valign="top" width="504"> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">just does a == comparison, override in descendents to provide meaningful comparison<o:p></o:p></span></p> </td> </tr> <tr style="height: 14.35pt;"> <td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 139.5pt; height: 14.35pt;" valign="top" width="186"> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">final native void wait()<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">final native void notify()<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">final native void notifyAll()<o:p></o:p></span></p> </td> <td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 378pt; height: 14.35pt;" valign="top" width="504"> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">Thread control. Two other versions of wait() accept timeout parameters and may throw InterruptedException.<o:p></o:p></span></p> </td> </tr> <tr style="height: 14.35pt;"> <td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 139.5pt; height: 14.35pt;" valign="top" width="186"> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">native int hashcode()<o:p></o:p></span></p> </td> <td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 378pt; height: 14.35pt;" valign="top" width="504"> <p class="MsoNormal">Returns a hash code value for the object.</p> <p class="MsoNormal">If two objects are equal according to the <span class="CODE">equals</span> method, then calling the <span class="CODE">hashCode</span> method on each of the two objects must produce the same integer result. </p> </td> </tr> <tr style="height: 14.35pt;"> <td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 139.5pt; height: 14.35pt;" valign="top" width="186"> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">protected Object clone() throws CloneNotSupportedException<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">CloneNotSupportedException is a checked Exception<o:p></o:p></span></p> </td> <td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 378pt; height: 14.35pt;" valign="top" width="504"> <p class="MsoNormal">Creates a new object of the same class as this object. It then initializes each of the new object's fields by assigning it the same value as the corresponding field in this object. <b style="">No constructor is called.</b> </p> <p class="MsoNormal">The <span class="CODE">clone</span> method of class <span class="CODE">Object</span> will only clone an object whose class indicates that it is willing for its instances to be cloned. A class indicates that its instances can be cloned by declaring that it implements the <span class="CODE">Cloneable</span> interface. Also the method has to be made public to be called from outside the class. </p> <p class="MsoNormal">Arrays have a public clone method. </p> <p class="MsoNormal" style="margin-left: 36pt;">int ia[ ][ ] = { { 1 , 2}, null };</p> <p class="MsoNormal" style="margin-left: 36pt;">int ja[ ][ ] = (int[ ] [ ])ia.clone();</p> <p class="MsoNormal">A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared, so ia and ja are different but ia[0] and ja[0] are same.</p> </td> </tr> <tr style="height: 14.35pt;"> <td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 139.5pt; height: 14.35pt;" valign="top" width="186"> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">final native Class getClass()<o:p></o:p></span></p> </td> <td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 378pt; height: 14.35pt;" valign="top" width="504"> <p class="MsoNormal">Returns the runtime class of an object. </p> </td> </tr> <tr style="height: 14.35pt;"> <td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 139.5pt; height: 14.35pt;" valign="top" width="186"> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">String toString<o:p></o:p></span></p> </td> <td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 378pt; height: 14.35pt;" valign="top" width="504"> <p class="MsoNormal">Returns the string representation of the object. Method in Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `<span class="CODE">@</span>', and the unsigned hexadecimal representation of the hash code of the object. Override to provide useful information.</p> </td> </tr> <tr style="height: 14.35pt;"> <td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 139.5pt; height: 14.35pt;" valign="top" width="186"> <p class="MsoNormal">protected void finalize() throws </p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;">Throwable<o:p></o:p></span></p> </td> <td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 378pt; height: 14.35pt;" valign="top" width="504"> <p class="MsoNormal">Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. </p> <p class="MsoNormal">Any exception thrown by the <span class="CODE">finalize</span> method causes the finalization of this object to be halted, but is otherwise ignored. </p> <p class="MsoNormal">The <span class="CODE">finalize</span> method in <span class="CODE">Object</span> does nothing. A subclass overrides the <span class="CODE">finalize</span> method to dispose of system resources or to perform other cleanup.</p> </td> </tr> </tbody> </table> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Math class is final, cannot be sub-classed.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Math constructor is private, cannot be instantiated.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">All constants and methods are public and static, just access using class name.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Two constants PI and E are specified.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Methods that implement Trigonometry functions are native. <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">All Math trig functions take angle input in radians.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: &quot;Times New Roman&quot;;">Angle degrees * PI / 180 = Angle radians<o:p></o:p></span></p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]-->Order of floating/double values:</p> <p class="Preformatted" style="margin-left: 18pt;"><span style="font-family: &quot;Times New Roman&quot;;">-Infinity --> Negative Numbers/Fractions --> -0.0 --> +0.0 --> Positive Numbers/Fractions --> Infinity <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">abs – int, long, float, double versions available<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">floor – greatest integer smaller than this number (look below towards the floor)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">ceil – smallest integer greater than this number (look above towards the ceiling)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">For floor and ceil functions, if the argument is NaN or infinity or positive zero or negative zero or already a value equal to a mathematical integer, the result is the same as the argument.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">For ceil, if the argument is less than zero but greater than –1.0, then the result is a negative zero<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">random – returns a double between 0.0(including) and 1.0(excluding)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">round returns a long for double, returns an int for float. (closest int or long value to the argument)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt;"><span style="font-family: &quot;Times New Roman&quot;;">The result is rounded to an integer by adding<span style=""> </span>½ , taking the floor of the result, and casting the result to type int / long.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: &quot;Times New Roman&quot;;">(int)Math.floor(a + 0.5f)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: &quot;Times New Roman&quot;;">(long)Math.floor(a + 0.5d)</span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">double rint(double) returns closest double equivalent to a mathematical integer. If two values are equal, it returns the even integer value. rint(2.7) is 3, rint(2.5) is 2.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Math.min(-0.0, +0.0) returns –0.0, Math.max(-0.0, +0.0) returns 0.0, -0.0 == +0.0 returns true.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">For a NaN or a negative argument, sqrt returns a NaN.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Every primitive type has a wrapper class (some names are different – Integer, Boolean, Character)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Wrapper class objects are immutable.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">All Wrapper classes are final.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">All wrapper classes, except Character, have a constructor accepting string. A Boolean object, created by passing a string, will have a value of false for any input other than “true” (case doesn’t matter).<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Numeric wrapper constructors will throw a NumberFormatException, if the passed string is not a valid number. (empty strings and null strings also throw this exception)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">equals also tests the class of the object, so even if an Integer object and a Long object are having the same value, equals will return false.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">NaN’s can be tested successfully with equals method. <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: &quot;Times New Roman&quot;;">Float f1 = new Float(Float.NaN);<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: &quot;Times New Roman&quot;;">Float f2 = new Float(Float.NaN);<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: &quot;Times New Roman&quot;;">System.out.println( ""+ (f1 == f2)+"<span style=""> </span>"+f1.equals(f2)+ "<span style=""> </span>"+(Float.NaN == Float.NaN) );<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt;"><span style="font-family: &quot;Times New Roman&quot;;">The above code will print false true false.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Numeric wrappers have 6 methods to return the numeric value – intValue(), longValue(), etc.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">valueOf method parses an input string (optionally accepts a radix in case of int and long) and returns a new instance of wrapper class, on which it was invoked. It’s a static method. For empty/invalid/null strings it throws a NumberFormatException. For null strings valueOf in Float and Double classes throw NullPointerException.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">parseInt and parseLong return primitive int and long values respectively, parsing a string (optionally a radix). Throw a NumberFormatException for invalid/empty/null strings.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Numeric wrappers have overloaded toString methods, which accept corresponding primitive values (also a radix in case of int,long) and return a string.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Void class represents void primitive type. It’s not instantiable. Just a placeholder class.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Strings are immutable.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">They can be created from a literal, a byte array, a char array or a string buffer.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Literals are taken from pool (for optimization), so == will return true, if two strings are pointing to the same literal.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Anonymous String objects (literals) may have been optimized even across classes.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">A String created by new operator is always a different new object, even if it’s created from a literal.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">But a String can specify that its contents should be placed in a pool of unique strings for possible reuse, by calling intern() method. In programs that do a lot of String comparisons, ensuring that all Strings are in the pool, allows to use == comparison rather than the equals() method, which is slower.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">All string operations (concat, trim, replace, substring etc) construct and return new strings.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">toUpperCase and toLowerCase will return the same string if no case conversion was needed.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">equals takes an object (String class also has a version of equals that accepts a String), equalsIgnoreCase takes a string. <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Passing null to indexOf or lastIndexOf will throw NullPointerException, passing empty string returns 0, passing a string that’s not in the target string returns –1.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">trim method removes all leading and trailing white-space from a String and returns a new String. White-space means, all characters with value less than or equal to the space character – ‘\u0020’.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">String class is final.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">+ and += operators are overloaded for Strings. <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">reverse, append, insert are not String methods.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">String Buffers are mutable strings.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">StringBuffer is a final class.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">They can be created empty, from a string or with a capacity. An empty StringBuffer is created with 16-character capacity. A StringBuffer created from a String has the capacity of the length of String + 16. StringBuffers created with the specified capacity has the exact capacity specified. Since they can grow dynamically in size without bounds, capacity doesn’t have much effect.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">append, insert, setCharAt, reverse are used to manipulate the string buffer.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">setLength changes the length, if the current content is larger than specified length, it’s truncated. If it is smaller than the specified length, nulls are padded. This method doesn’t affect the capacity.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">equals on StringBuffer<span style=""> </span>does a shallow comparison. (same like ==) Will return true only if the objects are same. Don’t use it to test content equality<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">trim is not a StringBuffer method.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">There is no relationship between String and StringBuffer. Both extend Object class.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">String context means, ‘+’ operator appearing with one String operand. String concatenation cannot be applied to StringBuffers.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">A new String buffer is created.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">All operands are appended (by calling toString method, if needed)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">Finally a string is returned by calling toString on the String Buffer.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family: Symbol;"><span style="">·<span style="font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"> </span></span></span><!--[endif]--><span style="font-family: &quot;Times New Roman&quot;;">String concatenation process will add a string with the value of “null”, if an object reference is null and that object is appearing in a concatenation expression by itself. But if we try to access its members or methods, a NullPointerException is thrown. The same is true for arrays, array name is replaced with null, but trying to index it when it’s null throws a NullPointerException.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: &quot;Times New Roman&quot;;"><o:p> </o:p></span></p> <span style="font-size: 10pt; font-family: &quot;Times New Roman&quot;;"> </span>shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.comtag:blogger.com,1999:blog-15047166.post-1125480683055826412005-08-31T02:28:00.000-07:002005-08-31T02:50:39.826-07:00SCJP study notes: Chapter 7 Threads<h1> </h1> Originally written by Velmurugan Periasamy. Copyright belongs to him. <h1>Chapter 7 Threads</h1> <p class="MsoNormal"><o:p> (Some info is outdated if you are preparing for the SCJP 1.4 exam). </o:p></p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->Java is fundamentally multi-threaded.</p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->Every thread corresponds to an instance of java.lang.Thread class or a sub-class.</p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->A thread becomes eligible to run, when its start() method is called. Thread scheduler co-ordinates between the threads and allows them to run.</p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->When a thread begins execution, the scheduler calls its run method.</p> <p class="MsoNormal" style="margin-left: 36pt;">Signature of run method – public void run()</p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->When a thread returns from its run method (or stop method is called – deprecated in 1.2), its dead. It cannot be restarted, but its methods can be called. (it’s just an object no more in a running state) </p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->If start is called again on a dead thread, IllegalThreadStateException is thrown.</p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->When a thread is in running state, it may move out of that state for various reasons. When it becomes eligible for execution again, thread scheduler allows it to run.</p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->There are two ways to implement threads.</p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="">1.<span style=";font-family:&quot;;font-size:7;" > </span></span><!--[endif]-->Extend Thread class</p> <p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->Create a new class, extending the Thread class.</p> <p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->Provide a public void run method, otherwise empty run in Thread class will be executed.</p> <p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->Create an instance of the new class.</p> <p class="MsoNormal" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]-->Call start method on the instance (don’t call run – it will be executed on the same thread)</p> <p class="MsoNormal" style="margin-left: 18pt;"><o:p> </o:p></p> <p class="MsoNormal" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="">2.<span style=";font-family:&quot;;font-size:7;" > </span></span><!--[endif]-->Implement Runnable interface</p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Create a new class implementing the Runnable interface.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Provide a public void run method.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Create an instance of this class.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Create a Thread, passing the instance as a target – new Thread(object)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Target should implement Runnable, Thread class implements it, so it can be a target itself.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Call the start method on the Thread.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style=";font-family:&quot;;" ><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >JVM creates one user thread for running a program. This thread is called main thread. The main method of the class is called from the main thread. It dies when the main method ends. If other user threads have been spawned from the main thread, program keeps running even if main thread dies. Basically a program runs until all the user threads (non-daemon threads) are dead.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >A thread can be designated as a daemon thread by calling setDaemon(boolean) method. This method should be called before the thread is started, otherwise IllegalThreadStateException will be thrown.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >A thread spawned by a daemon thread is a daemon thread.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Threads have priorities. Thread class have constants MAX_PRIORITY (10), MIN_PRIORITY (1), NORM_PRIORITY (5)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >A newly created thread gets its priority from the creating thread. Normally it’ll be NORM_PRIORITY.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >getPriority and setPriority are the methods to deal with priority of threads.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Java leaves the implementation of thread scheduling to JVM developers. Two types of scheduling can be done.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">1.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Pre-emptive Scheduling.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt;"><span style=";font-family:&quot;;" >Ways for a thread to leave running state -<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >It can cease to be ready to execute ( by calling a blocking i/o method)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >It can get pre-empted by a high-priority thread, which becomes ready to execute.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >It can explicitly call a thread-scheduling method such as wait or suspend.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt;"><span style=";font-family:&quot;;" ><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Solaris JVM’s are pre-emptive.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Windows JVM’s were pre-emptive until Java 1.0.2<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt;"><span style=";font-family:&quot;;" ><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">2.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Time-sliced or Round Robin Scheduling<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >A thread is only allowed to execute for a certain amount of time. After that, it has to contend for the CPU (virtual CPU, JVM) time with other threads.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >This prevents a high-priority thread mono-policing the CPU.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >The drawback with this scheduling is – it creates a non-deterministic system – at any point in time, you cannot tell which thread is running and how long it may continue to run.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt;"><span style=";font-family:&quot;;" ><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Mactinosh JVM’s <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Windows JVM’s after Java 1.0.2 <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Different states of a thread:<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">1.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Yielding<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Yield is a static method. Operates on current thread.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Moves the thread from running to ready state.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >If there are no threads in ready state, the yielded thread may continue execution, otherwise it may have to compete with the other threads to run.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Run the threads that are doing time-consuming operations with a low priority and call yield periodically from those threads to avoid those threads locking up the CPU.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">2.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Sleeping<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Sleep is also a static method.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Sleeps for a certain amount of time. (passing time without doing anything and w/o using CPU) <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Two overloaded versions – one with milliseconds, one with milliseconds and nanoseconds.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Throws an InterruptedException.(must be caught)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >After the time expires, the sleeping thread goes to ready state. It may not execute immediately after the time expires. If there are other threads in ready state, it may have to compete with those threads to run. The correct statement is the sleeping thread would execute <i style="">some time after</i> the specified time period has elapsed.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >If interrupt method is invoked on a sleeping thread, the thread moves to ready state. The next time it begins running, it executes the InterruptedException handler.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">3.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Suspending<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Suspend and resume are instance methods and are deprecated in 1.2<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >A thread that receives a suspend call, goes to suspended state and stays there until it receives a resume call on it.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >A thread can suspend it itself, or another thread can suspend it.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >But, a thread can be resumed only by another thread.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Calling resume on a thread that is not suspended has no effect.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Compiler won’t warn you if suspend and resume are successive statements, although the thread may not be able to be restarted. <o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">4.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Blocking<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Methods that are performing I/O have to wait for some occurrence in the outside world to happen before they can proceed. This behavior is blocking.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >If a method needs to wait an indeterminable amount of time until some I/O takes place, then the thread should graciously step out of the CPU. All Java I/O methods behave this way.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >A thread can also become blocked, if it failed to acquire the lock of a monitor.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">5.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Waiting<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >wait, notify and notifyAll methods are not called on Thread, they’re called on Object. Because the object is the one which controls the threads in this case. It asks the threads to wait and then notifies when its state changes. It’s called a monitor.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Wait puts an executing thread into waiting state.(to the monitor’s waiting pool)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Notify moves one thread in the monitor’s waiting pool to ready state. We cannot control which thread is being notified. notifyAll is recommended.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >NotifyAll moves all threads in the monitor’s waiting pool to ready.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >These methods can only be called from synchronized code, or an IllegalMonitorStateException will be thrown. In other words, only the threads that obtained the object’s lock can call these methods.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style=";font-family:&quot;;" ><o:p> </o:p></span></p> <p class="Preformatted" style=""><span style=";font-family:&quot;;" ><o:p> </o:p></span></p> <p class="Preformatted" style=""><span style=";font-family:&quot;;" >Locks, Monitors and Synchronization<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Every object has a lock (for every synchronized code block). At any moment, this lock is controlled by <u>at most</u> one thread.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >A thread that wants to execute an object’s synchronized code must acquire the lock of the object. If it cannot acquire the lock, the thread goes into blocked state and comes to ready only when the object’s lock is available.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >When a thread, which owns a lock, finishes executing the synchronized code, it gives up the lock.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Monitor (a.k.a Semaphore) is an object that can block and revive threads, an object that controls client threads. Asks the client threads to wait and notifies them when the time is right to continue, based on its state. In strict Java terminology, any object that has some synchronized code is a monitor.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 18pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >2 ways to synchronize:<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 25.5pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">1.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Synchronize the entire method<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 43.5pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Declare the method to be synchronized - very common practice.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 43.5pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Thread should obtain the object’s lock.<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 25.5pt; text-indent: -18pt;"><!--[if !supportLists]--><span style=";font-family:&quot;;" ><span style="">2.<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Synchronize part of the method<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 43.5pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >Have to pass an arbitrary object which lock is to be obtained to execute the synchronized code block (part of a method).<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 43.5pt; text-indent: -18pt;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span style="">·<span style=";font-family:&quot;;font-size:7;" > </span></span></span><!--[endif]--><span style=";font-family:&quot;;" >We can specify “this” in