tag: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: "Times New Roman";"><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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";"><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: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";">final native void wait()<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: "Times New Roman";">final native void notify()<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";">protected Object clone() throws CloneNotSupportedException<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: "Times New Roman";"><o:p> </o:p></span></p> <p class="Preformatted" style=""><span style="font-family: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";"><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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">-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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman";">(int)Math.floor(a + 0.5f)<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: "Times New Roman";">(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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";"><o:p> </o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: "Times New Roman";"><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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";">Float f1 = new Float(Float.NaN);<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: "Times New Roman";">Float f2 = new Float(Float.NaN);<o:p></o:p></span></p> <p class="Preformatted" style="margin-left: 36pt;"><span style="font-family: "Times New Roman";">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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";"><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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">+ 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: "Times New Roman"; 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: "Times New Roman";">reverse, append, insert are not String methods.<o:p></o:p></span></p> <p class="Preformatted" style=""><span style="font-family: "Times New Roman";"><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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";"><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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";"><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: "Times New Roman"; 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: "Times New Roman";">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: "Times New Roman";"><o:p> </o:p></span></p> <span style="font-size: 10pt; font-family: "Times New Roman";">
</span>shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.com