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("日\t一\t二\t三\t四\t五\t六");
        
        for(int i=1;i<calendar.get(Calendar.DAY_OF_WEEK);i++){
            System.out.print('\t');
        }
        
        for(int i=1;i<=maxDate;i++){
            if(i==day){
                System.out.print("*");
            }
            System.out.print(i+"\t");
            int  w = calendar.get(Calendar.DAY_OF_WEEK);
            if(w==Calendar.SATURDAY){
                System.out.print('\n');
            }
            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 {    }//超范围
}
复制代码



本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4833533.html,如需转载请自行联系原作者

相关文章
|
4月前
|
设计模式 算法 安全
Java (3)
Java (3)
27 0
|
4月前
|
Java
L2-2 小字辈(Java)
L2-2 小字辈(Java)
39 0
|
Java
Java 中do...while()的使用
Java 中do...while()的使用
56 0
|
监控 Dubbo 安全
JAVA问答8
JAVA问答8
100 0
|
Java
Java常见的坑(二)
你猜上述程序输出的是什么? 是 ABC easy as 123 吗? 你执行了输出操作,你才发现输出的是 ABC easy as [C@6e8cf4c6 ,这么一串丑陋的数字是什么鬼? 实际上我们知道字符串与任何数值的相加都会变为字符串,上述事例也不例外, numbers输出其实实际上是调用了Object.toString()方法,让numbers转变为'[c' + '@' + 无符号的十六进制数。
78 0
1096 大美数(JAVA)
若正整数 N 可以整除它的 4 个不同正因数之和,则称这样的正整数为“大美数”。本题就要求你判断任一给定的正整数是否是“大美数”。
1096 大美数(JAVA)
1105 链表合并(JAVA)
给定两个单链表 L1​=a1​→a2​→⋯→an−1​→an​ 和 L2​=b1​→b2​→⋯→bm−1​→bm​。如果 n≥2m,你的任务是将比较短的那个链表逆序,然后将之并入比较长的那个链表,得到一个形如 a1​→a2​→bm​→a3​→a4​→bm−1​⋯ 的结果。例如给定两个链表分别为 6→7 和 1→2→3→4→5,你应该输出 1→2→7→3→4→6→5。
1105 链表合并(JAVA)
1100 校庆(JAVA)
2019 年浙江大学将要庆祝成立 122 周年。为了准备校庆,校友会收集了所有校友的身份证号。现在需要请你编写程序,根据来参加校庆的所有人士的身份证号,统计来了多少校友。
1100 校庆(JAVA)
|
Java 程序员 C++