Java常用类(三)

目录

1. 基本类型包装类

1.1 基本类型包装类

构造方法

比较方法

转换方法

其他方法:

1.2 装箱和拆箱

2. Math类

3.  Random类

4. System类


1. 基本类型包装类

1.1 基本类型包装类

基本数据类型:java中使用关键字直接进行声明,用法简单。(byte,short,int,long,char,boolean,float,double)

基本类型:没有面向对象的使用方式,所以java为每种基本类型定义了一个类,来表示基本类型数据,这个类就是包装类

作用:

(1)作为基本类型对应的类类型存在

(2)包含每种基本数据类型的相关的属性和相关的操作方法

基本类型基本类型包装类
byteByte
shortShort
intInteger
longLong
charCharacter
floatFloat
doubleDouble
booleanBoolean

以Integer包装类为例:(其他包装与其相似)

构造方法

Integer(int a);

Integer(String a);

源码:

public Integer(int value) {
            this.value = value;
        }
public Integer(String s) throws NumberFormatException {
            this.value = parseInt(s, 10);
  }

比较方法

static int compareTo(Integer a);

boolean equals(Object);

int max(int a,int b);

int min(int a,int b);

转换方法

static toBinaryString(int i); 转换成二进制

static String toHexString(int i); 转换成十六进制

static String toOctalString(int i); 转换成八进制

其他方法:

int intValue();  把对象中的基本类型取出来

static int parseInt(String s);   将String类型转换成int类型

String toString();  将int包装类型转为String类型

static Integer valueOf(int i); 将基本类型转为引用类型

static Integer valueOf(String s); 将引用类型转为基本类型

import java.util.Objects;

public class ObjectDemo1 {
    public static void main(String[] args) {
        /*
        基本类型包装类:
         */
        /*
        构造方法:
        1、
        public Integer(int value) {
            this.value = value;
        }
        2、
        public Integer(String s) throws NumberFormatException {
            this.value = parseInt(s, 10);
        }
         */
        Integer x =new  Integer("5");
        Integer y = new Integer(5);
        System.out.println(Integer.MAX_VALUE);//最大值 -2147483648
        System.out.println(Integer.MIN_VALUE);//最小值 2147483647
        System.out.println(Integer.BYTES);//4字节
        System.out.println(Integer.SIZE);//32位
        /*
        其他方法:
         */
        System.out.println(x == y);//比地址  false
        System.out.println(x.equals(y));//比值 true
        System.out.println(Integer.max(10, 5));//取最大值  10
        System.out.println(Integer.min(10, 5));//取最小值  5
        /*
        转换方法:
         */
        System.out.println(Integer.toBinaryString(5)); //转二进制 101
        System.out.println(Integer.toHexString(17));//转十六进制 11
        System.out.println(Integer.toOctalString(9));//转八进制 11
        /*
        int intValue()

         */
        Integer a = new Integer(10);//将基本类型包装到对象中
        int b = a.intValue();//把对象中的基本类型取出来
        /*
        static int parseInt(String s)将String类型转换成int类型
         */
        System.out.println(Integer.parseInt("25"));//25
        /*
        String toString()将int包装类型转为String类型
         */
        System.out.println(a.toString());//10
        /*
        static Integer valueOf(int i)将基本类型转换成引用类型
        static Integer valueOf(String s)将引用类型转换成基本类型
         */
        Integer d = Integer.valueOf(10);//10
        System.out.println(d);
        Integer e = Integer.valueOf("20");//20
        System.out.println(e);
        Integer f = Integer.valueOf("44", 8);
        System.out.println(f);

    }
}

1.2 装箱和拆箱

装箱拆箱

int a = 10;

Integer a1 = new Integer(a); 

a1 为包装类型

Integer b =Integer.valueOf(a); 

b 为引用类型

int c = b.intValue();

c为基本类型

自动装箱:自动将基本数据类型转换为包装器类型;底层默认调用ValueOf(int a)

  /*
        自动装箱:自动的将基本类型转换成引用类型
         */
        int a = 10;
        Integer a1 = a;
        Integer a2 = 10;
        //底层默认调用ValueOf(int a)

自动拆箱:自动将包装器类型转换为基本类型;底层调用的是Integer的intValue()

public class ObjectDemo2 {
    public static void main(String[] args) {
      
        /*
        自动拆箱:自动将引用类型转换成基本类型
        //底层默认调用intValue()
         */
        int a3 = a1;
        int a4 = a2;
        Integer x = 127;  // Integer x = 128;
        Integer y = 127;  // Integer y = 128; 
        System.out.println(x==y);//true / System.out.println(x==y); false
        /*public static Integer valueOf(int i) {
                          -128                            127
            if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
                return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
            return new Integer(i);
        }*/
        System.out.println(x.equals(y));//true

    }
}

x==y 为false原因:

 自动装箱 ,当x == y 时,在(-128~127)之间,x、y的地址相同,为true;否则为false。

源码:

public static Integer valueOf(int i) {
                         // -128                            127
            if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
                return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
            return new Integer(i);
}

2. Math类

Math类是简单的类,在java.lang包下,提供了一系列静态方法用于科学计算。

特点:其方法的参数和返回值类型一般为double型。

import java.util.Arrays;
import java.util.Random;

public class MathDemo {
    public static void main(String[] args) {
        Integer integer1 = new Integer(20);
        System.out.println(integer1);//20
        Integer integer2 = new Integer("10");//10
        System.out.println(integer2);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MIN_VALUE);

        //绝对值
        System.out.println(Math.abs(-1));//1
        //最大值
        System.out.println(Math.max(10, 5));//10
        //最小值
        System.out.println(Math.min(10, 5));//5
        //π值
        System.out.println(Math.PI);//3.141592653589793
        //向上取整
        System.out.println(Math.ceil(9.1));// 10.0
        //向下取整
        System.out.println(Math.floor(9.9));// 9.0
        //四舍五入
        System.out.println(Math.round(9.5));// 10
        //平方根
        System.out.println(Math.sqrt(9));// 3.0
        //取0~1之间的随机数
        System.out.println(Math.random());

        Random r = new Random();
        r.nextInt();//在int范围内随机取值
        System.out.println(r.nextInt());//在int范围内随机取值
        System.out.println(r.nextInt(100));//在0~100范围内取值(不包括100)
        byte[] ch = new byte[10];
        r.nextBytes(ch);
        System.out.println(Arrays.toString(ch));

    }
}

3.  Random类

此类用于产生随机数。

构造方法

public Random()

public Random() {
    this(seedUniquifier() ^ System.nanoTime());
}

成员方法

public int nextInt() 在int范围内随机取值

public int nextInt(int n) 在0~100范围内取值(不包括100)

import java.util.Arrays;
import java.util.Random;

public class MathDemo {
    public static void main(String[] args) {
     
        //取0~1之间的随机数
        System.out.println(Math.random());

        Random r = new Random();
        r.nextInt();//在int范围内随机取值
        System.out.println(r.nextInt());//在int范围内随机取值
        System.out.println(r.nextInt(100));//在0~100范围内取值(不包括100)
        byte[] ch = new byte[10];
        r.nextBytes(ch);
        System.out.println(Arrays.toString(ch));
        //[15, 127, -119, 59, 10, -46, 51, 15, -116, -11]

    }
}

4. System类

不能被实例化

成员方法:

public static void exit(int status)  退出程序(JVM)

public static long currentTimeMillis()   获取当前时间(long类型)

import java.util.Arrays;

public class SystemDemo {
    public static void main(String[] args) {
        /*
        System类,不能被实例化
         */
        //
        /*public final  static PrintStream out = null;*/

       // System.out.println( System.getenv());//环境变量
        //System.out.println(System.getenv("Path"));//Path的环境变量
        System.getProperties();//虚拟机
        /*
        常用的方法:
         */
        Long s = System.currentTimeMillis();//获取当前时间(long类型),
//自1970.1.1 00:00:00到程序运行时的那一刻的时间差(单位:ms)
        System.out.println(s);
        //System.exit(0);//退出程序(JVM)
        //System.gc();//垃圾回收,一般程序中不使用
        int a[] = {1,2,3,4,5};
        int b[] = new int[10];
        System.arraycopy(a, 1, b, 2, 4);
        System.out.println(Arrays.toString(b));//[0, 0, 2, 3, 4, 5, 0, 0, 0, 0]
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值