String s1 = new StringBuilder(” Go “).append(” od “).toString();
System.out.println(s1.intern() == s1); String s2 = new
StringBuilder(” ja “).append(” va “).toString(); String s2 = new
StringBuilder(” ja “).append(” va “).toString();
System.out.println(s2.intern() == s2); Output is true or false
in the search on the net, have explained string constants in the pool a Java this string constants, and so the example below would be false.
String sb =new StringBuilder(” Go “).append(” OD “).toString();
String sb =new StringBuilder(” Go “).append(” OD “).toString();
String s1 = new String(” Good “);
System.out.println(sb.intern()==sb);
returns false!!
originally used in intern method, will first judgment in the constant pool have the corresponding string, have put the string, not words
The intern() implementation in JDK 1.7 does not copy the instance any more, it just records the instance reference that first appears in the constant pool, so the reference returned by the intern() is the same as the instance of the string created by the StringBuilder
Read More:
- The differences between the equals method in the string class and the equals method in the object class
- Mybatis error,There is no getter for property named ‘xx’ in ‘class java.lang.String The solution
- Method of ignoring case in Python string comparison
- Solve the problem that power view in Excel prompts activate method of oleobject class failed or activate method like oleobject is invalid
- In Java, int is converted to string, and zero is added before the number of bits is insufficient
- How to get the current time in java time string
- A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.
- C++ foundation — clear/erase/pop of string class_back
- [Solved] Flowable Start Error: ClassCastException: java.time.LocalDateTime cannot be cast to java.lang.String
- java.lang.NoSuchMethodError: org.json.JSONObject.put(Java/lang/String; Java/util/Collection;) “209151;”
- The solution to the problem that the method of interacting with database in the servlet class is invalid after the servlet submits 404, 500 refresh and becomes 404, and after connecting to the data pool
- Java encrypts the string with MD5
- Difference between isempty method and isblank method in stringutils
- Java compareto() method
- There is no getter for property named ‘id‘ in ‘class java.lang.Integer‘
- How to generate main () method in java graphical interface
- Problem solving of failed to read candidate component class in Java
- Java String.split () special character processing
- Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- Power view error: invalid activate method of class oleobject