Home > Java > javaTutorial > body text

Java String Overview (Part 1)

黄舟
Release: 2017-03-14 11:34:44
Original
1593 people have browsed it



1. Java MemoryModel and ConstantPool

1. Java Memory Model

           Java String Overview (Part 1)

  • Program Counter

     When there are multiple threads, when the number of threads exceeds the number of CPUs or the number of CPU cores, there will be CPU time resources must be grabbed based on time slice polling. Therefore, Each thread must have an independent program counter to record an instruction to be run, which is a thread-private memory area. If the JAVA method is executed, the counter records the java bytecode address being executed. If the native method is executed, the counter is empty.

  • Virtual machine stack

      Thread private, created at the same time as the thread, is a memory model that manages the execution of JAVA methods. The stack mainly stores some basic types of variablesdata (int, short, long, byte, float, double, boolean, char) and objects Quote . When each method is executed, a frame stack will be created to store the method's variable table, operand stack, dynamic link method, return value, return address and other information. The size of the stack determines the reachable depth of method calls (How many levels of recursion, or how many levels of nested calls to other methods, the -Xss parameter can set the virtual machine stack size). The size of the stack can be fixed or dynamically expanded. If the requested stack depth is greater than the maximum available depth, a stackOverflowError is thrown; if the stack is dynamically expandable but there is no memory space to support expansion, an OutofMemoryError is thrown. Use the jclasslib tool to view the structure of the class file. The following picture shows the stack frame structure diagram:

            Java String Overview (Part 1)

  • Local method area

      has similar functions to the virtual machine stack , but what is managed is not a JAVA method, but a local method, and the local method is implemented in C.

  • JAVA heap

     Shared by threads, it stores all object instances and arrays, is Main area for garbage collection. The heap is a runtime data area from which class objects allocate space. These objects are created through instructions such as new, newarray, anewarray and multianewarray. They do not Program code is required to explicitly release it. The heap can be divided into the new generation and the old generation (tenured). The new generation is used to store newly created objects and young objects. If the objects have not been recycled and survive long enough, the old objects will be moved to the old generation. The new generation can be further subdivided into eden (Garden of Eden), survivorSpace0 (s0, from space), and survivorSpace1 (s1, tospace). The newly created objects are put into eden, and both s0 and s1 have gone through GC at least once and survived. If the surviving object still exists after a certain period of time, it enters the old generation (tenured).

                                                                      = by The metadata information of machine-loaded classes: such as constants, Java String Overview (Part 1)static

    variables, and code compiled by the just-in-time compiler, also becomes the permanent generation.
  • If the hotspot virtual machine determines that the definition information of a class will not be used, it will also be recycled. The basic conditions for recycling are at least: all instances of the class are recycled, and the ClassLoader that loads the class is recycled.

    2. Constant pool

     The constant pool is part of the class information, and the class information is reflected in the JVM memory model corresponding to the method area. That is to say, the constant pool is located in the method area. The constant pool mainly stores two major constants: Literal and Symbolic References. Among them, literals mainly include string literals, integer literals and constant values ​​declared as final; while symbol references belong to the compilation principle. The concept includes the following three types of constants:

    • The fully qualified name of the class and interface

    • The name of the field and descriptor

    • Method name and descriptor


    ## 2. Constants and variables

    • We generally refer to

      things whose memory address remains unchanged but whose value can be changed as variables. In other words, the content of the memory is variable under the premise that the memory address remains unchanged. For example:

    • public class String_2 {  
          public static void f(){  
              Human_1 h = new Human_1(1,30);  
              Human_1 h2 = h; 
              System.out.printf("h: %s\n", h.toString());   
              System.out.printf("h2: %s\n\n", h.toString());   
      
              h.id = 3;  
              h.age = 32;  
              System.out.printf("h: %s\n", h.toString());   
              System.out.printf("h2: %s\n\n", h.toString());   
      
              System.out.println( h == h2 );   // true : 引用值不变,即对象内存底子不变,但内容改变
          }
      }
      Copy after login
    • We generally refer to things that

      if the memory address does not change, the value cannot be changed as a constant, A typical String is immutable, so it is called constant (constant). In addition, We can define constants through the final keyword, but strictly speaking, only the basic type is a constant after it is modified (for basic types, its value is immutable, and for object variables Said that its reference can no longer be changed). eg:

    • final int i = 5;
      Copy after login

    3. String definition and basics

    1. String declaration

           

      Java String Overview (Part 1)

        

      From the statement about String in the JDK, we can know:


    2. Description of String in JDK

    The

    String class represents character strings . All string literals (string literals) in Java programs, such as “abc”, are implemented as instances of this class. Strings are constant(constant); their values ​​cannot be changed after they are created. String buffers【StringBuilder OR StringBuffer】 support mutable strings. Because String objects are immutable, they can be shared ( flyweight mode ).


    3. Built-in operations of String class

      The class String includes methods for examining inpidual characters of the sequence for examining inpidual characters of the sequence, for comparing strings , for searching strings , for extracting substrings and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the java.lang.Character class.


    4、字符串串联符号(”+”)以及将其他对象转换为字符串的特殊支持

      The Java language provides special support for the string concatenation operator (+), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(JDK1.5 以后) OR StringBuffer(JDK1.5 以前) class and its append method. String conversions(转化为字符串) are implemented through the method toString, defined by class Object and inherited by all classes in Java.


    注意:

    • String不属于八种基本数据类型,String 的实例是一个对象。因为对象的默认值是null,所以String的默认值也是null;但它又是一种特殊的对象,有其它对象没有的一些特性(String 的不可变性导致其像八种基本类型一样,比如,作为方法参数时,像基本类型的传值效果一样)。 例如,以下代码片段:

    public class StringTest {
    
        public static void changeStr(String str) {
            String s = str;
            str += "welcome";
            System.out.println(s);
        }    public static void main(String[] args) {
            String str = "1234";
            changeStr(str);
            System.out.println(str);
        }
    }/* Output: 
            1234
            1234 
    *///:~
    Copy after login
    • new String() 和 new String(“”)都是声明一个新的空字符串,是空串不是null;


    四. String 的不可变性

    1、什么是不可变对象?

      众所周知,在Java中,String类是不可变类 (基本类型的包装类都是不可改变的) 的典型代表,也是Immutable设计模式的典型应用。String变量一旦初始化后就不能更改,禁止改变对象的状态,从而增加共享对象的坚固性、减少对象访问的错误,同时还避免了在多线程共享时进行同步的需要。那么,到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对象就是不可变的。不能改变状态指的是不能改变对象内的成员变量,包括:

    • 基本数据类型的值不能改变;

    • 引用类型的变量不能指向其他的对象;

    • 引用类型指向的对象的状态也不能改变;

    除此之外,还应具有以下特点:

    • 除了构造函数之外,不应该有其它任何函数(至少是任何public函数)修改任何成员变量;

    • 任何使成员变量获得新值的函数都应该将新的值保存在新的对象中,而保持原来的对象不被修改。


    2、区分引用和对象

      对于Java初学者, 对于String是不可变对象总是存有疑惑。看下面代码:

    String s = "ABCabc";
    System.out.println("s = " + s);    // s = ABCabc
    
    s = "123456";
    System.out.println("s = " + s);    // s = 123456
    Copy after login

      首先创建一个String对象s,然后让s的值为“ABCabc”, 然后又让s的值为“123456”。 从打印结果可以看出,s的值确实改变了。那么怎么还说String对象是不可变的呢? 其实这里存在一个误区: s 只是一个String对象的引用,并不是对象本身。对象在内存中是一块内存区,成员变量越多,这块内存区占的空间越大。引用只是一个 4 字节的数据,里面存放了它所指向的对象的地址,通过这个地址可以访问对象。 也就是说,s只是一个引用,它指向了一个具体的对象,当s=“123456”; 这句代码执行过之后,又创建了一个新的对象“123456”, 而引用s重新指向了这个心的对象,原来的对象“ABCabc”还在内存中存在,并没有改变。内存结构如下图所示:

                      Java String Overview (Part 1)

      Java和C++的一个不同点是,在 Java 中,引用是访问、操纵对象的唯一方式: 我们不可能直接操作对象本身,所有的对象都由一个引用指向,必须通过这个引用才能访问对象本身,包括获取成员变量的值,改变对象的成员变量,调用对象的方法等。而在C++中存在引用,对象和指针三个东西,这三个东西都可以访问对象。其实,Java中的引用和C++中的指针在概念上是相似的,他们都是存放的对象在内存中的地址值,只是在Java中,引用丧失了部分灵活性,比如Java中的引用不能像C++中的指针那样进行加减运算。


    3、为什么String对象是不可变的?

      要理解String的不可变性,首先看一下String类中都有哪些成员变量。 在JDK1.6中,String 的成员变量有以下几个:

    public final class String
        implements java.io.Serializable, Comparable<string>, CharSequence{
        /** The value is used for character storage. */
        private final char value[];    /** The offset is the first index of the storage that is used. */
        private final int offset;    /** The count is the number of characters in the String. */
        private final int count;    /** Cache the hash code for the string */
        private int hash; // Default to 0</string>
    Copy after login

      在JDK1.7中,String类做了一些改动,主要是改变了substring方法执行时的行为,这和本文的主题不相关。JDK1.7中String类的主要成员变量就剩下了两个:

    public final class String
        implements java.io.Serializable, Comparable<string>, CharSequence {
        /** The value is used for character storage. */
        private final char value[];    /** Cache the hash code for the string */
        private int hash; // Default to 0</string>
    Copy after login

      由以上的代码可以看出, 在Java中,String类其实就是对字符数组的封装JDK6中, value是String封装的数组,offset是String在这个value数组中的起始位置,count是String所占的字符的个数。在JDK7中,只有一个value变量,也就是value中的所有字符都是属于String这个对象的。这个改变不影响本文的讨论。 除此之外还有一个hash成员变量,是该String对象的哈希值的缓存,这个成员变量也和本文的讨论无关。在Java中,数组也是对象(可以参考我之前的文章java中数组的特性)。 所以value也只是一个引用,它指向一个真正的数组对象。其实执行了String s = “ABCabc”; 这句代码之后,真正的内存布局应该是这样的:

                     Java String Overview (Part 1)

      value,offset和count这三个变量都是 private 的,并且没有提供setValue,setOffset和setCount等公共方法来修改这些值,所以在String类的外部无法修改String。也就是说一旦初始化就不能修改, 并且在String类的外部不能访问这三个成员。此外,value,offset和count这三个变量都是final的, 也就是说在String类内部,一旦这三个值初始化了, 也不能被改变。所以,可以认为String对象是不可变的了。

      那么在String中,明明存在一些方法,调用他们可以得到改变后的值。这些方法包括substring, replace, replaceAll, toLowerCase等。例如如下代码:

    String a = "ABCabc";
    System.out.println("a = " + a);    // a = ABCabca = a.replace(&#39;A&#39;, &#39;a&#39;);
    System.out.println("a = " + a);    //a = aBCabc
    Copy after login

      那么a的值看似改变了,其实也是同样的误区。再次说明, a只是一个引用, 不是真正的字符串对象,在调用a.replace(‘A’, ‘a’)时, 方法内部创建了一个新的String对象,并把这个心的对象重新赋给了引用a。String中replace方法的源码可以说明问题:

                Java String Overview (Part 1)

      我们可以自己查看其他方法,都是在方法内部重新创建新的String对象,并且返回这个新的对象,原来的对象是不会被改变的。这也是为什么像replace, substring,toLowerCase等方法都存在返回值的原因。也是为什么像下面这样调用不会改变对象的值:

    String ss = "123456";
    System.out.println("ss = " + ss);     // ss = 123456ss.replace(&#39;1&#39;, &#39;0&#39;);
    System.out.println("ss = " + ss);     //ss = 123456
    Copy after login

    4、String对象真的不可变吗?

      从上文可知String的成员变量是 private final 的,也就是初始化之后不可改变。那么在这几个成员中, value比较特殊,因为他是一个引用变量,而不是真正的对象。value是final修饰的,也就是说final不能再指向其他数组对象,那么我能改变value指向的数组吗? 比如,将数组中的某个位置上的字符变为下划线“_”。 至少在我们自己写的普通代码中不能够做到,因为我们根本不能够访问到这个value引用,更不能通过这个引用去修改数组,那么,用什么方式可以访问私有成员呢? 没错,用反射,可以反射出String对象中的value属性, 进而改变通过获得的value引用改变数组的结构。下面是实例代码:

    public static void testReflection() throws Exception {    //创建字符串"Hello World", 并赋给引用s
        String s = "Hello World"; 
    
        System.out.println("s = " + s); //Hello World
    
        //获取String类中的value字段
        Field valueFieldOfString = String.class.getDeclaredField("value");    //改变value属性的访问权限
        valueFieldOfString.setAccessible(true);    //获取s对象上的value属性的值
        char[] value = (char[]) valueFieldOfString.get(s);    //改变value所引用的数组中的第5个字符
        value[5] = &#39;_&#39;;
    
        System.out.println("s = " + s);  //Hello_World}
    Copy after login

      在这个过程中,s始终引用的同一个String对象,但是再反射前后,这个String对象发生了变化, 也就是说,通过反射是可以修改所谓的“不可变”对象的。但是一般我们不这么做。这个反射的实例还可以说明一个问题:如果一个对象,他组合的其他对象的状态是可以改变的,那么这个对象很可能不是不可变对象。例如一个Car对象,它组合了一个Wheel对象,虽然这个Wheel对象声明成了private final 的,但是这个Wheel对象内部的状态可以改变, 那么就不能很好的保证Car对象不可变。


    五. String 对象创建方式

    1. 字面值形式: JVM会自动根据字符串常量池中字符串的实际情况来决定是否创建新对象

    JDK 中明确指出:

    String s = "abc";
    Copy after login

    等价于:

    char data[] = {&#39;a&#39;, &#39;b&#39;, &#39;c&#39;};
    String str = new String(data);
    Copy after login

      该种方式先在栈中创建一个对String类的对象引用变量s,然后去查找 “abc”是否被保存在字符串常量池中。若”abc”已经被保存在字符串常量池中,则在字符串常量池中找到值为”abc”的对象,然后将s 指向这个对象; 否则,中创建char数组 data,然后在中创建一个String对象object,它由 data 数组支持,紧接着这个String对象 object 被存放进字符串常量池,最后将 s 指向这个对象。

    例如:

        private static void test01(){  
        String s0 = "kvill";        // 1
        String s1 = "kvill";        // 2
        String s2 = "kv" + "ill";     // 3
    
        System.out.println(s0 == s1);       // true  
        System.out.println(s0 == s2);       // true  }
    Copy after login

      执行第 1 行代码时,“kvill” 入池并被 s0 指向;执行第 2 行代码时,s1 从常量池查询到” kvill” 对象并直接指向它;所以,s0 和 s1 指向同一对象。 由于 ”kv” 和 ”ill” 都是字符串字面值,所以 s2 在编译期由编译器直接解析为 “kvill”,所以 s2 也是常量池中”kvill”的一个引用。 所以,我们得出 s0==s1==s2;


    2. 通过 new 创建字符串对象 : 一概在堆中创建新对象,无论字符串字面值是否相等

    String s = new String("abc");
    Copy after login

    等价于:

    1、String original = "abc"; 
    2、String s = new String(original);
    Copy after login

      所以,通过 new 操作产生一个字符串(“abc”)时,会先去常量池中查找是否有“abc”对象,如果没有,则创建一个此字符串对象并放入常量池中。然后,在堆中再创建“abc”对象,并返回该对象的地址。所以,对于 String str=new String(“abc”)如果常量池中原来没有”abc”,则会产生两个对象(一个在常量池中,一个在堆中);否则,产生一个对象。
     
      用 new String() 创建的字符串对象位于堆中,而不是常量池中。它们有自己独立的地址空间,例如,

        private static void test02(){  
        String s0 = "kvill";  
        String s1 = new String("kvill");  
        String s2 = "kv" + new String("ill");  
    
        String s = "ill";
        String s3 = "kv" + s;    
    
    
        System.out.println(s0 == s1);       // false  
        System.out.println(s0 == s2);       // false  
        System.out.println(s1 == s2);       // false  
        System.out.println(s0 == s3);       // false  
        System.out.println(s1 == s3);       // false  
        System.out.println(s2 == s3);       // false  }
    Copy after login

      例子中,s0 还是常量池中”kvill”的引用,s1 指向运行时创建的新对象”kvill”,二者指向不同的对象。对于s2,因为后半部分是 new String(“ill”),所以无法在编译期确定,在运行期会 new 一个 StringBuilder 对象, 并由 StringBuilder 的 append 方法连接并调用其 toString 方法返回一个新的 “kvill” 对象。此外,s3 的情形与 s2 一样,均含有编译期无法确定的元素。因此,以上四个 “kvill” 对象互不相同。StringBuilder 的 toString 为:

     public String toString() {    
     return new String(value, 0, count);   // new 的方式创建字符串
        }
    Copy after login

      构造函数 String(String original) 的源码为:

        /**
         * 根据源字符串的底层数组长度与该字符串本身长度是否相等决定是否共用支撑数组
         */
        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);  // 创建新数组并赋给 v
            } 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;
        }
    Copy after login

      由源码可以知道,所创建的对象在大多数情形下会与源字符串 original 共享 char数组 。但是,什么情况下不会共享呢?
      
      Take a look at substring , and you’ll see how this can happen.

      Take for instance String s1 = “Abcd”; String s2 = s1.substring(3). Here s2.size() is 1, but s2.value.length is 4. This is because s1.value is the same as s2.value. This is done of performance reasons (substring is running in O(1), since it doesn’t need to copy the content of the original String).

    String s1 = "Abcd";       
    // s1 的value为Abcd的数组,offset为 0,count为 4String s2 = a.substring(3);      
    // s2 的value也为Abcd的数组,offset为 3,count为 1String c = new String(s2);      
    // s2.value.length 为 4,而 original.count = size = 1, 即 s2.value.length > size 成立
    Copy after login

      Using substring can lead to a memory leak. Say you have a really long String, and you only want to keep a small part of it. If you just use substring, you will actually keep the original string content in memory. Doing String snippet = new String(reallyLongString.substring(x,y)) , prevents you from wasting memory backing a large char array no longer needed.

    详细可见:
    how could ‘originalValue.length > size’ happen in the String constructor?
    String构造器中 originalValue.length > size 发生的情况


The above is the detailed content of Java String Overview (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template