Java实用类

Java的实用类

一、包装类

基于基本数据类型所对应的引用类型,包装类既然是引用类型,那么其拥有null值,以及对应的API

image

(1)数值类型的创建

package com.mine.demo01;
public class Test1 {
	public static void main(String[] args) {
		//数值
		Integer i1 = new Integer(100);//基本数据类型为参数
		Integer i2 = new Integer("100");//以字符串为参数
		System.out.println(i1);
		System.out.println(i2);
	}
}

注意:

超出边界

package com.mine.demo01;
public class Test2 {
	public static void main(String[] args) {
		//数值
		Byte b = new Byte("1000");
		System.out.println(b);
	}
}

结果:
Exception in thread "main" java.lang.NumberFormatException: Value out of range.
Value:"1000" Radix:10
at java.lang.Byte.parseByte(Byte.java:151)
at java.lang.Byte.<init>(Byte.java:316)
at com.qf.demo01.Test2.main(Test2.java:6)
  • 格式化异常
package com.mine.demo01;
public class Test3 {
	public static void main(String[] args) {
		//数值
		Byte b = new Byte("1000a");
		System.out.println(b);
	}
}

结果:
Exception in thread "main" java.lang.NumberFormatException: For input string:
"1000a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Byte.parseByte(Byte.java:149)
at java.lang.Byte.<init>(Byte.java:316)
at com.qf.demo01.Test3.main(Test3.java:6)
  • Long类型不用加L/l
package com.mine.demo01;
public class Test4 {
	public static void main(String[] args) {
		//数值
		Long l = new Long("3000000000L");
		System.out.println(l);
	}
}


结果:
Exception in thread "main" java.lang.NumberFormatException: For input string:
"3000000000L"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.<init>(Long.java:965)
at com.qf.demo01.Test4.main(Test4.java:6)
  • 浮点数标识位
package com.mine.demo01;
	public class Test5 {
public static void main(String[] args) {
		//数值
		Float f = new Float("3.14F");
		Double d = new Double("3.1415926D");
		System.out.println(f);
		System.out.println(d);
	}
}

结果:
3.14
3.1415926

(2)字符类型的创建

package com.mine.demo02;
public class Test1 {
	public static void main(String[] args) {
		Character c = new Character('y');//char只有一种构造
	}
}

(3)布尔类型的创建

package com.mine.demo02;
public class Test2 {
	public static void main(String[] args) {
		Boolean b1 = new Boolean(true);
		Boolean b2 = new Boolean(false);
		Boolean b3 = new Boolean("abcde");
        System.out.println(b1);
        System.out.println(b2);
		System.out.println(b3);
	}
}

结果:
true    
false
false

结果:

如果参数为null值,或者不为true字符串,那么结果为false

(4)字符串和对应数据类型的转换

核心的API

package com.mine.demo03;
import java.nio.DoubleBuffer;
public class Test2 {
	public static void main(String[] args) {
		String arg = "3.1415926";
		//字符串转对应类型
		double d1 = Double.parseDouble(arg);
		Double d2 = Double.valueOf(arg);
		System.out.println(d1);
		System.out.println(d2);
	}
}

结果:
3.1415926
3.1415926

无需掌握的API

package com.mine.demo03;
public class Test1 {
	public static void main(String[] args) {
		Integer i1 = new Integer(100);
		int i2 = i1.intValue();//包装类型转基本数据类型
		Integer i3 = Integer.valueOf(100);//基本数据类型转包装类型
	}
}

(5)自动装箱和自动拆箱

package com.mine.demo03;
public class Test3 {
	public static void main(String[] args) {
		Integer i1 = 1;//自动装箱
		int i2 = i1;//自动拆箱
	}
}

建议:从此刻开始,放弃使用基本数据类型,而全部使用包装类型

二、String类(字符串)

(1)字符串的内存问题

package com.mine.demo04;
public class Test1 {
	public static void main(String[] args) {
		String str1 = "df";//常量池
		String str2 = "df";//常量池
		String str3 = new String("df");//堆
		String str4 = new String("df");//堆
		System.out.println(str1 == str2);
		System.out.println(str1 == str3);
		System.out.println(str3 == str4);
	}
}


结果:
true
false
false

(2)怎么把一个字符串对象加入常量池

package com.mine.demo04;
public class Test2 {
	public static void main(String[] args) {
		String str1 = new String("df");//堆
		String str2 = "df";
		String str3 = str1.intern();//把一个在堆当中的字符串加入常量池,
        //如果常量池没有该对象,则新建;如果有,则返回地址。
		System.out.println(str1 == str2);
		System.out.println(str1 == str3);
		System.out.println(str2 == str3);
	}
}


结果:
false
false
true

(3)一些intern的应用场景

package com.mine.demo05;
import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入一个字符串:");
		String str = scanner.next();
		test(str);
	}
	public static void test(String str){
		str = str.intern();
		if("df" == str){
		System.out.println(true);
	}else{
		System.out.println(false);
		}
	}
}


结果:
请输入一个字符串:df
true

(4)字符串的API

注意:
1.String的本质是一个char类型的数组
2.它是一个最终类
3.它可以被序列化(暂时先了解一下,不用背)
4.用于字符集的格式化

image

package com.mine.demo1;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Test13 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str1 = "WWW.DFshmily.STUDIO";
        String str2 = "www.dfshmily.studio";
        String str3 = "www.dfshmily.studio";
        //获取指定位置的字符
        char c = str1.charAt(4); 
        System.out.println(c);//D

        //判断两个字符串是否相等(区分大小写):相等的条件是,内容和长度都相等
        System.out.println(str1.equals(str2));//false
        System.out.println(str2.equals(str3));//true

        //判断两个字符串是否相等(不区分大小写):相等的条件是,内容和长度都相等
        System.out.println(str1.equalsIgnoreCase(str2));//true
        System.out.println(str2.equalsIgnoreCase(str3));//ture

        //字符串格式化(格式化见表格)
        System.out.println(String.format("字符串格式化是:%s",str1)); //字符串格式化是:WWW.DFshmily.STUDIO

        //获取字符串的字符编码
        String str4 = "哈哈哈";
        System.out.println(Arrays.toString(str4.getBytes()));  //[-27, -109, -120, -27, -109, -120, -27, -109, -120]

        //字符集转码
        byte[] str5 = str4.getBytes("UTF-8");
        System.out.println(str5);  //[B@5cad8086
        String str6 = new String(str5,"iso-8859-1");
        System.out.println(str6);  //哈哈哈

        str5 = str6.getBytes("iso-8859-1");
        str6 = new String(str5,"UTF-8");
        System.out.println(str6);  //哈哈哈

        //从左往右判断某个字符是否存在,返回该字符第一次出现的位置,如果没找到返回-1
        System.out.println(str1.indexOf("W"));   //0

        //从右往左判断某个字符是否存在,返回该字符从左往右最后一次出现的位置,如果没找到返回-1
        System.out.println(str1.lastIndexOf("W")); //2

        //判断字符串是否为空,判断字符串长度是否为0
        System.out.println("".isEmpty());//使用Spring的StringUtils.isEmpty(str) ==>null && ""  //true

        //字符串长度
        System.out.println(str1.length());  //19

        //替换
        String filePath = "d://file%20path//file%20path1";
        filePath = filePath.replaceAll("20","df");
        System.out.println(filePath);   //d://file%dfpath//file%dfpath1

        //特殊符号要转义: . ^ & \
        String str7 = str1.replaceAll("\\.","_");
        System.out.println(str7);   //WWW_DFshmily_STUDIO

        //分割字符串,变成一个数组
        String[] strarr = str1.split("\\.");
        System.out.println(Arrays.toString(strarr));  //[WWW, DFshmily, STUDIO]

        //截取某一段
        //1.从指定游标处截取到末尾
        System.out.println(str1.substring(4));   //DFshmily.STUDIO
        //2.从指定游标1处开始,截取到指定游标2之前一位
        System.out.println(str1.substring(4,12));  //DFshmily

        //大小写转换
        String str8 = "study";
        String firstName = String.valueOf(str8.charAt(0)).toUpperCase();//将首字母变为大写
        System.out.println(firstName);    //S
        String setName = firstName + str8.substring(1);//substring(1)将从下标为1开始截取
        System.out.println(setName);  //Study

        //大写转小写
        System.out.println(str1.toLowerCase());  //www.dfshmily.studio

        //去空格
        String str9 = " a b c ";
        System.out.println(str9.trim()); //去两边空格    //a b c
        System.out.println(str9.replaceAll(" ",""));//去所有空格   //abc
    }
}

结果:

D
false
true
true
true
字符串格式化是:WWW.DFshmily.STUDIO
[-27, -109, -120, -27, -109, -120, -27, -109, -120]
[B@5cad8086
哈哈哈
哈哈哈
0
2
true
19
d://file%dfpath//file%dfpath1
WWW_DFshmily_STUDIO
[WWW, DFshmily, STUDIO]
DFshmily.STUDIO
DFshmily
S
Study
www.dfshmily.studio
a b c
abc

三、BigDecimal

1.概念

精确运算极大数和极小数要用到,其本质是字符串(速度极慢)

案例:

package com.mine.demo1;

import java.math.BigDecimal;

public class Test5 {
    public static void main(String[] args) {
        /*
        计算不精确
         */
        double i = 1 - 0.9;
        System.out.println(i);

        
        BigDecimal bd1 = new BigDecimal("1");
        BigDecimal bd2 = new BigDecimal("0.9");
        BigDecimal bd3 = bd1.subtract(bd2);
        System.out.println(bd3);

    }
}


结果:
0.09999999999999998
0.1
package com.mine.demo1;

import java.math.BigDecimal;

public class Test6 {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal(10);
        BigDecimal bd2 = new BigDecimal(3);
        System.out.println(bd1.divide(bd2,3,BigDecimal.ROUND_HALF_UP));

    }
}


结果:
3.333

四、StringBuffer和StringBuilder

高效的可变长字符串拼接工具(速度极快)

package com.mine.demo1;

public class Test7 {
    public static void main(String[] args) {

        long start = System.currentTimeMillis();

        StringBuffer sbf = new StringBuffer();
        sbf.append("a");
        sbf.append("b");
        sbf.append("c");
        sbf.append("d");
        sbf.insert(0,"df");
        System.out.println(sbf);

        String str = sbf.toString();
        System.out.println(str);
        long end = System.currentTimeMillis();
        System.out.println(end - start);//程序执行完耗时
    }
}


结果:
dfabcd
dfabcd
1
package com.mine.demo1;

public class Test8 {
    public static void main(String[] args) {
        StringBuilder sbd = new StringBuilder();
        sbd.append("a");
        sbd.append("b");
        sbd.append("c");
        sbd.append("d");
        sbd.insert(0,"df");
        System.out.println(sbd);

        String str = sbd.toString();
        System.out.println(str);
    }
}


结果:
dfabcd
dfabcd

区别

1.StringBuilder比StringBuffer效率高

2.StringBuffer线程安全

3。建议:只用StringBuffer

五、Date(时间)

核心的类:java.util.Date,当前的计算机时间

package com.mine.demo1;

import javax.xml.crypto.Data;
import java.util.Date;

public class Test9 {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);

        long time = date.getTime();
        //1970-01-01 0:0:0 到当前时间的毫秒数
        System.out.println(time);//基于格林威治时间的毫秒计时

    }
}


结果:
Fri Mar 17 17:17:21 CST 2023
1679044641052

时间运算

package com.mine.demo1;

import java.util.Date;

public class Tets10 {
    public static void main(String[] args) {

        Date date = new Date();//获取当前时间
        long nowDate = date.getTime();//将当前时间转化为格林威治格式的当前时间
        long time = 1000L * 60 * 60 * 24 * 365 * 10;//10年的毫秒数
        long oldDate = nowDate - time;//10年的毫秒计时
        Date d = new Date(oldDate);//从毫秒计时变为时间对象
        System.out.println(d);//输出10年前这时候的时间
    }
}


结果:
Tue Mar 19 17:26:09 CST 2013

获取时间单位

package com.mine.demo1;

import java.util.Calendar;
import java.util.Date;

public class Test11 {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(0));//设定时间,new Date(0)为Thu Jan 01 08:00:00 CST 1970
        //System.out.println(c);

        //获取时间单位
        System.out.println(c.get(Calendar.YEAR));
        System.out.println(c.get(Calendar.MONTH + 1));
        System.out.println(c.get(Calendar.DAY_OF_MONTH));
        System.out.println(c.get(Calendar.HOUR));
        System.out.println(c.get(Calendar.MINUTE));
        System.out.println(c.get(Calendar.SECOND));
        System.out.println(c.get(Calendar.MILLISECOND));

    }
}


结果:
1970
1
1
8
0
0
0

六、时间格式化

时间格式化:SimpleDateFormat

image

package com.mine.demo1;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test12 {
    public static void main(String[] args) throws Exception{
        SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr = "2023-3-17 00:00:00";
        Date d = sfd.parse(timeStr);//将字符串转换为时间
        System.out.println(d);
        String newTimeStr = sfd.format(d);
        System.out.println(newTimeStr);//将时间格式转换为字符串

        System.out.println();

        //获取当前时间转化为"yyyy-MM-ss HH-mm-ss"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date = new Date();
        System.out.println(date);
        System.out.println(sdf.format(date));
    }
}


结果:
Fri Mar 17 00:00:00 CST 2023
2023-03-17 00:00:00

Sat Mar 18 09:25:25 CST 2023
2023-03-18 09-25-25

作业1:

需求:用户的注册和登录系统
要求的属性包括:
用户名:6-18位长度,首位必须为字母,所有字符必须为字母和数字,要验重
密码:要求同上
重复密码:
邮箱:必须符合格式xxxxxx@xxxxx.xxxx

生日:格式年-月-日

posted @ 2023-03-17 21:01  DFshmily  阅读(17)  评论(0编辑  收藏  举报