Java类库实践点滴(一) —— Logging、Object、assert
Java类库实践点滴(一) —— Logging、Object、assert
一、java.util.Logging:
例子:
输出:
Oct 14, 2003 10:33:18 AM TestLogging main
WARNING: sample warning
Oct 14, 2003 10:33:18 AM TestLogging main
SEVERE: severe log
Oct 14, 2003 10:33:18 AM TestLogging main
WARNING: warning log
Oct 14, 2002 10:33:18 AM TestLogging main
INFO: info log
二、java.lang.Object类
首先需要注意:
1. 每一个class扩展自Object
2. 不能在Object变量中存储基本类型
常用的Object的方法:
常用的线程的对象方法:
垃圾收集的对象方法:
三、 断言的基本使用
编译和运行:
javac -source 1.4 TestAssertions.java
java -ea TestAssertions
输出:
test one
after assertion
test two
Exception in thread "main" java.lang.AssertionError: bad Hal
at TestAssertions.testPodDoor(TestAssertions.java:19)
at TestAssertions.main(TestAssertions.java:33)
Java类库实践点滴(一) —— Logging、Object、assert
一、java.util.Logging:
例子:
- //测试Java1.4中的日志新功能
- import java.util.logging.*;
- public class TestLogging {
- //设置logger
- static Logger testLog = Logger.getLogger("test.log");
- public static void main(String[] args) {
- //测试Warn级别的日志
- testLog.warning("sample warning");
- //测试所有级别
- //缺省在控制台上显示 SEVERE, WARNING, INFO级别的日志
- testLog.log(Level.SEVERE, "severe log");
- testLog.log(Level.WARNING, "warning log");
- testLog.log(Level.INFO, "info log");
- testLog.log(Level.CONFIG, "config log");
- testLog.log(Level.FINE, "fine log");
- testLog.log(Level.FINER, "finer log");
- testLog.log(Level.FINEST, "finest log");
- testLog.log(Level.ALL, "all log");
- }
- }
输出:
Oct 14, 2003 10:33:18 AM TestLogging main
WARNING: sample warning
Oct 14, 2003 10:33:18 AM TestLogging main
SEVERE: severe log
Oct 14, 2003 10:33:18 AM TestLogging main
WARNING: warning log
Oct 14, 2002 10:33:18 AM TestLogging main
INFO: info log
二、java.lang.Object类
首先需要注意:
1. 每一个class扩展自Object
2. 不能在Object变量中存储基本类型
常用的Object的方法:
常用的线程的对象方法:
- objectWithThreadWaiting.notify();
- //唤醒当前线程
- objectWithThreadWaiting.notifyAll();
- //唤醒所有线程
- objectToMakeThreadWait.wait();
- //线程一直处于sleep状态,直到出现 notify()或 notifyAll()为止
- objectToMakeThreadWait.wait(longTimeToWaitInMilliseconds);
- //当前线程一直处于sleep状态,直到出现notify(), notifyAll(),
- //或过了longTimeToWaitInMilliseconds秒延迟之后为止
- objectToMakeThreadWait.wait(longTimeToWaitInMilliseconds, intTimeToWaitInNanoseconds);
- //当前线程一直处于sleep状态,直到出现notify(), notifyAll(),
- //或longTimeToWaitInMilliseconds + intTimeToWaitInNanoseconds 秒延迟之后为止
垃圾收集的对象方法:
- objectForGarbageCollection.finalize();
- //当没有对象引用存在时,执行垃圾收集
三、 断言的基本使用
- //jdk1.4 中的新的assert 语句和关键字
- // 用 -source 作为1.4 编译的标志
- // 用 -ea 标志来运行
- public class TestAssertions {
- static boolean goodHalOpen = true;
- static boolean badHalOpen = false;
- static boolean openPodDoor = false;
- //设置断言
- static void testPodDoor() {
- /*注意: 在assert语句中,如果冒号之前的表达式等于false,那么
- 就抛出异常java.lang.AssertionError
- 当抛出AssertionError时,将输出冒号之后的表达式的文本。 */
- assert openPodDoor : "bad Hal";
- System.out.println("after assertion");
- System.out.println(" ");
- }
- public static void main(String[] args) {
- System.out.println("test one");
- openPodDoor = goodHalOpen;
- testPodDoor();
- System.out.println("test two");
- openPodDoor = badHalOpen;
- testPodDoor();
- }
- }
编译和运行:
javac -source 1.4 TestAssertions.java
java -ea TestAssertions
输出:
test one
after assertion
test two
Exception in thread "main" java.lang.AssertionError: bad Hal
at TestAssertions.testPodDoor(TestAssertions.java:19)
at TestAssertions.main(TestAssertions.java:33)