包装类与String类型

  • 日考

    已知有类:
    class Worker{
    	String name;
    	int age;
    	double salary;
    }
    // 请提供toString与equals方法
    

    演示的代码如下:

package com.txw.test;

import java.util.Objects;

public class Worker {
    String name;
    int age;
    double salary;

    @Override
    public String toString() {
        return "Worker{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Worker worker = (Worker) o;
        return age == worker.age &&
                Double.compare(worker.salary, salary) == 0 &&
                Objects.equals(name, worker.name);
    }
}

1包装类

  • 概念: 基本数据类型所提供的对象形式。
  • 特点:
    1. Object 可以统一所有数据类型
    2. 可以区分null与默认值0/0.0
    3. 内部提供了一些工具方法便于使用
    4. 内部支持缓存(缓存区大小 1B -128~127)
    
    由于包装类的优势很明显,设计类时所有的属性都要使用包装类类型(编程习惯)。
1.1 类型匹配(重点)
  • 基本数据类型与其对应的包装类型
    基本数据类型包装类
    byteByte
    shortShort
    intInteger
    longLong
    floatFloat
    doubleDouble
    booleanBoolean
    charCharacter
1.2 类型转换方法
  1. 基本数据类型转换为包装类。
    方法1:valueOf( )。
    byte b = 10;	// 基本数据类型
    Byte bo = Byte.valueOf( b );	// 将基本数据类型转换为包装类
    
    int a = 20;·	// 基本数据类型
    Integer io = Integer.valueOf( a );	// 将基本数据类型转换为包装类
    double d = 30D;	// 基本数据类型
    Double od = Double.valueOf( d );	// 将基本数据类型转换为包装类
    
    方法2:构造方法。
    byte b = 10;		// 基本数据类型
    Byte bo2 = new Byte( b ); 			// 将基本数据类型转换为包装类
    int a = 20;			// 基本数据类型
    Integer io2 = new Integer( a ); ·	// 将基本数据类型转换为包装类
    double d = 30D;		// 基本数据类型
    Double od2 = new Double( d );		// 将基本数据类型转换为包装类
    
    推荐使用valueOf方法,可以利用缓存。
     Integer ia = new Integer( 10 );
     Integer ib = new Integer( 10 );
     System.out.println( ia == ib );	// false 没有利用缓存区
     Integer ic = Integer.valueOf( 10 );
     Integer id = Integer.valueOf( 10 );
     System.out.println( ic == id );	// true 都是从缓存区获取的对象
    
  2. 包装类型转换基本数据类型。
    方法:xxValue( )。
    Integer io = Integer.valueOf( 10);
    int i = io.intValue();			// 由包装类转换为基本数据类型
    Double od = Double.valueOf( 30D );
    double d = od.doubleValue();	// 由包装类转换为基本数据类型
    Byte bo = Byte.valueOf( (byte)10 );
    byte b = bo.byteValue();		// 由包装类转换为基本数据类型
    
1.3 自动封箱、自动拆箱
  • 自动封箱,自动将数据封装为包装类类型。
    编译器自动将基本数据类型转换为包装类型。
    演示的代码如下:
    int a = 10;
    Integer io  = Integer.valueOf( a );
    
    Integer io2 = 10;	// 自动调用Integer.valueOf( 10 );
    
  • 自动拆箱,自动将包装类类型转换为基本数据类型。
    演示的代码如下:
    Integer io = Integer.valueOf( 10 );
    int a = io.intValue();	// 将包装类转换为基本数据类型
    int a2 = io;	// 自动调用 intValue() 自动拆箱,将包装类型转换为基本数据类型
    System.out.println(io -  10);	// 需要计算时,自动拆箱
    
  • 优点:编码时不需要手动转换,使用基本数据类型与使用包装类类型,无差别。
    int a = 10;
    Integer io = 10;
    System.out.println( a - 5);
    System.out.println( io - 5 );
    
1.4 字符串转换为包装类型
  • 方式1:valueOf。
    Integer a = Integer.valueOf( "123" );
    
  • 方式2:构造方法。
    Integer a2 = new Integer("123");
    
    演示的代码如下:
package com.txw.test;

public class TestWarpperClass {
    public static void main(String[] args) {
        Integer a = Integer.valueOf( "123" );
        Integer a2 = new Integer("123");
        System.out.println( a2 );
        int a3 = Integer.parseInt("123");
        System.out.println( a3 );
        /*int a = 10;
        Integer io  = Integer.valueOf( a );
        Integer io2 = 10;	// 自动调用Integer.valueOf( 10 );*/
     /*   int a = 10;
        Integer io = 10;
        System.out.println( a - 5);
        System.out.println( io - 5 );
*/
        // Integer io = Integer.valueOf( 10 );
        // int a = io.intValue();	// 将包装类转换为基本数据类型
        // int a2 = io;		// 自动调用 intValue() 自动拆箱,将包装类型转换为基本数据类型
        // System.out.println(io -  10);
        // Scanner sc = new Scanner(System.in);
        // int age =  sc.nextInt();
        // Integer age2 = sc.nextInt();
      /* Integer io = Integer.valueOf( 10);
        int i = io.intValue();		// 由包装类转换为基本数据类型
        Scanner sc = new Scanner(System.in);
        int age =  sc.nextInt();
        Double od = Double.valueOf( 30D );
        double d = od.doubleValue();	// 由包装类转换为基本数据类型
        Byte bo = Byte.valueOf( (byte)10 );
        byte b = bo.byteValue();	// 由包装类转换为基本数据类型
		*/
        /*   Integer ia = new Integer( 10 );
        Integer ib = new Integer( 10 );
        System.out.println( ia == ib );

        Integer ic = Integer.valueOf( 10 );
        Integer id = Integer.valueOf( 10 );
        System.out.println( ic == id );
*/
      /*  byte b = 10;		// 基本数据类型
        Byte bo = Byte.valueOf( b );		// 将基本数据类型转换为包装类
        Byte bo2 = new Byte( b );    // 将基本数据类型转换为包装类
        int a = 20;			// 基本数据类型
        Integer io = Integer.valueOf( a );		// 将基本数据类型转换为包装类
        Integer io2 = new Integer( a );   // 将基本数据类型转换为包装类

        double d = 30D;		// 基本数据类型
        Double od = Double.valueOf( d );// 将基本数据类型转换为包装类
        Double od2 = new Double( d );
        System.out.println(d);
        System.out.println(od);
        System.out.println(od2);*/
    }
}

2 String类

  • 概念:String是Java字符串的一种实现方式。
2.1 创建字符串对象
  1. 使用""直接创建。
    String str = "ABC";
    
  2. 使用构造方法创建。
    String str = new String("ABC");
    
    创建字符串对象时,String将每一个字符保存到内部的char[]数组中。
2.2 常用方法
  • 比如:

    方法名作用
    char[] toCharArray()返回字符串中的char[]
    boolean equalsIgnoreCase(String str)忽略大小写验证是否相同
    int indexOf( String str)返回字符串第一次出现的位置
    int indexOf(String str , int fromIndex)从fromIndex向后查找str返回第一次出现的位置
    int lastIndexOf( String str)返回str最后一次出现的位置
    int lengt()返回字符串的长度(字符个数)
    String toUpperCase(String str)将str转换为大写并返回新的字符串
    String toLowerCase(String str)将str转换为小写并返回新的字符串
    String trim()去掉字符串左右两边的空格
    String replace(char a,char b)将字符串中的字符a替换为字符b
    String replace(String a,String b)将字符串串中的a,替换为b
    String[] split(String regex)根据regex分割字符串
    String substring(int begin,int end)截取字符串从begin开始到end-1结束
2.3 String类的特点(了解)
  • 特点:
    1. 字符串字面值("")存储在一个特殊的空间里(JDK6持久带,JDK7后存储在元空间)
    持久带或元空间里的数据不会被销毁,主要存储的数据都具备一定的复用性
    2. String类中的数据是不可变的,如果需要改变则需要重新创建新的String对象
    3. String类为final类,则不能被继承
    4. new String("")会产生两个字符串对象,一个在堆空间由引用指向,一个在元空间存储字面值
    
2.4 可变字符串
  1. StringBuilder 速度快,线程不安全 1.5 产物。
    特点:
    拼接字符串时使用 append()方法进行拼接。
    使用toString方法将StringBuilder转换为String。
    
    演示的代码如下:
    StringBuilder sb = new StringBuilder();		// String str = "";
    sb.append("A");		// str += "A"
    sb.append("B");		// str += "B"
    // 将StringBuilder转换为字符串
    String str = sb.toString();
    
    由于StringBuilder拼接字符串效率很高,所以在Java中如果出现String字符串拼接,编译器会自动创建StringBuilder完成拼接动作。
    演示的代码如下:
    String str = "ABC";
    //字符串拼接 自动new StringBuiolder().append(str).append("A").toString();
    str+="A";
    
    2.StringBuffer(不用)速度慢,线程安全 1.0 产物。
    演示的代码如下:
package com.txw.test;

public class TestString {
    public static void main(String[] args) {
        // String str = "ABC";		// final char[] value = {'A','B','C'}
       // String str2 = "D";		// final char[] value = {'D'}
        // str += str2;		// 产生新的字符串对象:final char[] value={'A','B','C','D'}
      // System.out.println( str );
        long t1 = System.currentTimeMillis();	// 获取当前时间的毫秒
        // String str = "";
        StringBuilder sb = new StringBuilder();
        for( int i=1; i<=100000; i++){
            // str+=i;
            sb.append(i);		// 字符串拼接 等于str+=i
        }
        long t2 = System.currentTimeMillis();	// 获取当前时间的毫秒
        System.out.println( t2 - t1 );
        // String str = new String("");
       // String str = "阿森/18/30.5";
       // 以 / 为分隔符,拆分成三部分
       // String[] strs = str.split("/");
       // 打印分割结果
       // strs[0] 阿森
       // strs[1] 18
       // strs[2] 30.5
      // System.out.println( Arrays.toString(strs) );
    }
}

3 总结加粗样式在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学无止路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值