String

public static void main(String[] args) {
        String str1 = "hello";
        System.out.println(str1);//hello
        String str2 = new String("abcdef");
        System.out.println(str2);//abcdef
        char[] val = {'a','b','c','d','e','f'};
        String str3 = new String(val);
        System.out.println(str3);//abcdef
        String str4 = new String(val,1,4);
        System.out.println(str4);//bcde
    }

字符串常量池:
放在堆里
内容不重复的

public static void main(String[] args) {
        String str1 = "hello";
        String str2 = new String("hello");
        System.out.println(str1 == str2);//false

        String str3 = "hello";
        System.out.println(str1 == str3);//true

        System.out.println(str1.equals(str2));//true
    }

常量 在编译时期已经被处理

public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "hel" + "lo";// 常量 在编译时期已经被处理
//        final int a = 10;
//        int c = a + 30;
        System.out.println(str1 == str2);//true
        String str3 = new String("hel") + "lo";
        System.out.println(str1 == str3);//false
    }
public static void main(String[] args) {
        String str1 = "hello";
        String str2 = str1;
        System.out.println(str1);//hello
        System.out.println(str2);//hello

        str1 = "abc";

        System.out.println(str1);//abc
        System.out.println(str2);//hello
    }
public static void func(String str,char[] ch) {
        str = "abcdef";
        ch[0] = 'g';
    }

    public static void main(String[] args) {
        String str1 = "hello";
        char[] ch1 = {'a'};
        System.out.println(str1);//hello
        System.out.println(Arrays.toString(ch1));//a
        func(str1,ch1);
        System.out.println(str1);//hello
        System.out.println(Arrays.toString(ch1));//g
    }
public static void main(String[] args) {
        String str1 = "hello";
        String str2 = new String("hello");
        System.out.println(str1 == str2);//false
        System.out.println(str1.equals(str2));//true
        String str3 = "hello";
        System.out.println(str1 == str3);//true
        System.out.println(str1.equals(str3));//true
    }
public static void main(String[] args) {
            String str1 = null;
            String str2 = new String("hello");

            //System.out.println(str1.equals(str2));//NullPointerException
            System.out.println(str2.equals(str1));//false

            String str3 = "hello";
            System.out.println(str3.equals("hello"));//true

            System.out.println("hello".equals(str3));//true  方法更好
    }

intern() 手动入池
当前的这个字符串在常量池中是否存在?
存在 把常量池中的引用赋值给当前的引用类型变量
不存在 把字符串在常量池中生成,再把引用赋值给当前的引用类型变量

public static void main(String[] args) {
        String str1 = "hello";
        
        String str2 = new String("hello").intern();
        System.out.println(str1 == str2);//true
    }

拼接会造成大量的临时变量

public static void main(String[] args) {
        String str = "hello";
        str = str + "world";
        str += "!!!";
        System.out.println(str);
        for (int i = 0; i < 10; i++) {
            str += 1;
        }//这样拼接会造成大量的临时变量
    }
public static void main(String[] args) {
        String str = "hello";
        str = "h" + str.substring(1);
        System.out.println(str);//hello  没有改变原来的字符串,而是有生成了一个
    }

利用反射–》自省的过程 反射(通过 反射 能修改 私有的)
1、通过class拿到class对象
2、通过getDeclaredField()拿到你要拿的字段
3、通过setAccessible(true)获得权限
4、通过get()拿到要修改的
5、修改

public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        String str = "hello";
        Class c1 = String.class;//1、通过class拿到class对象(这里是String的class对象)
        //2、通过getDeclaredField()拿到你要拿的字段(这里是String 的 value字段)
        Field field = c1.getDeclaredField("value");
        field.setAccessible(true);//3、通过setAccessible(true)获得权限
        char[] val = (char[]) field.get(str);//4、通过get()拿到要修改的
        val[0] = 'g';//5、修改
        System.out.println(str);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值