工具类的学习笔记

工具类

  • 1.包装类
  • 2.与数学有关的类
  • 3.日期相关
  • 4.字符串相关
  • 5.集合相关
  • 6.异常有关
  • 7.输入输出有关
  • 8.网络通信相关
  • 9.反射与注解
  • 10.GUI Swing

一.包装类。

为了让基本类型与引用类型有了联系,是基本类型与引用类型之间的桥梁。

基本类型 包装类

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

char

Character

boolean

Boolean

需要借助官方API帮助文档去学习工具类(相当于使用说明书) API:Application Programming Interface 应用程序编程接口

java API 链接

怎样去学习呢?

1.类所在的包

2.类的关系,自己默认的继承 实现

3.类中提供的常用的方法

4.类是否可以创建对象,调用方法

总结

  1. 八个包装类都在java.lang包里面,不需要import导包直接就可以使用。
  2. 八个包装类中前六个都与数字有关,都默认继承父类Number。
  3. 八个包装类都实现了Serializable,Comparable接口。
  4. 八个包装类都有带自己对应的基本类型参数的构造方法,除了Character之外,还有七个有构造方法的重载,参数是String 类型。
  5. 创建对象,调用属性/方法。

有六个与数字有关的类都继承了Number。

Number类提供了xxxxValue()方法,是将一个包装类型对象转化为对应的基本类型(即拆包),下面是这些xxxxValue()方法的源码。

public abstract int intValue();

/**
 * Returns the value of the specified number as a {@code long},
 * which may involve rounding or truncation.
 *
 * @return  the numeric value represented by this object after conversion
 *          to type {@code long}.
 */
public abstract long longValue();

/**
 * Returns the value of the specified number as a {@code float},
 * which may involve rounding.
 *
 * @return  the numeric value represented by this object after conversion
 *          to type {@code float}.
 */
public abstract float floatValue();

/**
 * Returns the value of the specified number as a {@code double},
 * which may involve rounding.
 *
 * @return  the numeric value represented by this object after conversion
 *          to type {@code double}.
 */
public abstract double doubleValue();

/**
 * Returns the value of the specified number as a {@code byte},
 * which may involve rounding or truncation.
 *
 * <p>This implementation returns the result of {@link #intValue} cast
 * to a {@code byte}.
 *
 * @return  the numeric value represented by this object after conversion
 *          to type {@code byte}.
 * @since   JDK1.1
 */
public byte byteValue() {
    return (byte)intValue();
}

/**
 * Returns the value of the specified number as a {@code short},
 * which may involve rounding or truncation.
 *
 * <p>This implementation returns the result of {@link #intValue} cast
 * to a {@code short}.
 *
 * @return  the numeric value represented by this object after conversion
 *          to type {@code short}.
 * @since   JDK1.1
 */
public short shortValue() {
    return (short)intValue();
}

在jdk1.5之后,可以自动拆装包:

Integer i3=10;//自动将基本类型int转换为包装类Integer对象。
int value =new Integer(10);//自动将包装类Integer对象转换为基本类型int。

在jdk1.5以前,是这样实现拆装包的:

Integer i1=new Integer(10);//将基本类型转化为包装类对象。
int i2=i1.intValue();//将包装类拆为基本类型。

还有一个方法,比较常用:

6.笔试中经常出现的问题。

Integer i1=10;
Integer i2=10;
Integer i3=new Integer(10);
Integer i4=new Integer(10);
System.out.println(i1==i2);true
System.out.println(i1==i3);false
System.out.println(i3==i4);false
System.out.println(i1.equals(i2));true
System.out.println(i1.equals(i3));true
System.out.println(i3.equals(i4));true
涉及==号与equals方法的区别
1.==可以比较基本数据类型也可比较引用类型
若比较的是基本类型,比较的是变量中存的值。
若比较的是引用类型,比较的是变量中存储的地址引用。
总的来说,比的是变量中存储的内容。
2.equals是Object类中继承过来的方法,每一个引用类型都可以调用
默认继承的equals方法与==号一致,比较的是存储的内容。
//Object类中的equals方法
public boolean equals(Object obj) {
    return (this == obj);
}
但是,若想要改变比较的规则,可以重写equals方法:
//Integer类中重写的equals方法。
public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}
因为Integer类就重写了equals,所以Integer比较的是数值。

上面的题的内存图:

 但是又发现了一个新的问题,若把上面题中的值改为1000,却和上面的结果不一致。

Integer i1=1000;
Integer i2=1000;
Integer i3=new Integer(1000);
Integer i4=new Integer(1000);
System.out.println(i1==i2);//false
System.out.println(i1==i3);//false
System.out.println(i3==i4);//false
System.out.println(i1.equals(i2));//true
System.out.println(i1.equals(i3));//true
System.out.println(i3.equals(i4));//true

这就跟Integer类的加载有关了,Integer在加载时,自己有一个静态的空间,空间内立即加载Integer类型的数组(cache),数组里面存储256个Integer对象(-127~128),如果我们用的对象范围在这之内,(比如:Integer i1=10)直接去静态区中找对应的对象。如果超出了范围(比如:Integer i1=1000)会帮我们创建一个新的Integer对象。

Integerle类的加载。

二.数学相关的类。

Math类

1.所属的包:java.lang

2.Math类的构造方法是私有的,不能直接调用构造方法。

3.Math类中提供的属性及方法都是静态的,故不需要创建对象,直接通过类名调用。

4.Math类有很多方法

abs();绝对值

random();随机数

pow(a,b);a的b次方(a,b都为double ,返回值也为double)

ceil() floor() rint() round()

System.out.println(Math.ceil(1.2));//向上取整,返回值为double  2.0
System.out.println(Math.floor(1.2));//向下取整,返回值为double  1.0
System.out.println(Math.rint(1.2));//临近的整数,若两边距离一样,返回而偶数(返回值为double)1.0
System.out.println(Math.round(1.2));//四舍五入的整数  1,返回值为int或long,传float返回int,传double,返回long

abs(); 绝对值 参数可以为: int long float double

max(); 参数可以为: int long float double

min(); 参数可以为: int long float double

sqrt(); 计算平方根 参数和返回值都是double

random();随机产生一个[0.0,1.0)之间的数,返回值为double类型

5.Math.random()计算小数的时候精度可能会有一些损失。所以,就需要另外一个类,

Random类,专门用来产生随机数。

Random类

  • 该类在java.util包中,需要import导包。
  • 该类没有任何继承关系,默认继承Object类。
  • 创建对象Random r=new Random();
  • 该类中的常用方法:
r.nextInt();//随机产生一个int取值范围内整数
int v = r.nextInt(10);//0-bound之间的范围[0,bound)内随机产生一个整数,bound必须为正数,若为负数,抛出IllegalArgumentException运行时异常
float f=r.nextFloat();//随机产生一个[0.0--1.0)范围内的float类型数
boolean c=r.nextBoolean();//随机产生true或false

UUID类

  • 所属的包:java.util 需要import来导包
  • 没有任何继承关系,默认继承Object类
  • 没有无参数构造方法,所以通常不会创建对象
一般会通过UUID.来调用randomUUID方法,产生一个32位随机元素,每一位是一个16进制的数字
UUID id=UUID.randomUUID();
System.out.println(id.toString());//通常用来生成数据库表格的主键

BigInteger类----大整数(超出了int的范围)

  • 所属的包:java.math
  • 继承自Number类
  • 创建对象:构造方法全部带参数,通常用带String参数的构造方法创建对象,例如:
BigInteger bigInteger=new BigInteger("1234422");

类中常用的方法:

BigInteger bigInteger=new BigInteger("6");
BigInteger bigInteger1=new BigInteger("1");
System.out.println(bigInteger.add(bigInteger1));//加------7
System.out.println(bigInteger.subtract(bigInteger1));//减------6
System.out.println(bigInteger.multiply(bigInteger1));//乘-------6
System.out.println(bigInteger.divide(bigInteger1));//除--------6

可以利用BigInteger类来求阶乘,例子如下:

public BigInteger jc(int num){//求阶乘
    BigInteger result=new BigInteger("1");
    for(int i=1;i<=num;i++){
        result = result.multiply(new BigInteger(i+""));    
    }
    return result;
}

BigDecima类----大的小数(超出了double的范围)

  • 所属包java.math
  • 继承Number类
  • 创建对象:构造方法全部带参数,通常用带String参数的构造方法创建对象。
  • 也可以做四则运算,方法与上面的类中的四则运算方法一样。
  • scal()方法,用来设置小数点之后的位数,如下图:
BigDecimal decimal=new BigDecimal("3.1415926535");
对象.setScale(参数一,参数二)
参数一:小数点后的位数   参数二:设置的模式(向下,向上取整等等)
decimal=decimal.setScale(2,BigDecimal.ROUND_DOWN);//小数点后面保留两位,按照向下取整
  • 还有一个可以将小数点之后和之前都处理的类:DecimalFormat
  • 所属的包:java.text
  • 通过带String类型参数的构造方法创建一个格式化对象
  • 调用format方法将一个小数转换为一个字符串。
DecimalFormat df =new DecimalFormat("000.####");//格式:0(必需存在)和#(可有可无)
String t=df.format(11.411111);输出011.4111
String t=df.format(1111.4);输出1111.4

三.日期相关的类。

1.System类

  • 属于Java.lang包(不用导包)
  • 有in out对象作为属性,gc()---回收对象 exit(0)
  • currentTimeMillis();方法---获取当前系统时间(当前系统时间与1970年1月1日 00:00:00之差 ,是一个毫秒值),返回值为long类型。

2.Date类

  • 属于java.util包
  • 无参数的构造方法 new Date(); new Date(long time);
  • after() before() setTime() getTime();
  • comepareTo();比较Date类型的时间的前后的 ,返回值为1 0 -1

3.SimpleDateFormat类

  • 在java.text包
  • 带String参数的构造方法创建对象 new SimpleDateFormat("yyyy-mm-dd kk:mm:ss");
  • String value=对象.format(date);

4.Calender类

  • 属于java.util类
  • 通过类中的一个方法创建对象Calender c=Calender.newInstance();//默认当前统时间的一个对象
  • after() before() setTime() getTime();
  • set(Calender.YEAR,值); get(Calender.YEAR);
  • getTimeInMillis();
  • getTimeZone();获取对应的时区

5.TimeZone类

需要通过类中的一个方法创建对象

TimeZone tz=TimeZone.getDefault();

getID(); getDisplayName();

四.字符串相关的类。

String类

提纲:

  • 属于java.lang包

没有任何继承关系,实现了三个接口Serializable,CharSequence,Comparable

  • 如何创建对象
  1. 直接创建:String str="abc";直接将字符串常量赋值给str,值存储在字符串常量池。(直接把引用指向字符串常量值)
  2. 无参构造方法创建空的String对象:String str = new String();(不常用)

3.有参(带String类型参数)的构造方法创建:String str = new String("abc");(常用)

4.String str = new String(byte[ ]); 将数组中的每一个元素转化成对应的char 组合成String

5.String str = new String(char[ ]); 将数组中的每一个char元素拼接成最终的String

  • 常用的方法
  1. boolean = equals(Object obj);

继承自Object类中的方法 重写啦 改变了规则 比较字符串中的字面值

==和equals()区别

equalsIgnoreCase();

2.int = hashCode();

继承自Object类中的方法 重写啦 31*h+和

3.int = compareTo();

实现自Comparable接口 实现方法 结果按照字典排布(unicode编码)顺序

按照两个字符串的长度较小的那个(次数)来进行循环

若每次的字符不一致 则直接返回code之差

若比较之后都一致 则直接返回长度之差

compareToIgnoreCase();

4.String = toString()

没有重写过的toString从Object类中继承过来的

类名@hashCode(16进制形式)

String类重写啦 返回的是String对象的字面值

5.char = charAt(int index);

返回给定index位置对应的字符

int = codePointAt(int index);

返回给定index位置对应字符的code码

6.int = length();

返回字符串的长度 (其实就是底层char[] value属性的长度)

注意: 区别数组length是属性 String的length()方法 集size()方法

7.String = concat(String str);

将给定的str拼接在当前String对象的后面

注意: 方法执行完毕需要接受返回值 String的不可变特性

concat方法与 +拼接的性能问题

开发中若遇到频繁的拼接字符串--->使用StringBuilder/StringBuffer 8.boolean = contains(CharSequence s);

判断给定的s是否在字符串中存在

9. startsWith(String prefix);

endsWith(String suffix);

判断此字符串是否已xx开头/结尾

10.byte[] = getBytes(); ---> getBytes(String charsetName);

char[] = toCharArray();

将当前的字符串转化成数组 "我爱你中国" char[] '我' '爱' '你' '中' '国'

11. int index = indexOf(int/String str [,int fromIndex] ); 四个方法重载

找寻给定的元素在字符串中第一次出现的索引位置 若字符串不存在则返回-1

lastIndexOf(int/String str , [int fromIndex]);

找寻给定的元素在字符串中最后一次出现的索引位置 若不存在则返回-1

12.boolean = isEmpty();

判断当前字符串是否为空字符串 (length是否为0)

注意: 与null之间的区别

13.replace();

replaceAll();

replaceFirst(); 换第一次出现的那个字串

将给定的字符串替换成另外的字符串

14.String[] = split(String regex [,int limit限度界限]);

按照给定的表达式将原来的字符串拆分开的

15.String = substring(int beginIndex [,int endIndex]);

将当前的字符串截取一部分

从beginIndex开始至endIndex结束 [beginIndex,endIndex)

若endIndex不写 则默认到字符串最后

16.String = toUpperCase();

String = toLowerCase();

将全部字符串转换成大写/小写

17.String = trim();

去掉字符串前后多余的空格

18.boolean = matches(String regex)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值