在Java代码中,常常会使用到这样的类的声明实例化:
Person per = new Person();
//这其实是包含了两个步骤,声明和实例化
Person per = null; //声明一个名为Person类的对象引用per
per = new Person(); // 实例化这个per对象
声明 指的是创建类的对象的过程;
实例化 指的是用关键词new来开辟内存空间。
它们在内存中的划分是这样的:
那什么是栈内存(heap)和栈内存(heap)呢?
栈内存:
在函数中定义的一些基本类型的变量和对象的引用变量都在函数的栈内存中分配。栈内存主要存放的是基本类型类型的数据 如( int, short, long, byte, float, double, boolean, char) 和对象句柄。注意:并没有String基本类型、在栈内存的数据的大小及生存周期是必须确定的、其优点是寄存速度快、栈数据可以共享、缺点是数据固定、不够灵活。
字符串常量池:
String str1 = "myString";
String str2 = "myString";
System.out.println(str1 ==str2 );
//注意:这里使用的是str1 ==str2,而不是str1.equals(str2)的方式。
//因为根据JDK的说明,==号只有在两个引用都指向了同一个对象时才返回真值
//而str1.equals(str2),只是比较两个字符串是否相等
结果为True,这就说明了str1和str2其实指向的是同一个值。
上述代码的原理是(这是错的解释),首先在栈中创建一个变量为str1的引用,然后查找栈中是否有myString这个值,如果没找到,就将myString存放进来,然后将str1指向myString。接着处理String str2 = \”myString\”;在创建完str2 的引用变量后,因为在栈中已经有myString这个值,便将str2 直接指向myString。这样,就出现了str1与str2 同时指向myString。
(上面的解释是错的,留着作对照。应该是字符串成常量池)
(正确解释)Java中字符串对象创建有两种形式,一种为字面量形式,如String str = “droid”;,另一种就是使用new这种标准的构造对象的方法,如String str = new String(“droid”);,这两种方式我们在代码编写时都经常使用,尤其是字面量的方式。然而这两种实现其实存在着一些性能和内存占用的差别。这一切都是源于JVM为了减少字符串对象的重复创建,其维护了一个特殊的内存,这段内存被成为字符串常量池或者字符串字面量池。
当代码中出现字面量形式创建字符串对象时,JVM首先会对这个字面量进行检查,如果字符串常量池中存在相同内容的字符串对象的引用,则将这个引用返回,否则新的字符串对象被创建(在堆中创建),然后将这个引用放入字符串常量池,并返回该引用。
字符串常量池存放的是对象引用,不是对象。在Java中,对象都创建在堆内存中。
堆内存:
堆内存用来存放所有new 创建的对象和 数组的数据
String str1 = new String ("myString");
String str2 = "myString";
System.out.println(str1 ==str2 ); //False
String str1 = new String ("myString");
String str2 = new String ("myString");
System.out.println(a==b); //False
创建了两个引用,创建了两个对象。两个引用分别指向不同的两个对象。以上两段代码说明,只要是用new()来新建对象的,都会在堆中创建,而且其字符串是单独存值的,即使与栈中的数据相同,也不会与栈中的数据共享。
jdk1.8关于String intern()方法的描述:
/**
* Returns a canonical representation for the string object.
*
* A pool of strings, initially empty, is maintained privately by the
* class {@code String}.
*
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
*
* It follows that for any two strings {@code s} and {@code t},
* {@code s.intern() == t.intern()} is {@code true}
* if and only if {@code s.equals(t)} is {@code true}.
*
* All literal strings and string-valued constant expressions are
* interned. String literals are defined in section 3.10.5 of the
* The Java™ Language Specification.
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/