String类

String类

  • String类:代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作 为此类的实例实现。
  • String是一个final类,代表不可变的字符序列。
  • 字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改。
  • String对象的字符内容是存储在一个字符数组value[]中的。
/**
 * @outor shkstart
 * @create 2020-04-07 17:03
 */
public class StringTest {
    /**
     * String:字符串,使用一对""引起来表示。
     * String实现了Serializable接口:表示字符串是支持序列化的。
     *       实现了Comparable接口:表示String可以比较大小。
     * String内部定义了final char[] value用于储存字符串数据。
     * String:代表不可变的字符序列。简称:不可变性。
     *  体现:1.党对字符串重新赋值时,需要重写指定区域赋值,不能使用原有的value进行赋值。
     *       2.当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值不能使用原有的value进行赋值。
     *  通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
     *  字符串常量池中是不会储存相同内容的字符串的。
     */
    public static void main(String[] args) {
        String s1="abc";//字面量的定义方式
        String s2="abc";
        System.out.println(s1 == s2);//代表s1和s2指向一个地址值
        s1="hello";

        System.out.println(s1);
        System.out.println(s2);

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

        String s3="abc";
        s3+="def";
        System.out.println(s3);
        System.out.println(s2);

        System.out.println("***********************");
        String s4="abc";
        String s5=s4.replace('a','m');

        System.out.println(s4);
        System.out.println(s5);
    }
}

true
hello
abc


abcdef
abc


abc
mbc

String对象的创建

String str = “hello”;
//本质上this.value = new char[0]; String s1 = new String();
//this.value = original.value; String s2 = new String(String original);
//this.value = Arrays.copyOf(value, value.length); String s3 = new String(char[] a);
String s4 = new String(char[] a,int startIndex,int count);

import org.junit.Test;

/**
 * @outor shkstart
 * @create 2020-04-07 18:25
 */
public class StringTest2 {
    /*
    String的实例化方式:
    方式一:通过字面量定义的方式
    方式二:通过new+构造器的方式
     */
    @Test
        public void test(){
            String s1 = "javaEE";
            String s2 = "javaEE";

            String s3 = new String("javaEE");
            String s4 = new String("javaEE");

        System.out.println(s1==s2);//true
        System.out.println(s1 == s3);//false
        System.out.println(s1 == s4);//false
        System.out.println(s3 == s4);//false
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值