Java 字符串对象和存储
以下代码将创建多少个对象,它们将存储在何处Java 字符串对象和存储,java,string,heap-memory,string-pool,Java,String,Heap Memory,String Pool,以下代码将创建多少个对象,它们将存储在何处 String s = "abc"; // line 1 String s1 = new String("abc"); // line 2 String str1 = new String("efg"); //line 3 供参考: 3个对象将被创建,1个在SCP中,2个在HEAP中。您最好猜一下是什么,为什么?搜索创建了多少字符串会产生很多类似的问题。@Deadpool请原谅,但我不说SCP是指“字符串常量池”吗?字符串池中不也会存储“efg”吗?
String s = "abc"; // line 1
String s1 = new String("abc"); // line 2
String str1 = new String("efg"); //line 3
供参考:
3个对象将被创建,1个在SCP中,2个在HEAP中。您最好猜一下是什么,为什么?搜索
创建了多少字符串
会产生很多类似的问题。@Deadpool请原谅,但我不说SCP是指“字符串常量池”吗?字符串池中不也会存储“efg”吗?
Total 3 objects will be created.
Line 1 : Object will be created in string pool,
Line 2 : Object will be created in Heap,
Line 3 : Object will be created in Heap.
Reason is :
a) By string literal : Java String literal is created by using double quotes like in line 1. It is always created in String pool,
b) By new keyword : Java String is created by using a keyword “new” like in line 2 and 3. It is always created in Heap memory.