JavaAPI2(实用类)

第二章:实用类
1、枚举类型Enum
   含义:枚举是由一组固定的(字符串)常量组成的类型
   优点:类型安全,易于输入,代码清晰
   public enum Genders{
     Male,Female
   }
   public class Student{
     public Genders sex;
   }
2、java.lang包
  (1)范围
     最广泛使用的包,包含了java程序的基础类和接口,例:包装类、Math类、String类等;
     同时还提供了用于管理类的动态加载、外部进程创建、主机环境查询和安全策略实施等“操作系统”的类。
  (2)包装类
     1>包括:Byte、Boolean、Short、Character、Integer、Long、Float、Double
     2>基本类型转换为包装类
      方一:Character char1=new Character(‘我’);     //我
         Integer int1=new Integer(‘我’);     //25105
         Boolean b1=new Boolean(“tRUE”); //true(true不分大小写,其余全为false)
     方二(char除外):Integer int2=Integer.valueOf(“12”);     //12
               Double d1=Double.valueOf(“12”);    //12.0
     3>包装类转换为基本类型xxxValue()
       int int3=int1.intValue();     //25105
       char char2=char1.charValue();     //我
     4>包装类和基本类型的自动转换
       Integer int4=6;     //6,又称自动装箱
       int int5=int4;     //6,又称自动拆箱
  (3)Math类
     1>Math类提供了一些基本的数学运算和几何运算的方法,且此类中的所有方法都是静态的;此类是final类,因此没有子类
     2>生成0-10随机数的两种方法
      方一:int ran= (int) (Math.random()*10);
         System.out.println(ran);
      方二:Random random=new Random();
         int r=random.nextInt(10);
         System.out.println(r);
     3>常用方法
      绝对值:Math.abs(-3)         //3
      向下取整:Math.floor(3.9)       //3.0
      向上取整:Math.ceil(3.1)        //4.0
      四舍五入取整:Math.round(3.1)     //3
      比较两个数的最大值:Math.max(3,2.5)   //3.0
      比较两个数的最小值:Math.min(3,2)     //2
  (4)String类
     1>常用方法
      获取字符串长度:length()
      比较两个字符串在内存中的地址:==
      比较两个字符串的内容:equals()
      比较内容忽略大小写:equalsIgnoreCase()
      字符串全部转换成大写:toUpperCase()
      字符串全部转换成小写:toLowerCase()
      字符串连接:String str=str1.concat(str2)<==>String str=str1+str2
      字符第一次出现的下标,没有返回-1:indexOf()
     字符最后一次出现的下标,没有返回-1:lastindexOf()
      截取从begin开始的字符串:substring(begin)
      截取从begin开始到end结束(不包括end)的字符串:substring(begin,end)
      拆分:split()
         String str3=“我爱中国,我爱我的祖国”;
         String []str4=str3.split(“我”);
         for(String st:str4){
           System.out.println(st);       // 爱中国, 爱 的祖国
         }
      返回指定索引处的值:char char=str3.charAt(4)      //,
     去前导和尾随的空格:trim()
     2>包装类转成字符串toString()
      Short s=6;String sh=Short.toString(s);
      String lon=Long.toString(5);
      String dou=Double.toString(5.5);
      String sb=new StringBuffer(“aaa”).toString();
     3>基本类型转成字符串String.ValueOf(基本类型)
      System.out.println(String.valueOf(‘6’));          //6
      String sex=‘男’+"";
     4>字符串转基本类型(Char除外)Integer.parseInt(字符串)
      int a=Integer.parseInt(“55”);
      double ch=Double.parseDouble(“2.3”);
     5>String、Stringbuffer、StringBuilder区别
      String:字符串常量
           不可变对象,所以经常改变字符串内容最好不要用String,因为每次生成对象都会对系统性能产生影响
       StringBuffer:字符串变量
             可变字符串,每次改变都是对对象本身操作,而不是生成新的对象;所以经常改变字符串内容推荐使用StringBuffer,线程安全
       StringBuilder:字符串变量
             和StringBuffer等价,它是单线程,不同步,线程不安全,效率更高
 (5)Thread类:线程
3、java.util包
  (1)范围
     包含系统辅助类,特别是Collection、List和Map等集合类
 (2)产生随机数:Random
  (3)处理字符串:StringBuffer
    常用方法:
        StringBuffer sb=new StringBuffer(“我爱中国”);
     1>追加:字符串.append(参数)
         System.out.println(sb.append(“wowowo”));     //我爱中国wowowo
         System.out.println(sb);          //我爱中国wowowo
     2>转换:字符串.toString()
         System.out.println(sb.toString());     //我爱中国wowowo
     3>插入:字符串.insert(下标,参数)
         System.out.println(sb.insert(2,“我的”));     //我爱我的中国wowowo
  (4)日期操作类:Date、Calendar、SimpleDateFormat等
     1>获取当前的时间
      Date date=new Date();
      System.out.println(date);      //Wed Dec 26 18:27:00 CST 2018
     2>格式化:日期格式->文本格式
      SimpleDateFormat formater=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
      String str=formater.format(date);
      System.out.println(str);      //2018-12-26 18:27:00
     3>解析:文本格式->日期格式
      Date dates=formater.parse(str);
      String str=formater.format(dates);     //Wed Dec 26 18:27:00 CST 2018
     4>Date类的增强版Calendar
      Calendar c=Calendar.getInstance();     //使用默认时区和区域设置获取日历。
      System.out.println(“今天是”+c.get(Calendar.YEAR)+“年”+(c.get(Calendar.MONTH)+1)+“月”+c.get(Calendar.DAY_OF_MONTH)+“日,星期”+(c.get(Calendar.DAY_OF_WEEK)-1));
      Date date=c.getTime();       //返回一个 Date表示此物体 Calendar的时间值
     System.out.println(date);        //Wed Dec 26 18:27:00 CST 2018
4、java.io包:包含了与输入输出有关的类,对文件提供基本的操作,包括对文件和目录属性的操作,对文件读写的操作等。
5、java.net包:包含了与网络有关的类,如Socket、ServerSocket等类
6、java.sql包:包含了与数据库相关的类,如Connection、Statement等类

猜你喜欢

转载自blog.csdn.net/weixin_43779785/article/details/85643137