public abstract class AbstractCollection { ... public abstract void add(); public abstract void length(); ... boolean isEmpty() {return length() == 0;} } public Set extends AbstractCollection { public void add() {...} public void length() {...} }
String s1 = "Hello", s2 = s1; s1 = s1.replace('H', 'J'); // Returns a NEW, different String
// Pre 1.5 Vector v = new Vector(); v.add(new Integer(5)); ... Integer num = new Integer(12); int i = num.intValue();
// Post 1.5 Vectorv = new Vector (); v.add(5); ... Integer num = 12; int i = num;
toString
Method
Employee e; ... System.out.println(e);and
Employee e; ... String s = "The value of e is: " + e;
package java.lang; class String { ... }
class MyApp { ... java.util.Vector v = new java.util.Vector(); // fully qualified name .. }
import java.util.Vector; class MyApp { ... Vector v = new Vector(); }
import java.util.*; class MyApp { ... Vector v = new Vector(); }
java.lang
System
)
java.util.Date = new java.util.Date(); // Date as used by the Java runtime java.sql.Date = new java.sql.Date(); // Date as required for database work
.throw
statement
Throwable
(i.e., something that can be thrown
Error
- Serious and fatal problems -- usually not caught
OutOfMemoryError
Exception
IOException
RuntimeException
IndexOutOfBoundException
NullPointerException
throw
statement:
class Stack { Object pop() throws StackException { ... if (isEmpty()) throw new StackException("pop attempted on empty stack"); ... } ... }
Throwable
try
block
class MyApp { ... Stack s = new Stack(); ... try { ... System.err.println(s.pop()); ... } catch (StackException e) { System.err.println("Ooops! + e); // Handle the exception System.exit(1); }