difference between string literal and string object
来源:互联网 发布:剑三叶英捏脸数据 编辑:程序博客网 时间:2024/05/14 07:10
new String("text");
explicitly creates a new and referentially distinct instance of a String
object;
String s = "text";
may reuse an instance from the string constant pool if one is available.
You very rarely would ever want to use the new String(anotherString)
constructor.
From the API:
String(String original)
: Initializes a newly createdString
object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since strings are immutable.
Whenever you call
new
in JAVA it create an object in heap and obviously it will call the constructor also.String literals will go into
It might help you to understand it visually to remember it for longer time.String Constant Pool
.
Whenever you call
new
in JAVA it create an object in heap and obviously it will call the constructor also.String literals will go into
String Constant Pool
.It might help you to understand it visually to remember it for longer time.
Object creation line by line:
String str1 = new String("java5");
- Using string literal "java5" in the constructor, an new string value is stored in string constant pool
- Using new operator a new string object is created in the heap with "java5" as value
String str2 = "java5
- Reference
str2
is pointed to already stored value in string constant pool
String str3 = new String(str2);
- A new string object is created in the heap with the same value as reference by
str2
String str4 = "java5";
- Reference
str4
is pointed to already stored value in string constant poolTotal objects : Heap[堆] - 2, Constant Pool[常量池] - 1
Whenever you call
new
in JAVA it create an object in heap and obviously it will call the constructor also.String literals will go into
String Constant Pool
.
It might help you to understand it visually to remember it for longer time.
Object creation line by line:
String str1 = new String("java5");
- Using string literal "java5" in the constructor, an new string value is stored in string constant pool
- Using new operator a new string object is created in the heap with "java5" as value
String str2 = "java5
- Reference
str2
is pointed to already stored value in string constant pool
- Reference
String str3 = new String(str2);
- A new string object is created in the heap with the same value as reference by
str2
- A new string object is created in the heap with the same value as reference by
String str4 = "java5";
- Reference
str4
is pointed to already stored value in string constant pool
- Reference
Total objects : Heap - 2, Pool - 1
- difference between string literal and string object
- difference between string and string.h
- String object and String literal are different
- [Java]Difference between String StringBuffer and StringBuilder
- Difference Between String , StringBuilder And StringBuffer Classes
- String and some of the difference between StringBuffer
- Difference between string created by array and pointer
- Difference Between String , StringBuilder And StringBuffer Classes With Example : Java
- what is difference between equals() and contentEquals() of String class?
- Difference between Object.keys() and Object.getOwnPropertyNames()
- Difference between char *str=“STRING” and char str[] = “STRING”
- what's the difference between NULL 0 '\0' and ""/''(space string or char) ?
- C++ String: What is the difference between '\n' and '\r\n'?
- conversion between UTF8 and string
- Conversion Between BitString and String
- The difference between (()) and ().
- difference between "<" and "<<"
- fckeditor unterminated string literal
- Service使用——不同应用程序间的绑定与信息传递
- 每日一问之软键盘的弹出和隐藏
- Linux下I2C驱动分析(一)
- 支持首字母的左滑删除listview
- 浅谈算法和数据结构: 九 平衡查找树之红黑树
- difference between string literal and string object
- 欢迎使用CSDN-markdown编辑器
- getaddrinfo简单应用——取得IP地址
- 动画
- 浅谈算法和数据结构: 十 平衡查找树之B树
- hibernate一级缓存(session缓存)
- js插件+UploadFile类实现图片的批量上传
- mybatis3.1分页自动添加总数
- java泛型与object的比较