第11天 常用类

关于常用类,建议大家编写一个常用类文档,没必要全部背下来,只要用的时候可以快速查找就可以了

1.Scanner input=new Scanner(System.in); 输入工具类
  a. 确定包名字   ,java.lang 程序自动导入
  b. 确定方法如何调用。
     类名.方法名();
     创建对象,对象名.方法名();
  c.确定构造方法,
  D.掌握方法:参数有几个,每个参数代表什么;
             返回值是什么类型,返回值的范围,代表的含义。

2.包装类:
  Java为每一个基本数据类型 都提供一个引用类型,这些引用类型 就叫包装类。
  int age=0   ---Integer i=new Integer(0);

 学习包装类的原因:
   1.默认值不同,基本数据类型的默认值没有办法证明不是真实值;
   2.集合---》替换 数组,集合只能存储引用数据变量。不能存储基本数据类型。
   3.包装类更符合面向对象思想。

 学习:
    所在包:java.lang
    父类:java.lang.Number
    接口:Serializable , Comparable < Integer >
    属性:Integer.MAX_VALUE  ;Integer.MIN_VALUE
    构造方法:  Integer(int value) ; Integer(String s)
    方法: compare(int x, int y)
          compareTo(Integer anotherInteger) //比较+排序 0  1 -1
          equals(Object obj)
          parseInt(String s) 最常用: 把一个字符串转成int 变量

    自动拆装箱:
      intValue() ;拆箱
      valueOf(String s) ;装箱

      Integer i2 = Integer.valueOf(9);
       System.out.println(i2.intValue() * 3);


3.BigInteger 类:

        BigInteger bi=new BigInteger("12345678901234567890");
        BigInteger i2=new BigInteger("2");
        System.out.println("和是:"+bi.add(i2));
        System.out.println("差是:"+bi.subtract(i2));
        System.out.println("积是:"+bi.multiply(i2));
        System.out.println("商是:"+bi.divide(i2));


4.BigDecimal 类:

        BigDecimal bd=new BigDecimal("123.1234567890123456789012345690");
        BigDecimal bd2=new BigDecimal("1.1");
        System.out.println("和是:"+bd.add(bd2));
        System.out.println("差是:"+bd.subtract(bd2));
        System.out.println("积是:"+bd.multiply(bd2));
        System.out.println("商是:"+bd.divide(bd2,1,ROUND_CEILING)); //参数1-bd2:除数; 参数2-1:保留结果的小数点位数;参数3-提供的除法运算法则
        double dou=1;
        double dou1=2;
        System.out.println(dou/dou1);


5.Math数学类:

     Math. random() ; 得到[0,1)之间的小数。
     获得[20-50]之间
     int number=(int)(Math.random()*31)+20;

6.Random类:专门用来生产 随机数的。

        Random random=new Random(1);  //参数 叫做 随机种子:种子相同,产生的随机数一定相同。
        System.out.println(random.nextDouble());
        Random random1=new Random(2);
        System.out.println(random1.nextDouble());

7.时间类型:=== String
    第一个阶段的时间类型:
         A.java.util.Date;

               /*  Date dt=new Date(); //默认获得当前系统时间对象
                 System.out.println(dt);//默认调用toString();
                 System.out.println(dt.toString());
                 System.out.println("年:"+(dt.getYear()+1900));
                 System.out.println("月:"+dt.getMonth()+1);//西方月份从0开始计算
                 System.out.println("日:"+dt.getDate());
                 System.out.println("小时:"+dt.getHours());
                 System.out.println("分钟:"+dt.getMinutes());
                 System.out.println("秒:"+dt.getSeconds());
                 System.out.println("星期:"+dt.getDay());

                 Date dt1=new Date(125,7,7); //根据参数,获得一个时间对象!
                 System.out.println("星期几:"+dt1.getDay());

         B.java.sql.Date

          java.sql.Date sdt=new  java.sql.Date(125,7,1);
             System.out.println(sdt);
          Date dt1=new Date(125,7,7,14,1,23);
             System.out.println(dt1);

         * java.sql.Date   VS  java.util.Date 的区别:
         * 1.前者没有无参构造,后者 无参 ,有参都有
         * 2.前者是子类,    后者是 父类
         * 3.前者不能时分秒,后者可以获得

         C.SimpleDateFormat 时间格式工具类:

        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(simpleDateFormat.format(dt1));
        System.out.println( simpleDateFormat.parse("2025年11月12日 14:14:14").getDay());

     整理:第一阶段中的时间类型的方法jdk已经弃用,不在维护了。所以需要学习第二阶段的时间。


    第二个阶段的时间类型:  没有格式处理的工具类;存储的时间信息多,jdk维护。
      Calendar 日历类:
       创建:
               Calendar calendar=Calendar.getInstance(); //当前系统时间
               Calendar calendar1=new GregorianCalendar();
       使用:
            System.out.println(calendar.get(Calendar.YEAR)); //获得指定的时间部分信息
            calendar.add(Calendar.MONTH,7); // 添加/减少 指定的时间部分
            calendar.set(2025,11,1); //重新设置时间
            转换方法:String---Calendar
              SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
              calendar.setTime(sdf.parse("2025-11-11"));


    第三个阶段的时间类型:
        LocalDate ld=LocalDate.now();//默认获得系统时间
        System.out.println(ld);

        LocalDate of = LocalDate.of(2025, 12, 12);
        System.out.println(of.getDayOfWeek());

        DateTimeFormatter dtf=DateTimeFormatter.ISO_LOCAL_DATE;
        System.out.println(dtf.format(of));


8.字符串:底层就是char[]

   一个内存地址 对应一个信息值;      一个内存地址对应多个信息值

    String [不可变]        [可变] StringBuffer 线程安全的,效率低       StringBuilder 不安全的,效率高

    介绍String类:
//构造方法:
        String str=new String();
        String str1=new String("123qwertyuiop123qwe");
        String str2=new String(new char[]{'1','2','3'});

        //方法:
        System.out.println(str1.substring(5)==str1);//截取:从下标5开始截取 到最后
        System.out.println(str1.substring(5,10));//截取:从下标5开始截取 到10-1
        System.out.println(str1.concat("!!!"));//拼接
        System.out.println(Arrays.toString(str1.toCharArray()));
        System.out.println(str1.equals(new String("123qwertyuiop123qwe")));
        System.out.println(str1.lastIndexOf("q") );//查找,参数所在的位置
        System.out.println(str1.indexOf("q"));
        System.out.println(str1.isEmpty()); //判断是否为null 空
        str1.length();//获得长度

        String strtime="2025-12-12";//从控制台接收yyyy-MM-dd字符串,为java.sql.Date赋值;
        String[] times = strtime.split("-");
        java.sql.Date dt=new java.sql.Date(Integer.parseInt(times[0])-1900,
                Integer.parseInt(times[1]),
                Integer.parseInt(times[2]));
        System.out.println(dt);

        //"123qwertyuiop123qwe"
        System.out.println(  str1.replace("12","33"));

     /* str1.charAt();
        str1.contains();
        str1.trim();
        str1.endsWith();
        str1.startsWith();
        str1.toLowerCase();
        str1.toUpperCase();*/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值