Java基础 - 常用类(包装类 String Date File 枚举)

第六章 常用类【这章中代码没有导入的类使用CTRL+Shift+O】导入

  1. 包装类的使用
    public class TestWrappedClass {//public final class Integer extends Number implements Comparable
    	
    	public static void main(String[] args) {
    		//基本数据类型转成包装类对象	
    		Integer a = new Integer(3);
    		Integer b = Integer.valueOf(50);//使用静态化方法推荐使用
    		
    		//将包装类对象转成基本数据类型
    		int c = b;//直接转换可以 有自动的拆箱
    		int d = a.intValue();//手动转型
    		double e = a.doubleValue();
    		
    		//把字符串转换成包装类对象
    		Integer f = new Integer("1111");//只能转纯数字类型的字符串否则会报错
    		Integer g = Integer.parseInt("88888");
    		
    		//将包装类转成字符串对象
    		String str = f.toString();
    		
    		//常见的常量
    		System.out.println("int类型中最大的整数是:"+Integer.MAX_VALUE);
    	}
    }
    
  2. 自动装箱与拆箱
    • 装箱和拆箱使用的方法:
      • 装箱: Ingeger.ValueOf()方法
      • 拆箱:intValue();方法
    public class TestAutoBox {
    	public static void main(String[] args) {
    		//自动装箱 编译器会自动的调用
    		Integer a = 123;//实际上是将Integer a = Ingeger.ValueOf(123);
    		
    		//自动拆箱 编译器实际上是调用a.intValue();
    		int b = a;
    		
    		Integer in3  = 1234;//不在其中就是新建对象
    		Integer in4  = 1234;
    		System.out.println(in3 ==in4);//false  因为是两个不同的对象
    		System.out.println(in3.equals(in4));//true  值相同
    		
    		Integer in1 = -123;
    		Integer in2 = -123;
    		System.out.println(in1 ==in2);
    		//true???为什么  因为-123在缓存范围内 [-128,128)是个数组
    		//中就会从中取出已经创建好的对象而不是新建对象
    		System.out.println(in1.equals(in2));//true			
    	}
    }	
    
  3. String类的使用
    public class TestString {
    	public static void main(String[] args) {
    		String str = "aafffafafff";//final修饰的是不可变类型
    		String str2 = str.substring(2,5);
    		System.out.println(str);//即使使用substring方法截取字符串也不可以改变
    		System.out.println(str2);
    		String str3 = "hello";
    		String str4 = "java";
    		String str6 = "hello" + "java";
    		String str5 = str3 +str4;
    		String str1 = "hellojava";
    		System.out.println(str1 ==str5);//false 
    		System.out.println(str6 == str1);//true 是因为编译器会自动的进行优化相当于str1	
    	}
    }
    
  • 注意:
    • 使用字符串进行比较时最好不要使用==进行比较使用equals方法进行比较
    • 字符串的循环累加:【绝对注意!】
      不要使用String的累加 而是使用StringBuilder的append方法 不会产生大量的对象降低程序运行的效率
      //测试StringBuilder StringBuffer
      public class TestStringBuilder01 {
      	public static void main(String[] args) {
      		String str = "sadfdfasdf";
      		//StringBuilder线程不安全但是效率高(常用),StringBuffer线程安全但是效率低【线程安全的问题会在多线程的哪章讲到 现在了解即可】
      		StringBuilder sb = new StringBuilder("adfdfdasdf");//没有使用final修饰是可变的类型
      		System.out.println(sb);
      		System.out.println(str);		
      	}
      }
      
      //测试StringBuilder的常用方法 线程安全效率高 
      public class TestStringBuilder02 {
      	public static void main(String[] args) {
      		StringBuilder sb = new StringBuilder();
      		for(int i = 0;i<26;i++) {
      			sb.append((char)('a'+i));
      		}
      		System.out.println(sb);
      		sb.reverse();//倒序
      		System.out.println(sb);
      		sb.setCharAt(3, 's') ;//将3 位置的字符替换成s
      		System.out.println(sb);
      		sb.insert(0, 'w');//在0 的位置上插入字符w  可以链式调用 因为将自己返回了
      		System.out.println(sb);
      		sb.delete(20, 23);//删除区间的字符
      		String str = "";
      		/*for(int i=0;i<5000;i++) {这种代码是不能出现的 
      		 * 这样会产生大量的对象会使程序崩溃
      			str = str +i;
      		}*/
      		//字符串的循环累加
      		StringBuilder sb1 = new StringBuilder("");
      		for(int i = 0;i<5000;i++) {
      			sb1.append(i);//这样的方式不会产生大量的对象(也会产生对象)
      		}
      	}
      }
      
  1. 时间处理的相关类
    1. Date类 表示时间1970年的0月0号0点是计算机的基准时间单位是毫秒 使用long类型的变量来表示时间 现在不常用 许多的方法已经过时
      //测试Data类的常用方法
      public class TestDateClass {
      	public static void main(String[] args) {
      		Date a = new Date();
      		Date b = new Date(200000000);
      		System.out.println(a);
      		System.out.println(a.getTime());//获取毫秒数	
      		System.out.println(a.after(b));//比较时间
      		System.out.println(a.before(b));	
      		//以后遇到处理时间的问题使用calendar类
      		Date c = new Date(120,1,1);//已经被代替最好不要使用
      		System.out.println(c);
      	}
      }
      
    2. DateFormat 日期格式化的类(抽象类)
      //测试DateFormat类将字符串和时间对象进行转化
      //DateFormat抽象类和SimpleDateFormat实现类的使用
      public class TestDateFormat {
      	public static void main(String[] args) throws ParseException {
      		//将时间对象转成字符串按照相应的格式
      		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//抽象类不可以new一般使用SimpleDateFormat类
      		System.out.println(df.format(new Date(400000)));
      		
      		//将字符串按照相应的格式转成字符串
      		DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      		Date date = df2.parse("1999-08-07 10:45:55");
      		System.out.println(date);
      		
      		//测试其他的格式输出 D是所属年的第几天 W第几周
      		DateFormat df3 = new SimpleDateFormat("D");
      		System.out.println(df3.format(new Date()));
      	}
      }
      
      
    3. Calendar类的使用
      // 测试Calendar 日历类/日期类 提供了日期的相关的展示与计算
      public class TestCalendar {
      	public static void main(String[] args) {
      		Calendar cl = new GregorianCalendar(2999,10,10,11,11,11);//与DateFormat类相同需要new他的实现类
      		System.out.println(cl);
      		//获取日期的相关元素 通过get方法
      		int year = cl.get(Calendar.YEAR);
      		int month = cl.get(Calendar.MONTH);
      		int date = cl.get(Calendar.DATE);//也可以使用DAY_OF_MONTH
      		int week = cl.get(Calendar.DAY_OF_WEEK);
      		System.out.print(year);
      		System.out.println(week);//1是周日  7 是周六
      		System.out.println(month);//其实0是一月 十一是十二月
      		System.out.println(date);//获取日期
      		
      		//设置日期的相关元素  通过set方法
      		Calendar cl2 = new GregorianCalendar();
      		cl2.set(Calendar.YEAR, 8102);
      		System.out.println(cl2.get(Calendar.YEAR));
      		
      		//日期的计算
      		Calendar cl3 = new GregorianCalendar();
      		cl3.add(Calendar.YEAR, -1000);
      		System.out.println(cl3.get(Calendar.YEAR));
      		
      		//将日期对象与时间对象的转换
      		Date d4 = cl3.getTime();//将日期对象转换成时间对象使用getTime方法
      		Calendar cl4 = new GregorianCalendar();
      		cl4.setTime(new Date());//将时间对象转成日期对象使用setTime方法
      		
      		//输出年月日周
      		printCalendar(new GregorianCalendar());
      	}
      	public static void printCalendar(Calendar c ) {
      		int year =c.get(Calendar.YEAR);
      		int month = c.get(Calendar.MONTH) +1;
      		int day = c.get(Calendar.DATE);
      		int dayweek = c.get(Calendar.DAY_OF_WEEK)-1;
      		String dayweek2 = dayweek ==0?"周日":"周"+dayweek;
      		System.out.println(year+"年"+month+"月"+day+"日"+dayweek2);
      	}
      }
      
  2. File的类的使用
    public class TestFileClass {
    	public static void main(String[] args) throws IOException {
    		File f = new File("E:/a.txt");
    		f.renameTo(new File("E/dsdf.txt"));//改名的操作
    		System.out.println(System.getProperty("user.dir"));//该项目的路径
    		File f2 = new File("a.txt");
    		//f2.createNewFile();//file的创建
    		System.out.println(f2.exists());//file 是否存在
    		System.out.println(f2.getPath());//获取文件的路径
    		System.out.println(f2.getAbsolutePath());//获得绝对路径
    		System.out.println(f2.isDirectory());//是否是目录
    		System.out.println(f2.isFile());//是否是文件
    		System.out.println(f2.length());//文件的大小
    		System.out.println(f2.getName());//获取文件的名字
    		System.out.println(new Date(f2.lastModified()));//获取文件最后的修改时间
    		
    		//使用mkdir创建目录
    		File f3 = new File("d:/1/2/3");
    		boolean flag = f3.mkdir();//目录结构中有一个不存在就不会创建整个目录树
    		System.out.println(flag);//创建失败
    		
    		//使用mkdirs创建目录
    		File f4 = new File("d:/1/2/3");
    		boolean flag2 = f4.mkdirs();//目录结构中有一个不存在但还会创建整个目录树
    		System.out.println(flag2);
    		System.out.println(f);
    	}
    }
    
  • File类中常用的方法:

    • 文件名称相关的方法:
    方法解释
    String getName()获取File对象表示的文件名称或路径(如果是路径(目录)返回最后一级的路径名)
    String getPath()获取File抽象对象所对应的路径名称
    String getParent()获取File抽象对象对应的父目录
    String getAbsoluteFile()获取File抽象对象的绝对路径
    String getAbsolutePath()获取File抽象对象的绝对路径名称
    String getParent()获取File抽象对象的父目录(上一级目录)
    boolean renameTo(File newName)将File设置一个新的名称,true成功
    • 文件检测相关的方法:
    方法解释
    boolean exists()判断File对象所对应的文件或目录是否存在
    boolean canWrite()判断File对象对应的文件或目录是否可以写
    boolean canRead()……是否可以读
    boolean isFile()是否是文件
    boolean isDirectory()是否是目录
    isAbsolute()是否是绝地路径
    long lastModified()返回文件的最后修改时间
    long length()返回文件内容的长度
    • 文件操作相关的方法
    方法解释
    boolean createNewFile()当file对象对应的文件不存在时,创建一个文件
    boolean delete()删除File对象所对应的文件或路径(目录
    static File crateTempFile(String prefix,String stuffx,File directory)directory在指定的目录中创建一个临时的空文件,使用给定前缀+系统随机生成的名称,后缀名称
    void deleteOnEcit()删除临时文件,只有当JVM正常退出才会删除临时文件
    • 目录操作相关的方法:
    方法解释
    boolean mkdir()创建一个File对象所对应的目录
    String[] list()列出File对象的所有子文件和路径名称(目录) 这就是遍历的两种方式之一
    File[] listFile()列出File对象的所有子文件和目录 这是另一种遍历的方式
    static File[] listRoots()列出系统所有的根路径
  • 使用递归遍历整个目录树

    //测试使用递归遍历整个目录结构
    public class TestFile2 {
    	public static void main(String[] args) {
    		File f = new File("D:\\QMDownload");
    		printFile(f);
    	}
    	static void printFile(File file) {
    		System.out.println(file.getName());
    		if(file.isDirectory()) {
    			File[] files = file.listFiles();
    			for(File temp: files){
    				printFile(temp);
    			}
    		}
    	}
    }
    
  1. Math类

    public class TestMath {
    	public static void main(String[] args) {
    		//取整的相关操作
    		System.out.println(Math.ceil(3.2));//取大于3.2的最小的整数Double类型
    		System.out.println(Math.floor(3.2));//取小于3.2的最大的整数Double类型
    		System.out.println(Math.round(3.2));//四舍五入
    		System.out.println(Math.round(3.8));//四舍五入
    		//绝对值 开方 幂的操作
    		System.out.println(Math.abs(-45));//取绝对值
    		System.out.println(Math.sqrt(64));//开方
    		System.out.println(Math.pow(2, 5));//幂运算
    		System.out.println(Math.pow(5, 2));
    		//Math中的常量
    		System.out.println(Math.PI);//double 类型
    		System.out.println(Math.E);//double
    		//随机数
    		System.out.println(Math.random());//[0,1)
    	}
    }
    
    
  2. 枚举当定义一组常量时

    enum 枚举名在{
    常量1常量2,常量3,常量4
    }//枚举体
    
    1. 建议 :
      • 在创建一组常量时 可以使用枚举
      • 最好是不要使用枚举的高级特性 枚举的高级特性可以由普通类来实现的 就没必要使用枚举(枚举能创建一个线程安全的单例模式)
      public class TestEnum {
      	public static void main(String[] args) {
      		Season a = Season.SPRING;
      		switch(a) {// switch中支持的类型 byte short int char enum 1.5 String 1.7
      		case SPRING:
      			System.out.println("春天来了");
      			break;
      		case SUMMER:
      			System.out.println("夏天来了");
      			break;
      		case AUTUMN:
      			System.out.println("秋天来了");
      			break;		
      		case WINTER:
      			System.out.println("冬天来了");
      		}
      	}
      }
      enum Season{
      	SPRING,SUMMER,AUTUMN,WINTER
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上山打卤面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值