Free SCJP ebook!

Free SCJP Mock exams!( Registration required)
The free SCJP ebook
Witscale Test Center

Previous Page Next Page Contents Index

Valid XHTML 1.0!

java.lang package.


Object class
Class java.lang.Object is the root of the java class hierarchy. Every class has Object as a superclass. All objects, including arrays, inherit the methods of this class. More in ebook
Important methods.
  • toString() method of Object class returns string representation of the object.
     public String toString();            
    
  • equals() method of Object class compare the object references.
     public boolean equals(Object o); 
                   
    
    Note :- If objects of a particular class are to be stored and retrieved from a collection, it is important for that class to override equals() method and hashcode() methods. Since equals() in Object class only compare object references, it returns true only when both object references are pointing to the same object. Hence, this methods needs to be overridden to do logical equality test. (More)
  • The hashCode method defined by class Object returns distinct integers for distinct objects. This is typically implemented by converting the internal (memory) address of the object into an integer.
     public native int hashCode();   
                 
    
    The relation between equals() method and hashcode() is such that if two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.
    Note that it is generally necessary to override the hashCode() whenever equals() is overridden.
  • Object class has supporting methods for java threading mechanism. These methods are namely wait(), notify and notifyAll(). ( More in ebook)

Math Class More in ebook
  • Math is final class, so it cannot be sub classed.
  • Math class cannot be instantiated either. All public methods are static.
  • random() method of Math class returns a value of type double such that
    0.0 <= value < 1.0
    
  • abs() returns the absolute value of argument value. abs() is also overloaded for long, float and double.
    public static int abs(int intValue);
    
  • Truncating (floor) and rounding(ceil,round) functions.
    • ceil() and floor() are not overloaded for other data types.
       public static double ceil(double value)
       public static double floor(double value);
      
    • ceil() returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.
    • floor() returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.
    • round() is overloaded for float and double arguments. It rounds to the nearest integer and return value of type int or long.
  • Trigonometric functions sin(), cos(), tan() returns the trigonometric sine, cosine and tangent of the angle respectively. These take the double argument and returns a double value. Argument angle value is in radian.
  • sqrt() returns the correctly rounded positive square root of a double value. If the argument is NaN or less than zero, then the result is NaN.



More in ebook
Wrapper classes More in ebook

  • java.lang package provides standard library classes that are closely related to primitives. These are called as wrappers.
  • These classes are Byte, Short, Character, Integer, Long, Float, Double and Boolean.
  • Wrapper class names differ from the primitives only in the initial upper case letter.
    Exceptions are Integer, which wraps int and Character, which wraps char values.
  • All wrapper objects can be constructed by passing a string EXCEPT Character wrapper class for char data type. Most of these constructor throws NumberFormatException, which is a runtime exception.
  • Wrapper classes override equals() method. equals() on wrappers return false if both the objects are not instances of the same class. This statement is true even when the value they are wrapping has the same numerical value.
  • All wrapper objects are immutable. Once an object is created, the wrapped primitive value cannot be changed. More in ebook
  • Wrapper classes are final and hence cannot be sub classed.
Wrappers practical use
Primitives cannot be directly added to a collection such as Vector. Therefore, they are first wrapped into the corresponding wrapper as an object and then stored.

String More in ebook

  • java.lang.String is a final class hence it cannot be sub classed.
  • String object represents a sequence of 16 bit unicode characters.
  • Strings are immutable which means a String object has a constant (unchanging) value. More in ebook
  • String literals are created and stored on the pool of literals.
  • equals() in String class compares Strings. It true if the argument is not null and is a String object that represents the same sequence of characters as this object. More in ebook

StringBuffer
  • java.lang.StringBuffer is also a final class hence it cannot be sub classed.
  • It is mutable, which means its value can be changed. More in ebook
  • StringBuffer class does not override the equals() method. Therefore, it uses Object class' equals(), which only checks for equality of the object references. StringBuffer equals() does not return true even if the two StringBuffer objects have the same contents.
  • Please note that String's equal() method checks if the argument if of type string, if not it returns false.
    string.equals(sbuf); //always false. string is a String object.
    sbuf.equals(string); //always false. sbuf is a StringBuffer object.
    
String Concatenation and operator + More in ebook

Consider expression,
  expr1 + expr2 + expr3
And if any of the three expressions is of type String then ,
expr1 + expr2 + expr3 is equivalent to new StringBuffer().append(expr1).append(expr2).append(expr3).toString();


Related SCJP Objective

Section 8 : Fundamental Classes in java.lang package.
  1. Write code using the following methods of the java.lang.Math class: abs, ceil, floor, max, min, random, round, sin, cos, tan, sqrt.
  2. Describe the significance of the immutability of String objects.
  3. Describe the significance of wrapper classes, including making appropriate selections in the wrapper classes to suit specified behavior requirements, stating the result of executing a fragment of code that includes an instance of one of the wrapper classes, and writing code using the following methods of the wrapper classes (e.g., Integer, Double, etc.):
    • doubleValue
    • floatValue
    • intValue
    • longValue
    • parseXxx
    • getXxx
    • toString
    • toHexString

More in ebook
Previous Page Next Page Contents Index Top of this page

Valid CSS!