Site hosted by Angelfire.com: Build your free website today!
scjp section 8: fundamental classes in the java.lang package
 
  • Write code using the following methods of the java.lang.Math class: abs, ceil, floor, max, min, random, round, sin, COs, tan, sqrt.
  • Describe the significance of the immutability of String objects.
  • Describe the significance of wrapper classes, including making appropriate selections in the wrapper classes to suit specified behaviour 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

The java.lang.* Packages

  1. java.lang.Object
    • ultimate ancestor of all java classes (if a class doesn't explicitly 'extend' another, then java makes it extend Object)
    • inherited methods
      • wait(), notify(), notifyAll()
      • should override: boolean equals(Object that) (in object class is just ==, so only if refer to same object) and String toString() (in object class simply is the reference value)

  2. java.lang.Math
    • class is final (therefore can't extend it)
    • constructor is private (therefore can't create an instance)
    • all methods and constants are static
    • constants: Math.PI, Math.E
    • methods:
      • public static int abs(int i) - also for long, float, double
      • public static double ceil(double d) - smallest integer not less than d
      • public static double floor(double d) - largest integer not greater than d
      • public static int max(int i, int j) - also for long, float, double
      • public static int min(int i, int j) - also for long, float, double
      • public static double random() - >= 0.0 and <1.0
      • public static int round(float f)
      • public static long round(double d)
      • public static double sin(double rads) - also cos, tan, sqrt, pow

  3. The Wrapper Classes
    • are final
    • Boolean, Byte, Short, Character, Integer, Long, Float, Double
    • contains immutable value
    • constructors:
           e.g.   int i = 321; Integer wrappedInt = new Integer(i);, or new Integer(321)
      or Integer wrappedInt = new Integer("321");
      • ensure float string includes and 'f', and boolean is "True" or "False" (actually just checks for .equalsIgnoreCase("true"), otherwise sets it's value to false), and Float and Double can have +/-Infinity|NaN too
      • can't do this way for a char
      • all but Boolean can throw a NumberFormatException (a runtime exception)
    • extraction: byteValue(), shortValue(), intValue(), longValue(), floatValue(), doubleValue(), called by any wrapped number, returns a byte, short, int, long, float, double primitive respectively
    • characterInstance.charValue() returns a char, and booleanInstance.booleanValue() returns a boolean
    • valueOf(String s)
      • static method for all wrapper classes except Character
      • returns the wrapped version
      • e.g. Integer wrappedInt = Integer.valueOf("321");
    • static Byte|Short|Integer|Long|Float|Double.parseByte|Short|Int|Long|Float|Double(String s)
      • static method
      • returns the primitive
      • e.g. int i = Integer.parseInt("321");
      • can throw a NumberFormatException
    • getBoolean|Integer|Long(String name_of_system_property)
      • static method
      • returns the wrapped value
      • overloaded getInteger|Long(String name_of_system_property, int|long default_value)
    • all have good toString() and equals(Object that) methods
    • static Integer|Long.toBinary|Octal|HexString(int|long)

  4. java.lang.String
    • immutable - so String literal added to a pool of literals at compile time
    • String s = "text"; - kept in pool of strings (so '==' seems to work on strings created in this way)
    • String s = new String("text"); - makes a copy of the string during run-time (so new creates a unique copy, can put back in pool with s.intern();)
    • non-static methods:
      • char charAt(int i)
      • int indexOf(char c) and int lastIndexOf(char c)
      • boolean startsWith(String that) and boolean endsWith(String that)
      • boolean equals(String that), boolean equalsIgnoreCase(String that) and int compareTo(String that)
      • String substring(int start_index), String substring(int start_index, int end_index, String trim(), String toUpper|LowerCase(), String concat(String that)
      • int length()
      • String toString() - returns the executing object