包装类与常用类

包装类与常用类,第1张

文章目录
    • 基本类型包装类
      • Integer类
        • 装箱拆箱
        • int和String相互转换
        • ==和equals练习
    • 常用类
      • Arrays类
        • Arrays.sort()练习
      • Math类
      • Random类
      • System类
      • BigDecimal类
      • BigInteger类
      • Date类
      • SimpleDateFormat类
      • Calendar类
        • 获取任意年份的2月份有多少天

基本类型包装类
概述
	对基本数据类型进行更多的 *** 作
	用于基本数据类型与字符串之间的转换
基本数据类型和包装类对应关系
	byte      Byte
	short     Short
	int       Integer
	long      Long
	float     Float
	double    Double
	char      Character
	boolean   Boolean
Integer类
概述
	Integer类在对象中包装类一个int类型的值
	int <-> String 相互转换
构造方法
	public Integer(int value)
	创建Integer 表示指定的int值
	public Integer(String s)
	创建Integer 表示int值表示的String参数
装箱拆箱
public static void main(String[] args) {
    //装箱:把基本数据类型转换成包装类类型
    //拆箱:把包装类类型转换成基本数据类型
    int num = 10;

    //手动装箱 int->Integer
    Integer integer1 = new Integer(num);
    Integer integer2 = Integer.valueOf(num);

    //手动拆箱 Integer->int
    int i = integer1.intValue();

    //jdk5以后可以自动装箱 自动拆箱
    //自动装箱 int->Integer
    //底层还是Integer.valueOf(num)
    Integer integer3 = num;

    //自动拆箱 Integer->int
    //底层还是intValue()
    int n = integer3;
}
int和String相互转换
public static void main(String[] args){
        int a = 100;
        //int -> String
        //拼接空字符
        String a1 = a + "";
        //Integer.toString()方法
        String toString = Integer.toString(a);
        //String.valueOf()方法
        String valueOf = String.valueOf(a);

        String b = "200";
        //String -> int
        //Integer.parseInt()方法
        int parseInt = Integer.parseInt(b);
        //Integer构造方法
        Integer integer = new Integer(b);
    }
==和equals练习
public static void main(String[] args) {
    //new Integer(i)
    //如果i在-128~127就直接返回
    //如果i不在-128~127底层就new Integer(i)
    
    Integer i1 = new Integer(127);
    Integer i2 = new Integer(127);
    System.out.println(i1 == i2);//false
    System.out.println(i1.equals(i2));//true
    System.out.println("-----------");

    Integer i3 = new Integer(128);
    Integer i4 = new Integer(128);
    System.out.println(i3 == i4);//flase
    System.out.println(i3.equals(i4));//true
    System.out.println("-----------");

    Integer i5 = 128;
    Integer i6 = 128;
    System.out.println(i5 == i6);//flase
    System.out.println(i5.equals(i6));//true
    System.out.println("-----------");

    Integer i7 = 127;
    Integer i8 = 127;
    System.out.println(i7 == i8);//true
    System.out.println(i7.equals(i8));//true
}

常用类 Arrays类
概述
	对数组进行 *** 作的工具类
	提供了排序 查找等方法
成员方法
	public static String toString(int[] a)
	以字符串表示形式返回指定数组的内容
	public static void sort(int[] a)
	将指定的数组排序(从小到大)
	public static int binarySearch(int[] a,int key)
	通过二分查找的方式查找数组中的元素(数组元素必须有序) 
	static boolean equals(int[] a, int[] a2) 
	比较两个数组元素 是否一致
    static int[] copyOf(int[] original, int newLength) 
    复制指定的数组 拷贝newLength个元素到新数组中
    static int[] copyOfRange(int[] original, int from, int to)
    复制指定数组 拷贝索引从from到to的元素到新数组
    public static void fill(int[] a, int val)
    数组填充 用val替换数组a中的元素
Arrays.sort()练习
排序 Arrays.sort()
1.数组是引用类型 sort()排序后 会直接影响到实参
2.可以通过传入一个接口Comparator实现定制排序
3.调用定制排序 传入两个参数 排序数组
实现Comparator接口的匿名内部类 实现compare方法
return 0;  排序不变
return 1;  从小到大
return -1; 从大到小
import java.util.Arrays;
import java.util.Comparator;
public class ArraysExercise {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦", 100);
        books[1] = new Book("圣墟", 90);
        books[2] = new Book("青年文摘", 10);
        books[3] = new Book("java从入门到放弃", 300);

        //通过价钱排序
        Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                return (b1.getPrice() - b2.getPrice());
            }
        });
        System.out.println(Arrays.toString(books));

        //通过书名长度排序
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                return o1.getName().length() - o2.getName().length();
            }
        });
        System.out.println(Arrays.toString(books));
    }
}

class Book {
    private String name;
    private int price;
    public Book(String name, int price) {
        this.name = name;
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

Math类
概述
	Math类包含基本数学运算方法
成员变量
	public static final double E  自然底数
	public static final double PI 圆周率
成员方法
	public static int abs(int a)		
	取绝对值 Math.abs(-9) -> 9
	public static double ceil(double a)	
	向上取整 Math.ceil(3.9) -> 4.0
	public static double floor(double a)	
	向下取整 Math.floor(4.001) -> 4.0
	public static int max(int a,int b)      
	获取最大值 Math.max(45, 90) -> 90
	public static int min(int a, int b)	
	获取最小值 Math.min(1, 9) -> 1
	public static double pow(double a,double b) 
	获取a的b次幂 Math.pow(2, 4) -> 2^4
	public static int round(float a) 
	四舍五入 Math.round(5.51) -> 6
	public static double sqrt(double a)
	获取正平方根 Math.sqrt(9.0) -> 3.0
	public static double random()
	求随机数 random返回的是0<=x<1之间的一个随机小数
	a~b之间的随机数
	公式(int)(a + Math.random()*(b-a +1) )

Random类
概述
	用于产生随机数
构造方法
	public Random()			
    没有给定种子,使用的是默认的(当前系统的毫秒值)
	public Random(long seed)	
    给定一个long类型的种子,给定以后每一次生成的随机数是相同的
成员方法
	public int nextInt()
	没有参数 表示的随机数范围 是int类型的范围
	public int nextInt(int n)
	可以指定一个随机数范围 
	void nextBytes(byte[] bytes)  
	生成随机字节并将其置于用户提供的空的 byte 数组中

System类
概述
	System类包含了一些类字段和方法
	不能被实例化
成员方法
	public static void gc()
	调用垃圾回收器
	public static void exit(int status)
	退出java虚拟机 0为正常退出 非0为异常退出
	public static long currentTimeMillis()
	返回当前时间距离1970-1-1 00:00:00的毫秒数

BigDecimal类
概述
	运算时float类型和double类型容易丢失精度
	BigDecimal类能精确表示和计算浮点数
构造方法
	public BigDecimal(String val)
	用字符串表示数值
成员方法
	BigDecimal a = new BigDecimal("3.255858585747474");
    BigDecimal b = new BigDecimal("3.25585847474747499999");
    
	public BigDecimal add(BigDecimal augend)
	加 a.add(b)
	
	public BigDecimal subtract(BigDecimal subtrahend)
	减 a.subtract(b)
	
	public BigDecimal multiply(BigDecimal multiplicand)
	乘 a.multiply(b)
	
	public BigDecimal divide(BigDecimal divisor)
	除(能整除情况) a.divide(b) 
	public BigDecimal divide(BigDecimal divisor, int roundingMode)
	除(不能整除情况) a.divide(b, BigDecimal.ROUND_CEILING)
	BigDecimal.ROUND_CEILING如果有无限循环小数 就会保留分子的精度
	
	public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
	scale 小数点后面保留几位 
	roundingMode 取舍模式 比如四舍五入

BigInteger类
概述
	运算时long类型不够用
	BigInteger类可以对超过long类型范围的数据进行运算
构造方法
	public BigInteger(String val)	
	用字符串表示数值
成员方法	
    public BigInteger add(BigInteger val)
    加
    public BigInteger subtract(BigInteger val)
    减
    public BigInteger multiply(BigInteger val)
    乘
    public BigInteger divide(BigInteger val)
    除

Date类
概述
	Date类表示特定的瞬间 精确到毫秒 
构造方法
	public Date()
	返回当前系统时间
	public Date(long date) 
	把一个long类型的毫秒值转换成一个日期
成员方法
	public long getTime()
	获取一个日期对象对应的毫秒值
	public void setTime(long time)
    给一个日期对象设置上指定的毫秒
类型转换
	Date date = new Date();
	Date -> long
	date.getTime();	
	long -> Date
	date.setTime()

SimpleDateFormat类
概述
	可以把一个日期对象格式化成一个文本(字符串)
	也可以把一个日期字符串解析成一个日期对象
构造方法
	public SimpleDateFormat()
	使用默认的模式来创建一个对象
	public SimpleDateFormat(String pattern)
	使用指定的模式(规则比如yyyy:MM:dd HH:mm:ss)来创建一个对象  
    规则的定义:
        y------年
        M------月
        d------天
        H------时
        m------分
        s------秒
成员方法
	public String format(Date date)
	把一个日期对象格式化成一个字符串
	public Date parse(String dateStr)
    把一个日期字符串解析成一个日期对象 注意要以指定格式解析
调用顺序
	Date date = new Date();
	SimpleDateFormat s = new SimpleDateFormat();
Date -> String
	String format = s.format(date);
String -> Date
	Date parse = s.parse(format);

Calendar类
概述
	Calendar类是一个抽象类 不能直接new对象 可以通过他的一个静态成员方法getInstance()来获取他的对象
成员方法
	public static Calendar getInstance()
    使用默认时区和语言环境获得一个日历对象
	public int get(int field)
    获得给定日历字段对应的值 field通过Calendar提供的字段来拿
	public void add(int field,int amount)	
    根据日历的规则,为给定的日历字段添加或减去指定的时间量
	public final void set(int year,int month,int date)
    设置日历时间 年月日
获取任意年份的2月份有多少天
public static void main(String[] args) {
        //获取任意年份的2月份有多少天
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个年份");
        int year = sc.nextInt();

        Calendar instance = Calendar.getInstance();
        instance.set(year,2,1);
        //2表示3月
        instance.add(Calendar.DAY_OF_MONTH,-1);
        //三月一号前一天就是二月最后一天
        int day = instance.get(Calendar.DAY_OF_MONTH);
        //获取二月最后一天的值
        System.out.println(day);
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/876706.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存