第八章 字符串与包装类

一、字符串及拼接


public class StringTest01 {
    public static void main(String[] args) {
        //String是字符串类,也是引用类型
        //String位于Java.lang包,这个包下的类使用的时候都不需要导包
        //创建字符串对象的方式有两种
        //1.引用字符串常量给字符串变量赋值
        String s1 = "Hello";
        //2.通过构造方法创建对象
        String  s2 = new String();
        char[] c = {'h','e','l','l','o'};
        //将字符数组中的数据拼接在一起作为字符串进行创建对象
        String s3 = new String(c);
        //字符数组索引为2开始,长度为2的字符拼接在一起作为字符串创建对像
        String s4 = new String(c,2,2);
        String s5 = new String("hello");
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        System.out.println(s5);
    }

}

二、String打印

public class StringTest02 {
    public static void main(String[] args) {
        String s1 = new String("hello");
        Student s2 = new Student();
        //String有自己的toString方法,所以打印的不是地址
        System.out.println(s1);
        System.out.println(s2);
    }

}

三、字符串的判断

public static void main(String[] args) {
        String s1 = "hello";//1
        String s2 = new String();//1
        String s3 = new String("hello world");//2
        String s4 = new String("hel1o");//2
        String s5 ="hello world";//0
        //两个 String s6 = "abcdef";
        //  String s7 = "qwerdf";
        //编译器进行优化,会将字符串拼接成一个完整的字符串,所以是两个字符串对象
        String s6 = "a"+"b"+"c"+"d"+"e"+"f";
        String s7 = "q"+"w"+"e"+"r"+"d"+"f";
        System.out.println(s6);
    }

四、字符串的查找

public class StringTest04 {
    public static void main(String[] args) {
        String s = " hello world everyone ";
        System.out.println("字符串的长度为:" + s.length());
        //字符串的索引也是从0开始的
        //indexOf为正向查找,从左到右查
        System.out.println("查找字符串的所在位置" + s.indexOf("l"));
        //lastIndexOf反向查找,从右到左查,索引还是从左开始
        System.out.println("查找字符串的所在位置" + s.lastIndexOf("l"));
        //charAt获取指定索引位置的字符,返回值是char类型
        System.out.println(s.charAt(2));
        //开始位置的索引表示的值要截取,结束位置表示的值不截取
        System.out.println("截取子字符串" + s.substring(6,8));
        System.out.println("去除字符串前后空格" + s.trim());
        //替换:第一个参数是旧字符串中要被替换的字符串,第二个参数是替换的新字符串
        System.out.println("替换" + s.replace(" ",""));
    }

}

五、equals


public class StringTest05 {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        System.out.println(s1.equals(s2));
        System.out.println(s1 == s2);
 String s3 = "hello";
        String s4 = new String("hello");
        System.out.println(s3.equals(s4));
        System.out.println(s3 == s4);;
    }

}

六、包装类

public class Wrapper {
    public static void main(String[] args) {
        //用基本数据类型给包装类型赋值就是装箱,而且是自动装箱
        Integer i1 = 20;
        //因为包装类是对象类型,所以也是可以使用new创建对象
        Integer i2 = new Integer(50);
        //用包装类类型给基本数据类型赋值就是拆箱,而且是自动拆箱
        int a = i2;
        System.out.println(i1);
        System.out.println(a);
        //将包装类转换为字符串
        String s = i1.toString();
        System.out.println(s);
        s = "123";
        //将字符串转换为基本数据类型,使用的是parseXXX方法,调用这个方法的是你要转为
        // 什么基本数据类型的包装类去掉用
        int b = Integer.parseInt(s);
        double c = Double.parseDouble(s);
        System.out.println(b);
    }

}

七、StringBuffer

public class StringBufferTest {
    public static void main(String[] args) {
        //StringBuffer叫做字符串缓冲区,也叫字符串生成器
        //底层是一个字符数组
        StringBuffer sb1 = new StringBuffer();//默认长度为16
        StringBuffer sb2 = new StringBuffer(20);//可以自我认定长度
        StringBuffer sb3 = new StringBuffer("hello");//字符串长度+16
        //StringBuffer跟String一样都重写了toString方法
        System.out.println(sb3);
        //StringBuffer进行字符串拼接的时候不能使用+进行字符串拼接
        // sb3 = sb3 + "world";
        //StringBuffer使用append方法进行追加字符串
        sb3.append("world");
        System.out.println(sb3);
        System.out.println("hello".toString());
        System.out.println(sb3.toString());

    }

}

八、必考题

1.为什么普通引用类型打印的是地址,而字符串对象打印的是内容

答:因为普通引用类型的对象默认调用的是Object的toString方法,所以默认打印的出来的就是 完整类路径+@+十六进制的地址值,而String类型有自己的toString方法,会将字符串转换成 字符串数组,然后在字符串数组打印的控制台上,所以输出的是字符串内容

2.一共创建了多少个String对象

答:字符串为了减少内存将字符串常量保存在字符串常量池中,而字符串常量池1.6之前 位于方法区中,但是到了1.8之后位于堆区中,如果常量池中有字符串常量,则使用的时候,直接引用,而如果字符串常量不存在则会创建新的字符串常量,但是不管常量池中如何变化,只要有new调用构造方法,就会在堆区中创建一个字符串。所以考虑创建多少个字符串同时考虑常量池和构造方法创建的堆区对象。

3.String的equals方法的实现原理

答:String会将字符串转换为字符数组,然后再进行按位逐一比较,但是为了提高比较效率会先进行三重判断,第一重判断比较两个对象的地址是否相同,如果相同则返回true,表示两个对象是同一个对象,如果不同则进行第二重判断,判断当前传递过来的对象是否是String类型创建的对象,如果不是则返回false,如果是则进行第三重判断,比较两个字符串的长度是否相同,如果不相同,则返回false,如果相同在进行字符数组的逐一比较,当全部相同参会返回为true,所以String的equals方法比较的是两个对象的内容是否相同而不是地址。

4.包装类作用就是:

(1)解决基本数据类型跟包装类类型进行互转

(2)当不能使用基本数据类型的时候,可以使用包装类类型例如集合

5.请写出十个你知道的位于java.lang包下的类

答:八种基本数据类型的包装类、String、StringBuffer、Object、System等

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值