Rectangle[] r = new Rectangle[10];
Shape[] s = r;
Integer Short Long Byte
Character Float Double Boolean
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(13); // calls new Integer(13)
int n = numbers.get(0); // calls intValue();
Object e = new Rectangle();
Class c = e.getClass();
System.out.println(c.getName()); // prints java.awt.Rectangle
double[] a = new double[10];
Class c = a.getClass();
if (c.isArray())
System.out.println(c.getComponentType());
// prints double
[D for double[])
[[java.lang.String; for String[][]
public class Employee
{
public String toString()
{
return getClass().getName()
+ "[name=" + name
+ ",salary=" + salary
+ "]";
}
...
}
Employee[name=Harry Hacker,salary=35000]
public class Manager extends Employee
{
public String toString()
{
return super.toString() + "[department=" + department + "]"; } ... }
Manager[name=Dolly Dollar,salary=100000][department=Finance]
/**
Searches for the first occurrence of the given argument,
testing for equality using the equals method.
@param elem an object.
@return the index of the first occurrence
of the argument in this list; returns -1 if
the object is not found.
*/
public int indexOf(Object elem)
{
if (elem == null)
{
for (int i = 0; i < size; i++)
if (elementData[i] == null) return i;
}
else
{
for (int i = 0; i < size; i++)
if (elem.equals(elementData[i])) return i;
}
return -1;
}
public class Employee
{
public boolean equals(Object otherObject)
// not complete--see below
{
Employee other = (Employee)otherObject;
return name.equals(other.name)
&& salary == other.salary;
}
...
}
public class Manager
{
public boolean equals(Object otherObject)
{
Manager other = (Manager)otherObject;
return super.equals(other)
&& department.equals(other.department);
}
}
public boolean equals(Object o)
{
if (o == this) return true;
if (!(o instanceof Set)) return false;
Collection c = (Collection) o;
if (c.size() != size()) return false;
return containsAll(c);
}
public class Object
{
public boolean equals(Object obj)
{
return this == obj;
}
...
}
public boolean equals(Object otherObject)
{
if (this == otherObject) return true;
if (otherObject == null) return false;
if (getClass() != otherObject.getClass()) return false;
...
}
public class Employee
{
public int hashCode()
{
return 11 * name.hashCode()
+ 13 * new Double(salary).hashCode();
}
...
}
public class Employee
{
public Object clone()
{
return super.clone(); // not complete
}
...
}
public interface Cloneable
{
}
public class Employee
implements Cloneable
{
public Object clone()
{
try
{
return super.clone();
}
catch(CloneNotSupportedException e)
{
return null; // won't happen
}
}
...
}
public class Employee
implements Cloneable
{
public Object clone()
{
try
{
Employee cloned = (Employee)super.clone();
cloned.hireDate = (Date)hiredate.clone();
return cloned;
}
catch(CloneNotSupportedException e)
{
return null; // won't happen
}
}
...
}
Employee[] staff = new Employee[2];
staff.add(new Employee(...));
staff.add(new Employee(...));
ObjectOutputStream out
= new ObjectOutputStream(
new FileOutputStream("staff.dat"));
out.writeObject(staff);
out.close();
Field[] fields = Math.class.getDeclaredFields();
for (Field f : fields)
if (Modifier.isStatic(f.getModifiers()))
System.out.println(f.getName());
Constructor[] constructors = Rectangle.clsss.getDeclaredConstructors(); for (Constructor c : constructors )
{
Class[] params = c.getParameterTypes();
System.out.print("Rectangle(");
boolean first = true;
for (Class p : params)
{
if (first) first = false; else System.out.print(", ");
System.out.print(p.getName());
}
System.out.println(")");
}
Rectangle()
Rectangle(java.awt.Rectangle)
Rectangle(int, int, int, int)
Rectangle(int, int)
Rectangle(java.awt.Point, java.awt.Dimension)
Rectangle(java.awt.Point)
Rectangle(java.awt.Dimension)
Method m = Rectangle.class.getDeclaredMethod(
"contains", int.class, int.class);
Constructor c = Rectangle.class.getDeclaredConstructor();
Method m = PrintStream.class.getDeclaredMethod(
"println", String.class);
m.invoke(System.out, "Hello, World!");
Class c = obj.getClass();
Field f = c.getDeclaredField(name);
f.setAccessible(true);
Object value = f.get(obj);
f.set(obj, value);
java.util.concurrent.atomic.AtomicLong seed=214557382433043 double nextNextGaussian=0.0 boolean haveNextNextGaussian=false
After calling nextInt:
java.util.concurrent.atomic.AtomicLong seed=2341557616363298 double nextNextGaussian=0.0 boolean haveNextNextGaussian=false
Object value = Array.get(a, i);
Array.set(a, i, value);
int n = Array.getLength(a);
Object a = Array.newInstance(type, length);
Object anew = Array.newInstance( a.getClass().getCompnentType(), 2 * Array.getLength(a) + 1); System.arrycopy(a, 0, anew, 0, Array.getLength(a)); a = anew;
public class ArrayList<E>
{
public E get(int i) { . . . }
public E set(int i, E newValue) { . . . }
. . .
private E[] elementData;
}
public class Utils
{
public static <E> void fill(ArrayList<E> a, E value, int count)
{
for (int i = 0; i < count; i++)
a.add(value);
}
}
ArrayList<String> ids = new ArrayList<String>();
Utils.fill(ids, "default", 10); // calls Utils.<String>fill
public static <E> void append(ArrayList<E> a, ArrayList<E> b, int count)Cannot append an ArrayList<Rectangle> to an ArrayList<Shape>
{
for (int i = 0; i < count && i < b.size(); i++)
a.add(b.get(i));
}
public static <E, F extends E> void append(
ArrayList<E> a, ArrayList<F> b, int count)
{
for (int i = 0; i < count && i < b.size(); i++)
a.add(b.get(i));
}
public static <E> void append(
ArrayList<E> a, ArrayList<? extends E> b, int count)
{
for (int i = 0; i < count && i < b.size(); i++)
a.add(b.get(i));
}
public static <E extends Comparable<E>> E getMax(ArrayList<E> a)
{
E max = a.get(0);
for (int i = 1; i < a.size(); i++)
if (a.get(i).compareTo(max) > 0) max = a.get(i);
return max;
}
public static <E extends Comparable<? super E>> E getMax(ArrayList<E> a)
public class ArrayList
{
public Object get(int i) { . . . }
public Object set(int i, Object newValue) { . . . }
. . .
private Object[] elementData;
}
public static Comparable getMax(ArrayList a)
// E extends Comparable<? super E> erased to Comparable
public static <E> void fillWithDefaults(ArrayList<E>,
Class<? extends E> cl, int count)
throws InstantiationException, IllegalAccessException
{
for (int i = 0; i < count; i++)
a.add(cl.newInstance());
}
Name in
Design Pattern |
Actual Name
(Beans) |
Client |
Builder tool |
Facade |
Main bean class with which the
tool interacts |
SubsystemClass |
Class used to implement bean functionality |