Java by API/java.lang/Object

Материал из Java эксперт
Версия от 14:41, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Object: finalize()

 
public class MainClass {
  public static void main(String[] args) throws Exception {
    Object f = new Object() {
      public void finalize() {
        System.out.println("Running finalize()");
      }
    };
    Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
        System.out.println("Running Shutdown Hook");
      }
    });
    f = null;
    System.gc();
    System.out.println("Calling System.exit()");
    System.exit(0);
  }
}





Object: getClass()

 
/*
 * Output: 
x is object of type: Class1
y is object of type: Class2
y"s superclass is Class1
 *  
 */
class Class1 {
  int a;
  float b;
}
class Class2 extends Class1 {
  double c;
}
public class MainClass {
  public static void main(String args[]) {
    Class1 x = new Class1();
    Class2 y = new Class2();
    Class clObj;
    clObj = x.getClass(); // get Class reference
    System.out.println("x is object of type: " + clObj.getName());
    clObj = y.getClass(); // get Class reference
    System.out.println("y is object of type: " + clObj.getName());
    clObj = clObj.getSuperclass();
    System.out.println("y"s superclass is " + clObj.getName());
  }
}





Object: getClass() (2)

 
/*
 * Output:
Public Methods:
 a1
 a2
 * */
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class MainClass {
  public static void main(String args[]) {
    try {
      MyClass a = new MyClass();
      Class c = a.getClass();
      System.out.println("Public Methods:");
      Method methods[] = c.getDeclaredMethods();
      for (int i = 0; i < methods.length; i++) {
        int modifiers = methods[i].getModifiers();
        if (Modifier.isPublic(modifiers)) {
          System.out.println(" " + methods[i].getName());
        }
      }
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}
class MyClass {
  public void a1() {
  }
  public void a2() {
  }
  protected void a3() {
  }
  private void a4() {
  }
}





Object: hashCode()

 
/*
 About to hashCode 2 objects.
MainClass@cf2c80 --> 13577344
java.awt.Color[r=68,g=136,b=204] --> -12285748
 */
public class MainClass {
  /** Some objects to hashCode() on */
  protected static Object[] data = { new MainClass(), new java.awt.Color(0x44, 0x88, 0xcc) };
  public static void main(String[] args) {
    System.out.println("About to hashCode " + data.length + " objects.");
    for (int i = 0; i < data.length; i++) {
      System.out.println(data[i].toString() + " --> " + data[i].hashCode());
    }
  }
}





Object: notifyAll()

 
/*
 * Output:
Calling wait
Calling notifyAll
Done
Done

 */
class SyncFlag {
  boolean flag;
  synchronized void k1() {
    if(flag == false) {
      flag = true;
      try {
        System.out.println("Calling wait");
        wait();
      }
      catch(Exception e) {
        e.printStackTrace();
      }
    }
    else {
      flag = false;
      System.out.println("Calling notifyAll");
      notifyAll();
    }
  }
}
class MyThread extends Thread {
  SyncFlag flag;
  MyThread(SyncFlag k) {
    this.flag = k;
  }
  public void run() {
    flag.k1();
    System.out.println("Done");
  }
}
public class MainClass {
  public static void main(String args[]) {
    SyncFlag k = new SyncFlag();
    new MyThread(k).start();
    new MyThread(k).start();
  }
}





Object: toString()

 
/*
 * Output:
 * Point[123456789,2147483647]
 * */
class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public String toString() {
    return "Point[" + x + "," + y + "]";
  }
}
public class MainClass {
  public static void main(String args[]) {
    Point p = new Point(123456789, 2147483647);
    System.out.print(p);
  }
}





Object: wait(long timeout)

 
public class Main {
  public static void main(String str[]) {
    final Object monitor = new Object();
    new Thread() {
      public void run() {
        try {
          synchronized (monitor) {
            System.out.println("10 seconds ...");
            monitor.wait(10000);
            System.out.println("Wait over");
          }
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    }.start(); 
  }
}