Witscale Test Center

Chapter summary


Chapter summary

This chapter covers entire eighth exam objective and a part of the fifth objective (section 5.2). We learned about the some of the fundamental classes in java.lang package. We quickly overviewed the API of Object class, Math class, String class and wrapper classes. We also learned the details of the key methods of these classes and their use. We saw how the string objects are immutable but the StringBuffer objects are not. Then we saw how the wrapper classes wrap primitives and provide generic methods for converting a string into primitives or the other way round. Finally, we learned about comparing objects and the difference between the == operator and the equals() method. This is brief summary of the important concepts in this chapter.

 

       The java.lang package.

§         The java.lang package contains the fundamental classes of the Java language. 

§         This package is imported automatically in every Java program.

 

       The Object class

§         Class java.lang.Object is the root of the Java class hierarchy. Every class has Object as a superclass including the arrays.

§         All objects, including arrays, inherit the methods of this class.

§         Important methods of Object class

§         toString() :-  returns string representation of the object.

public String toString()

 

§         equals() :-  Compare the object by comparing the object references.

public boolean equals(Object obj)

 

§         hashCode():- returns a hashcode value ( integer) for a object.

public native int hashCode())

 

§         In addition, this class has threading related wait(), notify and notifyAll() methods and also a finalize() method invoked just before the object is garbage collected.         

 

       The Math Class

§         The Math class is final. Therefore, it cannot be subclassed.

§         The Math class cannot be instantiated either. It does not have public constructor. It can still be used, as all its public methods are static.

§         Important static methods of Math class

§         random():- returns a value of type double such that 0.0 <= value < 1.0.

public static double random()

 

§         abs() :-  returns the absolute value of argument value. abs() is overloaded to take int, long, float and double arguments. Signature ( of  int version) is

public static int abs(int a)

 

§         max() :-  returns maximum of two arguments. It is overloaded to take int, long, float or double arguments.

§         min() :- returns minimum of two arguments. It is overloaded to take int, long, float or double arguments.

§         Math class has one truncating (floor) and two rounding (ceil,round) methods.

§         ceil() and floor() are not overloaded for other data types.

§         ceil() returns the smallest integer (as a double value) that is not smaller than the argument.

             public static double ceil(double a)

 

§         floor() returns the largest integer (as a double value) that is not larger than the argument.

public static double floor(double a)

 

§         round() is overloaded for float and double arguments. It rounds to the nearest integer and returns the value of type int or long.

             public static int round(float a)

      public static long round(double a)

 

§         Trigonometric functions sin(), cos(), tan() returns the trigonometric sine, cosine and tangent of the angle respectively. These take the angle as double argument and returns a double value. Argument angle is in radians.

§         The 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.

§         The floating-point numbers (float or double values) can be divided by 0.0 without any errors. The result is either positive or negative infinity and is represented in Float and Double wrapper classes.

§         NaN is not equal to anything, not even itself.

 

       The String class

§         java.lang.String is a final class hence it cannot be subclassed.

§         String object represents a sequence of 16 bit Unicode characters.

§         Strings are immutable which means a String object has a constant (unchangeable) value.

§         Because strings are immutable, they always create a new string if any of its contents are changed.

§         String objects are immutable but the string reference variables are not. If you redirect a String reference to a new String object, the old String could be lost.

§         String literals are created and stored on the pool of literals.

§         The equals() in String class compares Strings. It returns true if the argument is a String object that represents the exact same sequence of characters as this object.

§         Strings have a method named length() whereas the arrays have an attribute named length.

 

       The StringBuffer class.

§         java.lang.StringBuffer is also a final class hence it cannot be subclassed.

§         A StringBuffer object is mutable, which means its value can be changed.

§         The StringBuffer class does not override the equals() method. Therefore, it inherits the  Object class’s equals()method which only checks for equality of the object references. Therefore if equals() is invoked on StringBuffer object, it does not return true even whenthe two StringBuffer objects have the same contents.

§         If we pass String object to StringBuffer’s equals() method  or StringBuffer object to String’s equals()  method, the result is always false irrespective of their contents.

 

       The wrapper classes

§         java.lang package provides a group of classes to represent primitives as objects. These are called as the wrapper classes.

§         Wrapper classes are final and hence cannot be sub classed.

§         These classes are six numeric wrapper classes, Byte, Short, Integer, Long, Float, Double and two non-numeric wrapper classes Character and Boolean.

§         Most wrapper class have a name exactly same as the primitive type they wrap except the initial letter is in uppercase. Exceptions are Integer, which wraps int and Character, which wraps char value.

§         All wrapper objects (except a Character object) can be constructed either

§         by passing a string argument Or

§         by passing primitive value as argument.

§         The constructor that accepts string throws NumberFormatException,if the string does not represent a primitive value correctly. The Character wrapper class has only one constructor that take char value as argument.

§         The three most important method families in wrapper classes are

§         The instance methods xxxValue() that takes no arguments, returns a primitive

§         The static methods parseXxx() that takes a String, returns a primitive. This method can throw NumberFormatException,if the string does not represent a primitive value correctly.

§         The static valueOf() methods that takes a String, returns a wrapped object. This method too can throw NumberFormatException,if the string does not represent a primitive value correctly.

§         All wrapper objects are immutable. Once an object is created, the wrapped primitive value cannot be changed.

 

       The equals() method in Object, String, StringBuffer and wrapper classes

§         The  == operator compares object references whereas the equals() method can be overridden to compare object contents.

§         The == operator can be applied to primitives as well as object references.

§         The equals() method can be invoked only on object references.

§         The String and Wrapper classes override equals() to compare their contents with the argument.

§         The StringBuffer class does not override the equals() method. Hence it uses Object class’s equals() method which compares exactly same as the == operator.

§         Wrapper classes override equals() method. The equals() on wrappers return false if both the objects are not of same type. This is true even when the value they are wrapping has the same numerical value.