包装类与常用类

文章目录

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

基本类型包装类

概述对基本数据类型进行更多的操作用于基本数据类型与字符串之间的转换
基本数据类型和包装类对应关系byte      Byteshort     Shortint       Integerlong      Longfloat     Floatdouble    Doublechar      Characterboolean   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->IntegerInteger integer1 = new Integer(num);Integer integer2 = Integer.valueOf(num);//手动拆箱 Integer->intint 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);//falseSystem.out.println(i1.equals(i2));//trueSystem.out.println("-----------");Integer i3 = new Integer(128);Integer i4 = new Integer(128);System.out.println(i3 == i4);//flaseSystem.out.println(i3.equals(i4));//trueSystem.out.println("-----------");Integer i5 = 128;Integer i6 = 128;System.out.println(i5 == i6);//flaseSystem.out.println(i5.equals(i6));//trueSystem.out.println("-----------");Integer i7 = 127;Integer i8 = 127;System.out.println(i7 == i8);//trueSystem.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() {@Overridepublic 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>() {@Overridepublic 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;}@Overridepublic 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) -> 9public static double ceil(double a)	向上取整 Math.ceil(3.9) -> 4.0public static double floor(double a)	向下取整 Math.floor(4.001) -> 4.0public static int max(int a,int b)      获取最大值 Math.max(45, 90) -> 90public static int min(int a, int b)	获取最小值 Math.min(1, 9) -> 1public static double pow(double a,double b) 获取a的b次幂 Math.pow(2, 4) -> 2^4public static int round(float a) 四舍五入 Math.round(5.51) -> 6public static double sqrt(double a)获取正平方根 Math.sqrt(9.0) -> 3.0public 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 -> longdate.getTime();	long -> Datedate.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 -> StringString format = s.format(date);
String -> DateDate 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);}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部