zoukankan      html  css  js  c++  java
  • String 简介

    特性:

              1.字符串的不可改变性

                            String  s="a"+"b"+"c";

                                       创建了四个对象

                                                在方法常量池中存储   a“,”b“,”c“,三个对象

                                                 最后会吧三个拼接到一起在创建一个对象   存储"abc"的对象,a,b,c的对象会被gc在空闲地时候回收

                            体现了字符穿的不可改变型

               2.字符串的底层代码还是字符数组

    public String(String original) {
    int size = original.count;
    char[] originalValue = original.value;
    char[] v;
    if (originalValue.length > size) {
    // The array representing the String is bigger than the new
    // String itself. Perhaps this constructor is being called
    // in order to trim the baggage, so make a copy of the array.
    int off = original.offset;
    v = Arrays.copyOfRange(originalValue, off, off+size);
    } else {
    // The array representing the String is the same
    // size as the String, so no point in making a copy.
    v = originalValue;
    }
    this.offset = 0;
    this.count = size;
    this.value = v;
    }

    两种创建方式

             直接创建   eg:String str="abcd";

             new创建    String s=new String();

    存储的位置

              直接存储   字符串会直接存储在方法区中的常量池中

             new 创建     会创建两个对象   首先会创建一个String对象 然后在创建一个字符串对象存储在常量池中,这是堆中存储的是常量池中的地址    常量池中的地址才是字符串真正存储的地址。

     内存图

  • 相关阅读:
    python sort dict 总结
    深度学习网络压缩模型方法总结(model compression)
    串口通讯(中)——USART
    串口通讯(上)——基础概念
    关于通讯的一点常识
    SysTick定时器的一个简单应用
    RCC时钟配置实践
    GPIO输入——按键检测
    stm32中断初识与实践(下)
    stm32中断初识与实践(上)
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/String.html
Copyright © 2011-2022 走看看