Day15常用API

1.包装类

1.1是什么

           包装类提供了一种将基本类型用作对象的方法。

          Java中的数据类型分为基本数据类型和引用数据类型,其中基本数据类型是不具有对象特征的,也就是说它们不能像对象一样拥有属性和方法,以及对象化交互。
        包装类的产生就是为了解决基本数据类型存在的这样一些问题,通过包装类可以让基本数据类型获取对象一样的特征,行使对象相关的权力。

1.2基本使用

作为和基本数据类型对应的类类型存在,方便涉及到对象的操作
提供每种基本数据类型的相关属性如最大值、最小值等以及相关的操作方法

将基本类型转化为引用类型:

语法结构:Byte b1 = new Byte(b);
                  Integer i1 = new Integer(11);
                  Boolean b2 = new Boolean(false)

2.Integer

2.1Integer的基本使用

package day_02._01_Integer;

/**
 * 以Integer为例讲解八种包装类
 */
public class Integer_02 {
    public static void main(String[] args) {
        // 获取最大值和最小值
        System.out.println("int最大值 : " + Integer.MAX_VALUE);
        System.out.println("int最小值 : " + Integer.MIN_VALUE);

        System.out.println(Byte.MAX_VALUE);
        System.out.println(Long.MAX_VALUE);

        // 创建Integer对象
        // int类型转换为Integer类型
        Integer i1 = new Integer(10);

        // 可以直接把纯数字的字符串转换为Integer类型
        Integer i2 = new Integer("10");

        System.out.println(i1);
        System.out.println(i2);
        // false
        System.out.println(i1 == i2);
        // 覆写了equals true
        System.out.println(i1.equals(i2));

    }
}

2.2Integer的常用方法

1    对象 int --> Integer:Integer i1 = new Integer(10);
 2   Integer --> int:int i2 = i1.intValue();

3   static int parseInt(String s); : 把纯数字字符串转换为int类型
 String --> int   必须是纯数字字符串,小数点也不行: int i3 = Integer.parseInt("123");

 小数允许有一个小数点: double d = Double.parseDouble("3.2");

将int类型的值,转换为二进制的字符串表示形式static String toBinaryString(int value);
语法结构:String s1 = Integer.toBinaryString(12);

转换为十六进制 System.out.println(Integer.toHexString(10));

 转换为八进制 System.out.println(Integer.toOctalString(10));

4   int --> Integer: Integer i4 = Integer.valueOf(10);

5    String --> Integer    Integer i5 = Integer.valueOf("10");

2.3  类型转换

 1 int --> Integer        Integer i1 = Integer.valueOf(11);

2 Integer --> int           int i2 = i1.intValue();

3 String --> Integer         Integer i3 = Integer.valueOf("22");

4 Integer --> String          String s1 = i3.toString();

5 String --> int         int i4 = Integer.parseInt("123");
 6 int --> String        String s2 = 2 + "";

2.4自动装箱,自动拆箱

装箱:把基本数据类型转换成包装类,分为自动装箱和手动装箱
拆箱:把包装类转换成基本数据类型,分为自动拆箱和手动拆箱
 并且 自动装箱和自动拆箱是在编译阶段完成的

 //装箱:把基本数据类型转换成包装类
    //1、自动装箱
    int t1=2;
    Integer t2=t1;
    //2、手动装箱:使用构造方法
    Integer t3=new Integer(t1);
    //拆箱:把包装类转换成基本数据类型
    //1、自动拆箱
    int t4=t2;
    //2、手动拆箱:使用intValue方法
    int t5=t2.intValue();
    //拆箱为其他基本数据类型,使用对应的xxxValue方法
    double t6 = t2.doubleValue(); //2.0

装箱和拆箱
        Integer i1 = Integer.valueOf(11);
        int i2 = i1.intValue();

 自动装箱和拆箱
        Integer i3 = 2;
        int i4 = i3;

基本数据类型转换为字符串
— 使用包装类的toString()方法
字符串转换为基本数据类型
— 自动拆箱调用包装类的parseXxx()静态方法
— 调用包装类的valueOf()方法转换为基本类型的包装类,然后自动拆箱

通过new关键字创建对象其引用必然不相同,而如果使用valueOf方法则需要考虑是否会从对象常量池中取对象。
除了FloatDoubleBoolean外其他包装类都可以应用对象常量池概念。

2.5整型常量池 深入理解自动装箱和自动拆箱

整型常量池的范围 : -128~127之间
1 都是编译时进行的操作
 2 自动装箱 的时候,会把赋值 操作 改变为 Integer.valueOf(值) 
 Integer.valueOf() 底层实现 :
 public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
   IntegerCache 是Integer中的静态内部类,也就是我们说的整型常量池
           static final int low = -128;
                static final int high = 127;
                static final Integer cache[];
            在static语句块中,对cache数组 进行初始化操作
                      cache = new Integer[(high - low) + 1];  长度为256
                      初始化数组中的数据
                     cache[k] = new Integer(j++);
                 数组中数据为 -128,-127,-126.....127  共 256个数字,下标为0~255   
    此时 整型常量池就初始化完成了,在堆内存中创建了256个对象       
   valueOf方法中这么写的
   判断 要赋值的数据值 是否在-128到127之间
            if (i >= IntegerCache.low && i <= IntegerCache.high)
     如果在这个范围内的话,之间在case数组中把对应的对象的地址拿出来,返回回去
                return IntegerCache.cache[i + (-IntegerCache.low)];
如果不再这个范围内的话,就new一个新的对象,保存这个数据
            return new Integer(i);            
        所以 我们写 Integer i1 = 123;  Integer i2 = 123; 使用 == 是相等的,因为他们指向堆内存的地址都是同一个
        反之 我们写 Integer i3 = 128; 就等于 Integer i3 = new Integer(128) , 如果使用== 肯定是不等的,需要使用equals才可以

3  System

3.1是什么

System代表系统,系统很多的属性和控制方法都在这个类中,位与java.lang包下
 long currentTimeMillis() : 获取当前系统时间的毫秒数 , 从1970-1-1 0:0:0 000开始 到现在的时间毫秒数
 我们这个地区 时间为 1970.1.1 8:00:00 000
 void exit(int status) : 退出虚拟机, 参数为0 表示正常退出 非0 表示异常退出,常用于图形界面,实现退出功能
4Date

4.1 是什么

表示特定的瞬间,精确到毫秒

4.2构造方法

Date():使用无参构造器创建的对象可以获取本地当前时间。

Date(long date)

 获取当前系统时间          Date d1 = new Date();

获取时间原点到指定毫秒数的时间         d1 = new Date(1000);

表示方法:年 y , 月 M , 日 d , 小时 H , 分 m , 秒 s , 毫秒 S

4.3时间格式化

Date类的API不易于国际化,大部分被废弃了,java.text.SimpleDateFormat类是一个不与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化:日期à文本、解析:文本à日期

创建格式化对象,并指定格式
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SSS");

 对时间进行格式化,返回字符串类型String strDate = sdf.format(d1);

解析 , 字符串格式 必须和解析格式一致 Date d2 = sdf.parse(strDate);

5 Calendar

Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能

日历类

5.1方法

获取Calendar实例的方法

使用Calendar.getInstance()方法

调用它的子类GregorianCalendar的构造器。

3、一个Calendar的实例是系统时间的抽象表示,通过get(int field)方法来取得想 要的时间信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、 MINUTE、SECOND

public void set(int field,int value)

public void add(int field,int amount)

public final Date getTime()

public final void setTime(Date date)

4、注意:

获取月份时:一月是0,二月是1,以此类推,12月是11

获取星期时:周日是1,周一是2 , 。。。。周六是7

获取日历对象         Calendar c = Calendar.getInstance();

获取当前是本周第几天, 第一天为周日,最后一天为周六  int i = c.get(Calendar.DAY_OF_WEEK);

 获取年 int year = c.get(Calendar.YEAR);

 获取月, 0开始,所以结果要加1    int month = c.get(Calendar.MONTH) + 1;

 获取日  int day = c.get(Calendar.DAY_OF_MONTH);

24小时制 int hour = c.get(Calendar.HOUR_OF_DAY);

分          int minute = c.get(Calendar.MINUTE);

秒         int second = c.get(Calendar.SECOND)

获取星期         int weekday = c.get(Calendar.DAY_OF_WEEK);

6Math

Mth 提供科学计算和基本的数字操作,常用方法都是静态的,使用类名直接调用即可

在java.lang下面,使用不需要导包

6.1使用

abs 绝对值        System.out.println(Math.abs(-1.2));
ceil : 向上取整System.out.println(Math.ceil(1.0001));
floor : 向下取整 System.out.println(Math.floor(2.999999));
 max : 比较谁大System.out.println(Math.max(2.3, 2.2));
min : 比较谁小 System.out.println(Math.min(2.3, 2.2));
平方根  开平方System.out.println(Math.sqrt(16));
 立方根  开立方System.out.println(Math.cbrt(8));

随机数 : 获取一个大于等于0 且 小于1 的数System.out.println(Math.random());
 向下取整( 随机数*(最大-最小 +1) + 最小)System.out.println(Math.random()*10 + 10);
四舍五入 : 四舍六入五留双, 小数大于0.5 就进位,小于0.5就舍弃,如果是0.5整 , 取偶数2.50001 : 3 System.out.println(Math.rint(2.5001));
 2的3次方  System.out.println(Math.pow(2, 3));

 7BigInteger

1、Integer类作为int的包装类,能存储的最大整型值为231-1,Long类也是有限的, 最大为263-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类 都无能为力,更不用说进行运算了。

2、java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供

所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位操作以及一些其他操作。
7.2常用方法

public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger。

BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger

BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger

BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger

BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数 相除只保留整数部分。

BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。

BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟(this % val) 的两个 BigInteger 的数组。

BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

7.3语法

不可变的任意精度的整数(而Integer有精度限制)
对象创建:BigInteger bi = new BigInteger("9999999999");

BigDecimal

不可变的、任意精度的有符号十进制数,适用于对小数进行精确的计算
对象创建:BigDecimal bd = new BigDecimal("4099.99");

参数是字符串
        BigInteger v0 = new BigInteger("11");

参数是数值
        BigDecimal v1 = new BigDecimal(20);

常用方法:(没有说明返回类型都是BigInteger)

add(BigInteger val) 加法运算
subtract(BigInteger val) 减法运算
multiply(BigInteger val) 乘法运算
divide(BigInteger val) 除法运算
remainder(BigInteger val) 取余数
divideAndRemainder(BigInteger val) 返回类型 BigInteger[]

8.Random
        // 创建随机数生成器
        Random r = new Random();

        // 大于等于0 且小于10的整数
        int result = r.nextInt(10);
        System.out.println(result);

        // 生成 10~20
        // nextInt(最大值 - 最小值 +1) + 最小值
        result = r.nextInt(11) + 10;
        System.out.println(result);

        // 生成a~z
        result = r.nextInt(26);
        char c = (char) (result + 97);
        System.out.println(c);
   
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值