栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

有关字符串的讲解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有关字符串的讲解

字符串:简单理解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、字符串是常量,它的值在创造过后不可以更改,例如:⬇

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”;的区别;

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
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号