四常用类

包装类的基本用法

      为什么需要包装类(Wrapper Class):java并不是纯面向对象的语言。Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的。但是我们在实际使用中经常需要将基本数据转化成对象,便于操作。比如:集合的操作中。这时,我们就需要将基本类型数据转化成对象!
       
 1 package com.zqf.wrapper;
 2 public class TestInteger {
 3  public static void main(String[] args) {
 4   Integer i1 = new Integer(123);
 5   Integer i2 = new Integer("123");
 6   System.out.println("i1==i2:"+(i1==i2));    //false
 7   System.out.println("i1.equals(i2):"+i1.equals(i2));   //内容     true
 8   System.out.println(i2.toString());    //说明Integer类重写了toString方法
 9   Integer i3 = new Integer(128);
10   System.out.println(i1.compareTo(i3));  //-1
11   System.out.println(i1.compareTo(i2));  //0
12   System.out.println(i3.compareTo(i2));  //1
13   
14   //(1)Integer- ->int       包装类对象,intValue()
15   int i= i1.intValue();
16   System.out.println(Integer.max(10, 20));
17   //(2)int - ->Integer 
18   Integer i4 = Integer.valueOf(123);
19         //(3)String  - - >int      包装类类名.parseInt(String s)
20   int ii=Integer.parseInt("234");
21   //(4)int -->String
22   String str= ii+"";
23   String s = String.valueOf(ii);
24   //(5)String - - > Integer
25   Integer i5 = new Integer("345");
26   //(6)Integer -- >String 
27   String ss = i5.toString();
28   System.out.println(i5.toString());
29  }
30 }
 

   自动装箱和拆箱

      自动装箱  auto-boxing
 基本类型就自动地封装到与它相同的类型的包装类中
1  Interger i = 100;

编译器调用了valueOf()方法

 1  Integer i= Integer.valueOf(100);
 2 package com.zqf.wrapper;
 3 public class TestAutoBoxing {
 4  public static void main(String[] args) {
 5   Integer a= 100;
 6   Integer b= 100;
 7   System.out.println(a==b); //数值相等
 8   Integer aa= 1000;
 9   Integer bb = 1000;
10   System.out.println("aa==bb"+(aa==bb)); //false
11  }
12 }

   自动拆箱 UNboxing

      包装类对象自动转换成基本类型数据
      
int a = new Integer(100);

   编译器为我们添加了:

int a = new Integer(100).intValue();
 1 package com.zqf.wrapper;
 2 public class TestUniBoxing {
 3  public static void main(String[] args) {
 4   int a = new Integer(100);
 5   //以上的代码相等于Integer(100).intValue();
 6   
 7   Integer b= null;
 8   int c=b; //自动拆箱
 9   int cc = b.intValue();
10   
11   System.out.println(c);
12  }
13 }

String类底层分析_JDK源码分析

    String的底层数据结构
 String的底层数据结构是char类型的数组
 String的相应方法的实现实际上就是对数组的一个操作 
 

Math类

Math类的常用方法:
 1 package com.zqf.math;
 2 public class TestMath {
 3  public static void main(String[] args) {
 4   System.out.println("绝对值:"+Math.abs(23)+"	"+"绝对值:"+Math.abs(-23));
 5   System.out.println("向上取整再转double:"+Math.ceil(23.00001)+"	"+Math.ceil(-9.999999));
 6   System.out.println("向下取整再转double:"+Math.floor(23.9999)+"	"+Math.floor(-23.000001));
 7   System.out.println("取最值:"+Math.max(10, 20)+"	"+Math.min(3, 40));
 8   System.out.println("5的二次方:"+Math.pow(5, 2));
 9   System.out.println("随机数[0,1):"+Math.random());
10   int ran=(int)(Math.random()*9000)+1000;
11   System.out.println("1000-9000之间的随机数:"+ran);
12   System.out.println("四舍五入:"+Math.round(34.567));
13  }
14 }

File类

  文件和目录路径名的抽象表示形式,一个File对象可以代表一个文件或目录

  可以实现获取文件和目录属性等功能

  可以实现对文件和目录的创建,删除等功能

  File不能访问文件内容

   File类操作文件

 1 package com.zqf.file;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 public class TestFile {
 7     //使用File操作文件
 8     public static void main(String[] args) throws IOException {
 9         //创建File类的对象
10         File f1 = new File("C:\a.txt");    //绝对路径
11         File f2 = new File("C:/a.txt");        
12         File f3 = new File("a.txt");        //相对路径
13         File f5 = new File("C:\test");        //目录
14         File f4 = new File(f5,"a.txt");
15         File f6 = new File("C:"+File.separator+"a.txt");
16         /**File操作文件的相应方法*/
17         //System.out.println(f1.createNewFile());
18         System.out.println(f1.delete());       //直接从磁盘上删除
19         System.out.println(f1.exists());    //判断是否存在,不存在false,存在true
20     }
21 
22 }

   File类操作目录

 1 package com.zqf.file;
 2 
 3 import java.io.File;
 4 
 5 public class TestDirectory {
 6     public static void main(String[] args) {
 7         //创建File类的对象
 8         File f = new File("C:"+File.separator+"test");
 9         System.out.println("目录是否存在"+f.exists());
10         System.out.println("是目录还是文件:"+f.isDirectory());
11         System.out.println("是目录还是文件:"+f.isFile());
12         
13         File f2 = new File("C:\aa\bb\cc");  //层目录
14         f2.mkdirs();  //用于创建目录,多层目录
15         f.delete();
16         File parent=f2.getParentFile();        //获取cc目录的父级目录
17         System.out.println(parent);
18         parent.delete();        //delete删除目录时,只允许删除空目录
19         f2.delete();  //删除cc
20         parent.delete();    //删除bb
21         
22         File f3 = new File("C:\");
23         String [] strFile= f3.list();
24         System.out.println(strFile.length);  //数组中元素的个数
25         for(String s:strFile){
26             System.out.println(s);
27         }
28     }
29 }

递归算法遍历目录

  编写一个程序,以树状结构展现特定的文件夹及其子文件(夹)(使用递归来做)

 1 package com.zqf.file;
 2 
 3 import java.io.File;
 4 
 5 public class TestFile2 {
 6     public static void main(String[] args) {
 7         File f= new File("C:\a.txt"+ "");
 8         printFile(f,0);
 9     }
10     public static void printFile(File file,int level){
11         //打印树状结构的层次关系
12         for(int i=0;i<level;i++){
13             System.out.print("-");
14         }
15         //输出目录或文件的名称
16         System.out.println(file.getName());
17         if(file.isDirectory()){        //判断File对象是否是目录
18             File [] listFiles=file.listFiles();
19             for (File temp:listFiles){
20                 //自己调用自己
21                 printFile(temp,level+1);
22             }
23         }
24     }
25 }

后面有Calender 日历类,File类,java.util.Date类,递归算法遍历目录,枚举详细介绍等,因不小心删除而丢失

原文地址:https://www.cnblogs.com/zqfdgzrc/p/10452161.html