• ※基本数据类型的包装类、String类、String增强类StringBuilder和StringBuffer、BigDecimal类、Math类



    基本数据类型的包装类

    基本数据类型是数据数值

    int a = 10;
    
    引用类型是对象引用
    
    String item = "abc";
    
    //http请求中提交的数据[字符串] price = 180 num = 3
    String str_price = "180";  //180
    String str_num = "3";      //3
    
    //可以使用包装类的方法  字符串-->int  【parse方法\valueOf(s)】
    int price = Integr.parseInt(str_price);//
    System.out.println(price);//180
    int num = Integer.parseInt(str_num);
    System.out.println(num);//3
    System.out.println(price*num);//540
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    包装类是基本数据类型的引用类型,提供基本数据类型的数据转换和数据操作的方法。

    其实就两个要记住的别的首字母大写即可 一个是Integer和Character

    –byte Byte

    –short Short

    –int Integer

    –long Long

    –boolean Boolean

    –float Float

    –double Double

    –char Character

    – 十进制转换为二、八、十六进制

    // 实现进制的转换 java中默认是十进制
    System.out.println(" 十进制转为二进制: " + Integer.toBinaryString(15));// 十进制转为二进制: 1111
    System.out.println(" 十进制转为八进制 : " + Integer.toOctalString(15));// 十进制转为八进制: 17
    System.out.println(" 十进制转为十六进制: " + Integer.toHexString(15));// 十进制十六进制: f
    -- 比较 两个数的大小 前比后大的数是1 后比前大是-1 相等是0
    
    System.out.println("比较两个数的大小:" + Integer.compare(10, 10));// 0
    System.out.println("比较两个数的大小:" + Integer.compare(-10, 10));// -1
    System.out.println("比较两个数的大小:" + Integer.compare(20, 10));// 1
    System.out.println("比较两个数的大小:" + Integer.compare(30, 10));// 1
    System.out.println("比较两个数的大小:" + Integer.compare(10, 20));// -1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ※自动类型提升顺序

     --小容量的数据类型可以赋值给大容量的数据类型
    
            基本类型提升的顺序
    
           byte-->short-->int-->long-->float-->double
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    int i = 0;
    double d = i;
    System.out.println(d);//Output result:0.0
               char-->int-->long-->float-->double
    
    16.33F+10//float
    int f = 16.33F+10;//compile error:类型不兼容:float类型无法转换为int类型 小转大不通过
    float f = 16.33F+10;//Output result:26.33 cause:16.33F是float类型,
    //而10是整数类型是java默认的int类型,又因float比int容量大,故java自动提升类型。
    16.33F+10.99//double
    float f = 16.33F+10.99;//compile error:类型不兼容:double类型无法转换为float类型 小转大不通过
    double d = 16.33F+10.99;Output result:27.31 cause:16.33Ffloat类型,
    //而10.99是整数类型是java默认的double类型,又因double比float容量大,故java自动提升类型。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    --强制类型转换
    
    • 1

    在这里插入图片描述

    –大容量的数据类型赋值给小容量的数据类型会有类型兼容问题,不能直接赋值,除非通过强制类型转换

            语法格式:
    
            类型1  变量a =  (类型1) 变量b
    
            类型1  变量a =  (类型1) (表达式)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    /**
      *
      * @Author Lantzrung
      * @Date 2022年7月16日下午4:44:42
      *  
      **/
    byte item1 = 2;//102
    byte item2 = 3;//103
    byte item3 = item1+item2;
    //编译错误:不兼容的类型: 从int转换到byte可能会有损失 [原因:两数相加的和是大于127]
    solution:byte item4 = (byte)(item1+item3)
    System.out.println("item4="+item4);//结果:-51    
    //205-128-->77-->-127+77-->-50 因为0不用算故此 要向前移动一位 -50-1-->-51
    //(int强转byte类型容量不足,溢出,故从byte类型的最小值取值开始)
    
    byte b = 182;//编译错误: 不兼容的类型: 从int转换到byte可能会有损失
     
    solution:byte b = (byte)182;结果:-74
    //(int强转byte类型容量不足,溢出,故从byte类型的最小值取值开始)   
    byte b1 = 2;//程序能输出2  
        
    short s = 45182;//编译错误:不兼容的类型:从int转换到short可能会有损失
    solution: short s1 = (short)45182;//结果:-20354 和上述同理        
    int i = 21000000000;//编译错误:过大的整数:21000000000 210亿 int最大的整数值为21亿左右
    long l = 21000000000;//错误:过大的整数:21000000000
    solution:long l1 = 21000000000L;//在过大数值中加上L的字母就可以把int类型数组提升为long类 
    //型数组
    System.out.println(l1);//210000000000
    
    int item1 = 67;
    byte item2 = item1;//编译error:不兼容的类型:从int转换到byte可能会有损失    
    solution:byte item2 = (byte)item1;//67 强转
    short item3 = item1;//编译error:不兼容的类型:从int转换到short可能会有损失
    solution:short item3 = (short)item1;//67 强转 
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    基本数据类型 包装类【自动装箱、自动拆箱】

    Integer priceInt = price;// 自动拆箱:基本数据类型:基本数据类型 封装为包装类
    int priceItem = priceInt;// 自动拆箱:包装类转换为基本数据类型
    int item = priceInt + 3;
    System.out.println(item);// 6
    
    • 1
    • 2
    • 3
    • 4

    –实现基本数据类型和引用类型之间的转换

    –自动装箱 将基本数据类型的数据封装为基本数据类型的包装类

    –自动拆箱 将基本数据类型的包装类拆封为基本数据类型的数据

    – 数值格式化异常

    double d = Double.parseDouble("12.33");
    System.out.println(d);
    		
    // 需要在parseDouble中加上" "
    // double d1 = Double.parseDouble(12.33);//String类型的包装类无法转换为double浮点数类型
    
    // Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    // The method parseDouble(String) in the type Double is not applicable for the
    // arguments (double)
    
    
    //System.out.println(d1);
    
    Double.parseDouble("12.33");// 数值格式化异常:java.lang.NumberFormatException
    // Double.parseDouble("12.33b");//数值格式化异常:java.lang.NumberFormatException
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    BigDecimal

    类是一个算术运算类,它可以给浮点类型的运算控制算术精度,但是要注意在创建对象是入参字符串类型的数据

    记得导包 java.math.BigDecimal

     import java.math.BigDecimal;
    
    /**
     * @author Lantzrung
     * @date 2022年7月18日
     * @Description
     */
    
    public class BigDecimalDemo {
    
    	public static void main(String[] args) {
                   double item1 = 12.58794654;
                    float item2 = 3.582145f;
    		// 9.00580154
    		// 9.00580657112793
    		System.out.println(item1 - item2);
    
    		// 使用BigDecimal 创建对象时要注意提供字符串
    		// BigDecimal b1 = new BigDecimal(item1);
    		// BigDecimal b2 = new BigDecimal(item2);
    		BigDecimal b1 = new BigDecimal("12.58794654");
    		// BigDecimal b2 = new BigDecimal("3.582145f");//float类型不兼容double 
            // java.lang.NumberFormatException
    		BigDecimal b2 = new BigDecimal("3.582145");// 9.00580154
      
    		// 算术
    		BigDecimal res1 = b1.subtract(b2);// 减法操作
    		System.out.println(res1.doubleValue());
    		BigDecimal res2 = b1.add(b2);// 加法操作
    		System.out.println(res2.doubleValue());
    		BigDecimal res3 = b1.multiply(b2);// 乘法操作
    		System.out.println(res3.doubleValue());
    		BigDecimal res4 = b1.divide(b2);// 除法操作
    		System.out.println(res4.doubleValue());
                    //除法操作一定要注意被除数的小数点不能太多,要是太多就用下列方法进行转换
                    /*
      		         * public static double div(double v1,double v2){
                     * BigDecimal b1 = BigDecimal.valueOf(v1); 
                     * BigDecimal b2 = BigDecimal.valueOf(v2);
                     * returnb1.divide
                     * (b2,DEE_DIV_SCALE,BigDecimal.ROUND_HALF_UP).doubleValue(); 
                     * }
    		         */
            int item1 = 1223;//hasCode返回值为:37913
    		double item2 = 1223;//hasCode返回值为:37913
    		double item3 = 1223.69;//hasCode返回值为:37913
    
    		// BigDecimal b1 = new BigDecimal(item1);
    		// BigDecimal b1 = new BigDecimal("1233");//hasCode返回值为:38223
    		// BigDecimal b1 = new BigDecimal(1223);//hasCode返回值为:37913
    		BigDecimal b3 = new BigDecimal(item2);// hasCode返回值为:37913
    
    		// b1.hashCode();//无法直接调用输出结果
    		// System.out.println("hasCode返回值为:"+b1.hashCode());
    		// System.out.println("hasCode返回值为:" + b2.hashCode());
    		 System.out.println("hasCode返回值为:" + b3.hashCode());
    
    		//System.out.println(BigDecimal.hasCode());//错误:输出的方式
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    //加法的操作

    System.out.println(“BigDecimal -> 0.05+0.01=”+(f1.add(f2)));

    //减法法的操作

    System.out.println(“BigDecimal -> 0.05-0.01=”+(f1.subtract(f2)));

    //乘法的操作

    System.out.println(“BigDecimal -> 0.05*0.01=”+(f1.multiply(f2)));

    //除法的操作

    System.out.println(“BigDecimal -> 0.05/0.01=”+(f1.divide(f2)));

    ​Math

    –abs(a):返回 int 值的绝对值。

    System.out.println(Math.abs(-10));//10

    –max(a,b):返回两个 int 值中较大的一个。

    Math.max(10, 50);// 没有输出结果,无法进行比较max System.out.println(Math.max(10, 50));// 50

    –min(a,b):返回两个 int 值中较小的一个。

    Math.min(-10, 15);// 没有输出结果,无法进行比较min System.out.println(Math.min(-10, 15));// -10

    –random() :返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。

    //生成10~30的随机数,包含10但不包含30
    int num = (int)(Math.random()*(30-10)+10);
    System.out.println("10~30随机数:"+num);
    //生成1~100的随机数,不包含0但包含100
    int num1 = (int)(Math.random()100+1);System.out.println(num1);//随机生成一个六位数long round = Math.round(Math.random()(999999 - 100000) + 100000);
    System.out.println("六位数为:"+round);
    --max(a,b):返回两个int值中较大的一个
    System.out.println(max);
    --sqrt(a) :返回正确舍入的 double 值的正平方根。
    
    System.out.println(Math.sqrt(-50));//NaN 为负数故此不输出结果 
    System.out.println(Math.sqrt(25));//5.0 25的平方为5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    NaN表示非数值,例如:0.0/0结果为NAN,负数的平方根结果也为NAN

    Date、SimpleDateFormat和Calendar日历以及练习

    Date、SimpleDateFormat

    日期类型:java.util.Date

    获取当前日期的两种方式:

    –Date date = new Date();

    –long item = System.currentTimeMillis(); Date date = new Date(item);

    日期格式化SimpleDateFormat

    –创建SimpleDateFormat的对象:

    SimpleDateFormat sm = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

     --使用:Date 格式化输出  String字符串
    
    • 1

    String item = sm.format(new Date());

     --String类型表示日期  转换为  java.util.Date;
    
    • 1
    /**
     * @author Lantzrung
     * @date 2022年7月15日
     * @Description
     */
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    // 记住不要导错包 不要导错了import java.sql.Date;
    
    public class DateDemo {
        public static void main(String[] args) throws ParseException {
    	// 1、获取当前的系统时间
    	Date date = new Date();// new 一个实例化对象
    	System.out.println(date);// Fri Jul 15 21:52:37 CST 2022 
    	// 2、通过时间戳来创建一个时间对象 时间戳-->1970年1月1如到现在的总毫秒数
    	// 方式一
    	long item = System.currentTimeMillis();
    	Date date2 = new Date(item);
    	System.out.println(date2);// Fri Jul 15 21:52:37 CST 2022
    	// 方式二
    	Date date1 = new Date(System.currentTimeMillis());
    	System.out.println(date1);// Fri Jul 15 21:52:37 CST 2022
    
    	// 3、时间格式化【本地化】 2022-07-15 22:12:58
    	// Date转换为字符串
    	// 格式化 yyyy-MM-dd HH:mm:ss 24小时制
    	SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String time = sm.format(date);
    	System.out.println(time);// 2022-07--15 22:12:58
    
    	// 格式化yyyy-MM-dd hh:mm:ss 12小时制
    	SimpleDateFormat sm1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	String time1 = sm1.format(date);// 2022-07-15 10:12:58 //先将用一个String 数组time1定 
        // 义 ,再将date代入sm1.format()中
      	System.out.println(time1);// 11:12:58
    
    	// 格式化 HH:mm:ss 24小时制
    	SimpleDateFormat sm2 = new SimpleDateFormat("HH:mm:ss");
    	String time2 = sm2.format(date);
    	System.out.println(time2);// 22:12:58
    
    	// 字符串转换为Date
    	// 需要抛出异常 throws ParseException
    	Date item1 = sm1.parse("2021-07-15 22:12:58");// 12小时制:2021-07-15 22:12:58
    	// 定义的格式话那里 需要注意细节 不然会出现报错
    	// Exception in thread "main" java.text.ParseException: Unparseable date:
    	// "2021-07-15 22:12:58"
    	// at java.text.DateFormat.parse(Unknown Source)
    	System.out.println(item1);// Thu Jul 15 22:12:58 CST 2021
    	System.out.println(sm1.format(item1));// 2021-07-15 10:12:58
    
    	Date dItem = sm.parse("2021-07-15 22:12:58");
    	// 需要抛出异常 throws ParseException
    	System.out.println(dItem);// Thu Jul 15 22:12:58 CST 2021
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    Calendar

    Calendar是日历类型 比Date类型(1996年)能更好的处理日期和时间

    类是一个抽象类,不能new对象

    星期一 Mon 星期二 Tues 星期三Wed 星期四Thur 星期五 Fri 星期六 Sat 星期天 Sun

    英语里面月份的缩写:

    一月份Jan 二月份Feb 三月份Mar 四月份Apr 五月份May 六月份June 七月份July 八月份Aug 九月份Sept 十月份Oct 十一月份Nov 十二月份Dec

    // Calendar是日历类型,更加具体,强大 比
    // 1、如何获取Calendar的对象 Calendar是抽象类,不能new对象 
    //--获取Calendar类的实例对象:
    Calendar calendar = Calendar.getInstance();//设置实例对象
    
    // getTime 获取当前date时间  从CST到CDT的夏令时更改
    System.out.println(calendar.getTime());// Thu Jul 14 12:31:34 CST 2022
    
    // 根据字段来获取具体的时间
    System.out.println(calendar.get(Calendar.YEAR)); // 注意:年份是2022 2022 年 year
    System.out.println(calendar.get(Calendar.MONTH));// 注意:月份是0~11 而不是1~12 6  月 month
    System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 注意:一个月的第几天  14 天 day 
    System.out.println(calendar.get(Calendar.DAY_OF_WEEK));// 注意:一周中的第几天 5 天 day
    System.out.println(calendar.get(Calendar.SECOND)); //读取秒数 40s
    
    // 根据字段来设置具体的时间 【set】 第一个参数为字段 第二个参数为数值
    calendar.set(Calendar.MONTH, 15);// 设置月份
    calendar.set(Calendar.DAY_OF_MONTH, 23);// 设置一个月的第几天
    System.out.println(calendar.getTime());// 输出时间
    
    //使用默认时区和语言环境获得一个日历。-- 设置当前时间:
    //重新获取当前电脑的时间 时间戳    Thu Jul 14 13:52:46 CST 2022
    //calendar.setTimeInMillis(System.currentTimeMillis());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    练习:获取2020年和2021年的母亲节的日期,tips,5月份的第二个星期日,要求:输出时间格式为yyyy年MM月dd日

    /**
     * @author Lantzrung
     * @date 2022年7月17日
     * @Description
     */
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    public class Demo2 {
    	public static void main(String[] args) {
    		        //获取2020年和2021年的母亲节的日期,tips,5月份的第二个星期日,要求:输出时 
                    //间格式为yyyy年MM月dd日
    				//1、如何获取Calendar的对象Calendar是抽象类,不能new对象
    				Calendar cd = Calendar.getInstance();//获取实例化对象
    				// 根据字段来设置具体的时间【set】第一个参数为字段 第二个参数为数值
    				cd.set(Calendar.YEAR, 2021);//设置年
    				cd.set(Calendar.MONTH, 4);//设置月 注意:这里月份是有0~11 所以5月份是要设置 
                    //4的数值value
    				cd.set(Calendar.DAY_OF_WEEK_IN_MONTH, 2);//这里输出的这个月的第几周的 
    				cd.set(Calendar.DAY_OF_WEEK, 1);//这里是从星期天输出的一周
    				
    				SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM--dd");
    				//Sun May 09 00:33:34 CST 2021
    				System.out.println(cd.getTime());//获取当前Calendar日历所要设置的时间
    			
    				cd.set(Calendar.YEAR, 2020);//设置年
    				cd.set(Calendar.MONTH, 4);//设置月 注意:这里月份是有0~11 所以5月份是要设置 
                    //4的数值value
    				cd.set(Calendar.DAY_OF_WEEK_IN_MONTH, 2);//这里输出的这个月的第几周的 
    				cd.set(Calendar.DAY_OF_WEEK, 1);//这里是从星期天输出的一周
    			
    				SimpleDateFormat sm1 = new SimpleDateFormat("yyyy-MM--dd");
    				//Sun May 10 00:33:34 CST 2020
    				System.out.println(cd.getTime());//获取当前Calendar日历所要设置的时间
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    ※String是字符串类型

    是不可变类,并且是一个引用类型。
    –创建String类型的对象 :
    String item = “hello”;
    或者
    String item = new String(“hello”);

    –字符串的对象比较

    – 返回指定索引处的 char 值:
    charAt(int index) ;

    –返回指定字符在此字符串中第一次出现处的索引:
    indexOf(int ch) ;

    – 返回指定字符在此字符串中最后一次出现处的索引:
    lastIndexOf(int ch) ;

    –返回此字符串的长度:
    length() ;

    –返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的:
    replace(char oldChar, char newChar);

    –测试此字符串是否以指定的前缀开始。
    startsWith(String prefix);

    –测试此字符串是否包含某个字符串。
    contains(String str)

    –返回一个新的字符串,它是此字符串的一个子字符串:
    substring(int beginIndex);

    –将此字符串转换为一个新的字符数组:
    toCharArray();

    –使用默认语言环境的规则将此 String 中的所有字符都转换为小写:
    toLowerCase();

    –使用默认语言环境的规则将此 String 中的所有字符都转换为大写:
    toUpperCase();

    –根据给定正则表达式的匹配拆分此字符串:
    split(String regex);

    String增强类:StringBuilder和StringBuffer

    • 1.获取
    • char charAt(index)
    • indexOf
    • String substring(start,end)…
    • 2.删除
    • StringBuffer delete(begin,end) 包含头,不包含尾
    • StringBuffer deleteCharAt(index)
    • 3.存储
    • StringBuffer append() 追加未尾
    • StringBuffer insert()
    • 4.修改
    • replace()
      1. 反转
    • reverse()
    /**
      * @author Lantzrung
      * @date 2022年7月17日
      * @Description
      */
    public class Demo {
    	public static void main(String[] args) {
    		//字符串的增强类
    		StringBuffer sbf = new StringBuffer("it is girl");
    		//  * 1.获取
    		//  *   char
    		//  *   charAt(index)
    		//  *   indexOf
    		//  *   String substring(start,end)....
    		System.out.println(sbf.charAt(0));//i 获取当前定义数值 在该字符串的字符位置
    		System.out.println(sbf.indexOf("s"));//4 获取当前定义字符 在该字符串的位置
    		System.out.println(sbf.substring(0, 5));//it is 获取 截取 定义开始的字符串
    
    		// 定义的字符串末尾 然后截取该定义的东西
    		//  * 2.删除
    		//  *   StringBuffer delete(begin,end) 删除一段字符,所要截取的字符串   包含头,不 
            //包含尾
    		sbf = sbf.delete(0, 3);// 这里是删除了it加上一个空格 定义了0是i 1是t 2是一个空格 3 
            //不计算
    		System.out.println(sbf.delete(0, 3));//直接输出删除后的结果
    		System.out.println(sbf);//it girl
    		//  *   StringBuffer deleteCharAt(index)//删除该索引的单个字符
    		sbf = sbf.deleteCharAt(2);//删除了定义的字符串中的第一个空格
    
    		//  * 3.存储
    		//  *   StringBuffer append() 追加未尾
    		sbf.append("!");// 在该字符串末尾添加! 
    		System.out.println(sbf);//it is my girl!
    		//  *   StringBuffer insert() (offset,str) (该字符串的索引位置,"需要在字符串添加的 
            //内容")
    		sbf.insert(6, "my ");// 向字符串第6位,添加"my ";位置。
    		System.out.println(sbf);// it is my girl!
    
    		//  * 4.修改
    		//  *   replace()
    		sbf = sbf.replace(0, 2, "IT");// 修改定义字符串的0到2,原本为it,修改为IT
    		System.out.println(sbf);
    
    		//  * 5. 反转
    		//  *   reverse()//把数组翻转过来
    		sbf.reverse();//!lrig ym si TI
    		System.out.println(sbf);
    
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    -String增强类

    1、字符串增强类的使用

    ※ String增强类

    StringBuilder是线程不安全的String增强类,StringBuilder 和 StringBuffer都是容器, 定义的api(方法)都相同

    StringBuffer是线程安全的String增强类。StringBuffer 是一个容器,称为字符串缓冲区,该字符串缓冲区可扩展final类

    特点: 长度可变化; 可以操作多个数据类型;通过toString()转成String

           如果对字符串进行修改时,无需创建新对象!性能相对String较好
    
    • 1

    ※ StringBuilder 和 StringBuffer区别:

     StringBuilder没有线程安全(机制),而 StringBuffer有线程安全的问题
    
     所以,StringBuilder性能较佳
    
     StringBuilder版本较新
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、字符串增强类的安全测试

    总结:三种类型的区别以及使用场景

    对于普通的操作,操作量不大时使用String

    对于大量的更新操作,使用字符串的增强类

    –单线程:StringBuilder

    –多线程:StringBuffer

    String
    
    String使用的时长:2795毫秒
    
    StringBuilder使用的时长:2毫秒
    
    StringBuffer使用的时长:5毫秒
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    /**
     * @author Lantzrung
     * @date 2022年7月17日
     * @Description
     */
    public class Demo1 {
    	public static void main(String[] args) {
    		// StringBuilder是线程不安全的 StringBuffer是线程安全的
    
    		// 测试String、StringBuilder和StringBuffer数据操作的性能
    		/*
    		 * String str = "abc"; long start = System.currentTimeMillis(); for (int i = 1;
    		 * i < 50000; i++) { str = str + "abc"; } long end = System.currentTimeMillis();
    		 * System.out.println("String使用的时长:" + (end - start) + "毫秒");
    		 */
    		// 线程不安全的
    		StringBuilder strBuilder = new StringBuilder("abc");
    		long start1 = System.currentTimeMillis();
    		for (int i = 1; i < 50000; i++) {
    			strBuilder.append("abc");
    		}
    		long end1 = System.currentTimeMillis();
    		System.out.println("StringBuilder使用的时长:" + (end1 - start1) + "毫秒");
    		System.out.println("---------------------");
    		// 线程安全的
    		StringBuffer strBuffer = new StringBuffer("abc");
    		long start2 = System.currentTimeMillis();
    		for (int i = 1; i < 50000; i++) {
    			strBuffer.append("abc");
    		}
    		long end2 = System.currentTimeMillis();
    		System.out.println("StringBuffer使用的时长:" + (end2 - start1) + "毫秒");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    练习:使用StringBuffer 往一个数值每隔三位(千位)插入一个’,’

    例如: "88123456789"-->"88,123,456,789"  
    
           12,345  
    
    • 1
    • 2
    • 3
    
    
    /**
     * @author Lantzrung
     * @date 2022年7月17日
     * @Description
     */
    public class Demo2 {
    	public static void main(String[] args) {
    		//创建string
    		StringBuilder sbf = new StringBuilder("88123456789");
    		// 从低位开始,每隔3位进行插入
    		for(int i =sbf.length()-3;i>0;i-=3){
    		    sbf=sbf.insert(i,",");
    		}
    		System.out.println(sbf);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    自己动手写Docker学习笔记
    setTimeout 、setInterval、requestAnimationFrame
    NCMMSC 2021丨长短视频多语种多模态识别挑战赛
    【python】flask中如何向https服务器传输信息
    Unix环境高级编程-学习-02-进程环境之进程终止、命令行参数、环境表、C程序的存储空间布局
    如何获取第三方maven依赖信息?
    docker简单快速使用上手
    3D建模吃香到底是真是假? 优漫动游
    java programer future plan
    Spring IoC容器简介说明(BeanFactory和ApplicationContext)
  • 原文地址:https://blog.csdn.net/Lantzrung/article/details/126092995