All classes inherit these methods: clone(), equals(), finalize(), getClass(), hashCode(), notify(), notifyAll(), toString(), wait()
toString()
public String toString()
returns a String representation of the object
in the Object class, returns a concatenation of the runtime class name and the hashcode
usually overridden to return something more appropriate
equals()
public boolean equals(Object obj)
in the Object class, returns true when the parameter obj is the same instance as the current object
usually overridden to more appropriate implementation
Ex: for String - checks for spelling
Ex: for BigDecimal - checks if value and scale are equal
hashCode()
public int hashCode()
returns an integer value that is the object's "hashcode"
used in "hashing" algorithms such as in HashSet and HashMap
rarely called directly
Best Practice: if you override equals, you must override hashcode otherwise, hashing algorithms fail
String and StringBuffer/StringBuilder
Java actually has TWO types of strings!
String - Immutable - once you create it, you can never change its value.
StringBuffer/StringBuilder - Mutable - these are the classes you should use if you need to do a lot of string manipulation.
String
The JVM maintains a pool of String objects. Each newly created String is stored in the pool. The instance lingers even if no references are pointing to it.
Great performance if there is no need to modify, since you don't incur the cost of instantiation.
Otherwise, performance is terrible. Each "modification" actually creates a new String.
The string pool is also the reason why you should never initialize a string with the "new" operator if the literal will do.
String a = "SELECT";
String b = "SELECT";
a == b true! a and b point to the same instance
String c = new String("SELECT");
String d = new String("SELECT");
c == b false! c and d point to different instances
String Methods
Notice that all methods for "string manipulation" will not modify the String instance. They will only return a new String.
Methods for searching for characters or patterns in a String: charAt, startsWith, endsWith, indexOf, lastIndexOf
Methods for comparing Strings: equals, equalsIgnoreCase, compareTo, compareToIgnoreCase
StringBuffer/StringBuilder
Maintains an internal character array, contents can be modified.
Great performance when modifications are needed.
Primitive Wrappers
Number
Short
Integer
Byte
Long
Float
Double
BigDecimal
Boolean
Character
Primitive Wrappers
Allow primitive values to participate in the world of objects.
For example, allows primitives to be stored in "Collections" (Java data structures)
Immutable - Just like String, once value is set, it cannot be changed.
Creating a Primitive Wrapper
Primitive wrappers are usually* created simply by calling the constructor and feeding the primitive value as a constructor.
Retrieving Primitive Values: Call one of the methods that end in "___Value"
Before Java 1.5, you can't use primitive wrappers directly in arithmetic nor comparison operators. You have to extract the primitive values first.
Autoboxing and Unboxing in Java 1.5
Java 1.5 automatically converts primitives to wrappers and wrappers to primitives
equals() method
Returns true if the object passed is of the same type and has the same value
Primitive Wrappers as Utility Classes
Primitive Wrappers also serve as "Utility Classes"*.
They contain static methods and static fields that are useful for the type of primitive that each of them wraps.
BigDecimal
Computers cannot accurately store decimal values!
Using floats and doubles are dangerous in financial applications!
BigDecimal accurately stores a decimal number to a specified level of precision.
Use the constructor that takes in a String to create precise values.
BigDecimal has methods for arithmetic operations: add, subtract, multiply, divide, min, max, negate, pow, abs, remainder, round
Collections will be discussed in more detail in a succeeding lecture.
Utility Classes
Classes that just hold useful routines.
All methods and fields are static.
Never instantiated.
System
Used to get some access the underlying operating system.