java04

包装类:
Java中的基本数据类型不是面向对象的(不是对象),所以有时候需要将基本数据类型转换成对象,包装类都位于java.lang包中,包装类和基本数据类型的关系(8种基本类型):
byte--Byte
boolean--Boolean
short--Short
char--Character
int--Integer
long--Long
float--Float
double--Double
-------------------------------------------------------
Integer  i = new Integer(1000);
Object o = new Object();
System.out.println(Integer.MAX_VALUE);//2147483647
System.out.println(Integer.toHexString(i));//转成16进制
Integer i2 = Integer.parseInt("234");
Integer i3 = new Integer("333");
System.out.println(i2.intValue());//234
String str = 234+"";



(拆箱和装箱只用于基本类型和包装类进行转换)
自动装箱:基本类型自动封装到与它相同类型的包装中,
Integer i = 100;本质上编译器编译时为我们添加了Integer i = new Integer(100);
自动拆箱:包装类自动转换成基本类型数据,
int a = new Integer(100);本质上编译器编译时为我们添加了int a = new Integer(100).intValue();
Integer a = 1000;  //jdk5.0之后 .  自动装箱,编译器帮我们改进代码:Integer a = new Integer(1000);

Integer b = null;
int c = b;  //自动拆箱,编译器改进:b.intValue();由于b是null所以报空指针异常。

System.out.println(c); 

Integer  d = 1234;
Integer  d2 = 1234;

System.out.println(d==d2);//false
System.out.println(d.equals(d2));//true

System.out.println("###################"); 
Integer d3 = -100;    //[-128,127]之间的数,仍然当做基本数据类型来处理。
Integer d4 = -100;
System.out.println(d3==d4);//true
System.out.println(d3.equals(d4));//true        
时间类:
java中的时间精确到毫秒,java中的时间也是数字。是从1970.1.1点开始到某个时刻的毫秒数,类型是long(2的63次方,很大有几亿年).

Date d = new Date();///at Jul 12 10:44:51 CST 2014
long t = System.currentTimeMillis();///1405133115506
System.out.println(t);//1405133115506
Date d2 = new Date(1000);

System.out.println(d2.toGMTString());//不建议使用1 Jan 1970 00:00:01 GMT
System.out.println(d2);//Thu Jan 01 08:00:01 CST 1970
d2.setTime(24324324);
System.out.println(d2.getTime());
System.out.println(d.getTime()<d2.getTime());



Calendar类:
Calendar  c = new GregorianCalendar();
c.set(2001, Calendar.FEBRUARY, 10, 12, 23, 34);//给日期设置值

c.set(Calendar.YEAR, 2001);
c.set(Calendar.MONTH, 1);   //二月
c.set(Calendar.DATE, 10);
c.setTime(new Date());

Date d = c.getTime();
System.out.println(d);//Sat Jul 12 14:13:22 CST 2014
System.out.println(c.get(Calendar.YEAR)); //获得年份,2014

//测试日期计算
c.add(Calendar.MONTH, -30);//减30个月
System.out.println(c);



可视化日历:
package cn.bjsxt.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
 * 可视化日历程序
 * @author dell
 *
 */
public class VisualCalendar {
public static void main(String[] args) {
    System.out.println("请输入日期(按照格式:2030-3-10):"); 
    Scanner scanner = new Scanner(System.in);
    String temp = scanner.nextLine();//2033-3-4
    
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date date = format.parse(temp);//Fri Mar 04 00:00:00 CST 2033
        Calendar calendar = new GregorianCalendar();
        
        calendar.setTime(date);
        int  day = calendar.get(Calendar.DATE);//4
        calendar.set(Calendar.DATE, 1);
        
        int maxDate = calendar.getActualMaximum(Calendar.DATE);//31
        System.out.println("日	一	二	三	四	五	六");
        
        for(int i=1;i<calendar.get(Calendar.DAY_OF_WEEK);i++){
            System.out.print('	');
        }
        
        for(int i=1;i<=maxDate;i++){
            if(i==day){
                System.out.print("*");
            }
            System.out.print(i+"	");
            int  w = calendar.get(Calendar.DAY_OF_WEEK);
            if(w==Calendar.SATURDAY){
                System.out.print('
');
            }
            calendar.add(Calendar.DATE, 1);
        }
        
    } catch (ParseException e) {
        e.printStackTrace();
    }
    
}
运行结果:
请输入日期(按照格式:2030-3-10):
2013-3-4
日    一    二    三    四    五    六
                    1    2    
3    *4    5    6    7    8    9    
10    11    12    13    14    15    16    
17    18    19    20    21    22    23    
24    25    26    27    28    29    30    
31    
File:
文件和目录路径名的抽象表示形式。
File f = new File("d:/src3/TestObject.java");//构造函数,f代表了这个文件,要操作这个文件就可以通过f来操作。
File f2 = new File("d:/src3");//构造函数实例化f2,f2是一个目录
File f3 = new File(f2,"TestThis.java");//在f2中建一个文件TestThis.java,f3就代表了TestThis.java这个文件。
File f5 = new File("d:/src3/aa/bb/cc/ee/ddd");
f3.createNewFile();//创建文件,不是创建文件夹。
f3.delete();//删除文件
f5.mkdir();//f5没有创建出来,创建的时候如果前面的父目录存在则创建,不存在则创建失败。
f5.mkdirs();//前面的父目录不存在也创建。
if(f.isFile()){
    System.out.println("是一个文件");
}
if(f2.isDirectory()){
    System.out.println("是一个目录");
}



public class FileTree {
    public static void main(String[] args) {
        //找一个自己硬盘上有用的文件夹
        File f = new File("d:/aaa/bbb/ccc/ddd");
        printFile(f, 0);
    }
    
    static void printFile(File file,int level){
        for (int i = 0; i < level; i++) {
            System.out.print("-");
        }
        System.out.println(file.getName()); 
        
        if(file.isDirectory()){
            File[]  files = file.listFiles();
            for (File temp : files) {
                printFile(temp, level+1);
            }
        }
    }
}
异常:
error是错误,不需要程序员处理,遇到error就重启。

Exception是异常,要处理。
Exception分为checked Exceotion 和unchecked Exveption.

finally代码经常用来关闭资源,一个try可以有多个catch,catch异常的捕获是有先后顺序的,局部变量只是在代码快有效,出了代码快就不能用了。

(return 一是返回值,二是结束运行。)


方法重写中声明异常原则:子类声明的异常范围不能超过父类声明的范围。
1.父类没有声明异常(父类没有抛出异常),子类也不能。
2.不可抛出原有方法抛出的异常类的父类或上层类。

异常往往在高层处理,谁调用你谁处理。

关键字:try,catch,finally,throws,throw


File f = new File("c:/tt.txt");
if (!f.exists())  {
    try {
        throw new FileNotFoundException("File can't be found!");//抛出一个异常,是throw不是throws 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
        
        
    
public class TestReadFile {
    public static void main(String[]  args) throws FileNotFoundException, IOException {//把异常抛给了JRE,
        String str;
        str = new TestReadFile().openFile();
        System.out.println(str);
    }
    
    String openFile() throws FileNotFoundException,IOException {//throws把异常向外抛,谁调用谁处理。
        FileReader reader = new FileReader("d:/a.txt");//FileReade,这个对象资源要关。
        char c = (char)reader.read();//读取一个字符
        char c1 = (char)reader.read();//读取一个字符
        char c2 = (char)reader.read();//读取一个字符
        char c3 = (char)reader.read();//读取一个字符
        System.out.println(""+c+c1+c2+c3);
        return ""+c;
    }
}    
        
        
        



//自定义异常
public class MyException extends Exception {
    
    public MyException(){
        
    }
    
    public MyException(String message){
        super(message);
    }
    
}

class TestMyException{
    void test()throws MyException{
        ///
    }
    
    public static void main(String[] args) {
        try {
            new TestMyException().test();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
}




class A {    
    public void method() throws IOException {    }
}
//方法重写的时候不要超了父类声明的异常的范围(编译通不过)
class B extends A {        public void method() throws FileNotFoundException {    }
}

class C extends A {        public void method() {    }
}

class D extends A {        public void method() throws Exception {    }     //超过父类异常的范围,会报错!
}

class E extends A {        public void method() throws IOException, FileNotFoundException {    }//没超过范围
}
class F extends A {        public void method() throws IOException, ArithmeticException {      }
}
class G extends A {        public void method() throws IOException, ParseException {    }//超范围
}

原文地址:https://www.cnblogs.com/yaowen/p/4833533.html