有关字符串的讲解

本文详细介绍了Java中String类的构造方法,包括无参构造、字节数组构造、字符数组构造等,并通过示例展示了如何使用这些构造方法创建字符串。同时,文章提到了字符串的不可变性以及`toString()`方法的重写。此外,还讨论了字符串常量与变量相加的区别,以及`==`与`.equals()`在比较字符串时的不同行为。最后,指出字符串拼接时的内存管理和效率问题。
摘要由CSDN通过智能技术生成

字符串:简单理解j就是由多个字符组成的数据,叫做字符串,当然,我们也可以将字符串堪称一个字符数组。根据帮助文档API可以发现,String代表的是字符串,属于java.lang包下面的,所以使用的时候不需要导包,String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。(对象);字符串不变; 它们的值在创建后不能被更改 字符串是常量,一旦被赋值,就不能改变

字符串的构造方法:

public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)

注意了!!!:String类重写了toString()的方法

接下来我们将用上述的构造方法来构造字符串

public class StringDemo {
    public static void main(String[] args) {
        //public String()
        String s = new String();
        System.out.println("s: " + s);//s:
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s.length());//字符串s的长度为:0

        System.out.println("***************************************");

        //public String(byte[] bytes) 将一个字节数组转成一个字符串
        byte[] b = {97,98,99,100,101};
        String s2 = new String(b);
        System.out.println("s2: " + s2);//s2: abcde   字符自动转化为ASCII码值

        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s2.length());// 字符串s的长度为:5

        System.out.println("***************************************");
        //public String(byte[] bytes,int index,int length)
        //将字节数组中的一部分截取出来变成一个字符串
        String s3 = new String(b, 1, 3);
        System.out.println("s3: " + s3); //s3: bcd
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s3.length()); //字符串s的长度为:3

        System.out.println("***************************************");
        //StringIndexOutOfBoundsException  越界异常
//        String s4 = new String(b, 1, 5);
//        System.out.println("s4: " + s4);
//        //查看字符串的长度
//        //public int length()返回此字符串的长度。
//        System.out.println("字符串s的长度为:" + s4.length());

        System.out.println("***************************************");
        //public String(char[] value) 将一个字符数组转成一个字符串
        char[] c = {'a','b','c','d','我','是','小','混','子'};
        String s5 = new String(c);
        System.out.println("s5: " + s5);//s5: abcd我是小混子
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s5.length());//字符串s的长度为:9

        System.out.println("***************************************");
        //public String(char[] value,int offset,int count)
        //将字符数组中一部分截取出来变成一个字符串
        String s6 = new String(c, 4, 5);//从第四位开始截取五个字符
        System.out.println("s6: " + s6);//s6: 我是小混子
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s6.length());//字符串s的长度为:5

        System.out.println("***************************************");

        //public String(String original)
        String s7 = "你好";
        String s8 = new String(s7);
        System.out.println("s8: " + s8);
        //查看字符串的长度
        //public int length()返回此字符串的长度。
        System.out.println("字符串s的长度为:" + s8.length());
    }
}

以上就是常用的字符串的构造方法,接下来的是字符串的一些注意点

1、字符串是常量,它的值在创造过后不可以更改,例如:⬇

/*
    字符串是常量,它的值在创建之后不能更改
    String s = “hello”;
    s += “world”;
    问s的结果是多少?

 */
public class StringDemo2 {
    public static void main(String[] args) {
        String s = "hello";
        s += "world";
        System.out.println(s);

    }
}

当然,如你所想,这个输出的结果是helloworld

2、String s = new String(“hello”)和String s = “hello”;的区别;

/*
    字符串比较之看程序写结果
    字符串拼接之看程序写结果

    1、==比较引用数据类型的时候,比较的是地址值
    2、String s1 = new String("hello");会在堆内存中创建对象
    3、String类中重写了Object的equals方法
    4、equals方法默认比较的是地址值,但是由于重写了,所以比较的是内容

 */
public class StringDemo3 {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = "hello";
        System.out.println(s1 == s2);//false
        System.out.println(s1.equals(s2)); //true
    }
}

        3、字符串如果是变量相加,是先开辟空间然后再拼接哦

        字符串如果是常量相加,是先相加,然后去常量池中找,如果找到了就返回,如果找不到就创建一个新的

public class StringDemo5 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        String s3 = "helloworld";
        String s4 = "hello"+"world";
        System.out.println(s3==s4); //true
        String s5 = s1+s2;
        System.out.println(s3==s1+s2); //false
        System.out.println(s3.equals(s1+s2)); //true

        //System下
        //public static int identityHashCode(Object x)
        System.out.println(System.identityHashCode(s3));//1163157884
        System.out.println(System.identityHashCode(s4));//1163157884
        System.out.println(System.identityHashCode(s5));//1956725890
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

被迫内卷的学习记录

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

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

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

打赏作者

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

抵扣说明:

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

余额充值