STRING POOL IN JAVA
来源:互联网 发布:外汇行情看盘软件 编辑:程序博客网 时间:2024/05/16 18:55
STRING POOL IN JAVA
String pool is a pool of strings where created Strings are stored and before creating new String object ,object compiler checks if same string literal is already defined .
As the title says , String pool is pool of Strings .String is special class in java and is immutable . The meaning of String immutable is that we can not change defined String object or simply it is unchangeable .
We can assign string value in two ways .Putting value in double quote directly or using new operator .When a new string value is assigned , compiler checks if it is already in the string pool or not .If it already exists there , it returns the same reference already existed in String pool without creating new String object and if it doesn’t find in String pool , it creates new object , puts in String pool and returns the reference .
Let’s see an example .
String a="hacktangle";String b="hacktangle";String c=new String("hacktangle");
Here, all three variables a , b, c have same value hacktangle . We have assigned value for b same as value of a . So , during compilation , compiler checks in the String pool if any object with same value exists or not . It obviously finds a . So , for b , it returns reference of a without creating new object . This saves a little memory .
When it goes to our third way , we are forcing to create new object c to heap and checks whether it exists or not in String pool .It creates new object and returns the reference .
We can check this by using == operator . Let’s check if objects for these are same or not .
System.out.println("a==b:"+a==b);System.out.println("a==c:"+a==c);
This prints ,
a==b:truea==c:false
This means , a and b are referencing same object and c is different object than a or b . String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String.
Sometimes in java interview, you will be asked question about String pool. For example, how many string is created statement below ;
String string = new String("Hacktangle");
In above statement, either 1 or 2 string will be created. If there is already a string literal “Hacktangle” in the pool, then only one string “string” will be created in the pool. If there is no string literal “Hacktangle” in the pool, then it will be first created in the pool and then in the heap space, so total 2 string objects will be created.
- STRING POOL IN JAVA
- JAVA String Pool
- Java String Pool
- JAVA中的String Pool .
- 【java基础】String Pool
- java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Server/Request
- java String深入constant pool
- java里的String Pool
- 理解Java String和String Pool
- 理解Java String和String Pool
- Java中的String和String pool
- Java String intern() 和 String Pool
- java string pool(java 字符串池)
- error--------Illegal UTF8 string in constant pool in class file
- Java String with Constant pool, heap&stack
- Java String Pool (字符串池)
- String Pool
- String Pool
- dmidecode
- SVN服务器搭建和使用(一)
- 使用Read Committed级别的隔离机制会出现的问题
- mysql中 explain使用详解
- springmvc配置步骤
- STRING POOL IN JAVA
- codefoces214b 整除问题
- struts2与dwr整合(初级)
- Handler源码解析
- 常量池、栈、堆的比较
- SQL性能突然降低引起的业务办理缓慢案例一则
- 设计模式之状态模式
- php实战:html+php+mysql实现留言板功能
- MySQL设置密码