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对象 然后在创建一个字符串对象存储在常量池中,这是堆中存储的是常量池中的地址    常量池中的地址才是字符串真正存储的地址。

     内存图

  • 相关阅读:
    cocos2dx 3.4 截图代码
    cocos2dx android平台事件系统解析
    cocos2dx3.4 保存json文件
    cocos2dx3.4 解析json文件
    cocos2dx3.4 分割plist图片
    cocos2dx3.4 导出节点树到XML文件
    win7系统cocos2dx 3.4 绑定自定义类到Lua
    cocos2dx 3.3创建新项目 和 VS2012解决方案加载失败问题
    浅谈端口扫描
    PHP 通过.user.ini 绕过黑名单限制
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/String.html
Copyright © 2011-2022 走看看