java in something of type String

  • definition

  Conceptually, a string in java is a sequence of Unicode characters, such as the string "Java \ u2122" is a string of five characters. java no built-in string type, but to provide a predefined class String standard Java class library, each instance of a string of double quotes is a string String class.

  (Note: Java built-in data types, that is, the basic data types, total of eight, byte, short, int, double, long, float, boolean, char fact, JAVA, there are still another basic type void, it. there is also a corresponding wrapper class java.lang.Void, but we can not operate on them directly.)

  Its essence is a char array. And modified by the final keyword. (Private final char value [])

  • characteristic

  1. Can not be inherited

  public final class String

  String is the final category, that means that the String class can not be inherited, and its members are the default method for the final approach. In Java, the final modified class is not allowed to be inherited, and members of the methods in this class are implicitly final approach.

  String class is actually to hold the string by char array.

  2. immutability (can not be changed)

  String class in the source code, we can see this comment:

  

  This shows that the String is immutable, once initialized, it can not be changed.

  private final char value[];首先String类是用final关键字修饰,这说明String不可继承。再看下面,String类的主力成员字段value是个char[ ]数组,而且是用<b>final</b>修饰的。final修饰的字段创建以后就不可改变。有的人以为故事就这样完了,其实没有。因为虽然value是不可变,也只是value这个引用地址不可变。挡不住Array数组是可变的。也就是说Array变量只是stack上的一个引用,数组的本体结构在heap堆。String类里的value用final修饰,只是说stack里的这个叫value的引用地址不可变。没有说堆里array本身数据不可变。看下面这个例子,所以String是不可变,关键是因为SUN公司的工程师,在后面所有String的方法里很小心的没有去动Array里的元素,没有暴露内部成员字段。private final char value[]这一句里,private的私有访问权限的作用都比final大。而且设计师还很小心地把整个String设成final禁止继承,避免被其他人继承后破坏。所以tring是不可变的关键都在底层的实现,而不是一个final。考验的是工程师构造数据类型,封装数据的功力。

  For Java, in addition to the basic types (ie int, long, double, etc.), the rest are objects. For what is immutable, "java concurrency in practice" book gives a roughly defined: When an object is created, its state can not be modified, then the object is immutable. An object satisfies the following three general, may be referred to as immutable object:

  1. Its state can not be created and then modified;

  2. All fields are final type;

  3. During its constructor to construct an object, this reference does not disclose.

  Here highlight what point 2, a target that all fields are final type, the object may be variable objects. Because the final keyword only immutable restriction object reference domain, but not limiting to modify the internal state of the corresponding field by this reference. Therefore, immutable objects in the strict sense, its final keyword domain should also be modified immutable objects and primitive type value.
Technically, immutable objects inside the domain are not necessarily all declared final type, String type that is the case. Within the String object we can see there is a field type named hash is not final, it is because of lazy evaluation of type String hashcode and stored in hash field (this is done by other final field to ensure that each type of calculated hashcode the results must be the same).

  In addition, the immutable String object changes due to all the internal storage structure of type String are new operating a new String object.

 

  java does not provide a method to modify the string, thus can not be amended, Java objects called document type String immutable string, for example, 3 3 is always the same, "hello" is always contains only five characters unit sequence, but you can not modify either one. Only modify string variables, so he quoted another string. StringBuffer string concatenation operator can use the append method.

  Here we should always remember that: "Once created String object is fixed, and any changes to the String object does not affect the original object, any change related operations will generate a new object" .

  Why do this? This will not reduce the efficiency?

  1. string constant pool require

  Only when the strings are immutable, the string pool be possible to achieve. Achieve string pool can save a lot of heap space at run time, because the same string different string variables are pointing to the pool. But if the string is variable, then the String interning will not be achieved (String interning refers to different strings only saves only one that is not saved the same string more), because in that case, if the variable has changed its value, then the value of other points to the value of the variable will change together.

  2. The need of the cache hash

  Because strings are immutable, so when it was created hashcode cached, do not need to be recalculated. This makes it suitable as a string of keys in the Map, the processing speed of the string to be faster than the other key objects. This is often the key in HashMap string.

  HashMap in Java is too important, and it is usually the key of type String. If String is mutable, then later modify the properties, will also change its hashcode. This results not find the original value in the HashMap.

  3. Security Considerations

  • Multi-thread safe

  If String is a variable, namely the String modify the contents of the address change. So when multiple threads simultaneously modify the time, value of length is uncertain, resulting in insecurity, can not get the right result interception. In order to ensure the correct order, and needs synchronzied, but it will be hard to imagine a performance problem.

  • Embodied in the security class loader

  The absolutely most important reason that String is immutable is that it is used by the class loading mechanism, and thus have profound and fundamental security aspects. Had String been mutable, a request to load "java.io.Writer" could have been changed to load "mil.vogoon.DiskErasingWriter"

  String class will be loaded at the time of need, if the String variable, you may modify Loading classes.

  • String constant pool

  Allocation strings and other objects distribution, is a high need to consume time and space, but because the string we use too much, then how to save these resources do, given the java program, is to create a string constant pool , the JVM in order to improve performance and reduce memory overhead when the number of optimization instantiated string: string constant pool . Whenever we create a string constant, JVM will first check the string constant pool, if the string constant pool already exists, then the reference to examples of direct returns the constant pool. If the string constant pool does not exist, examples of the string and place it in the constant pool. Since the immutability of String string quite sure we can certainly two of the same string does not exist in the constant pool (this is crucial to understanding the above).

  Java in the constant pool is actually divided into two forms: static constant pool and runtime constant pool .
  The so-called static constant pool that * .class file constant pool, class file constant pool not only contains the string (digital) literal, also contains the class, method of information, most of the space occupied by the file class.
The runtime constant pool , it is jvm virtual machine loading operation after completion of the class, the class file constant pool is loaded into memory and stored in the method area, we often say that the constant pool, refers to the method area the runtime constant pool.

Guess you like

Origin www.cnblogs.com/HiMay/p/11490503.html