01/10/2018, 11:24
Thắc mắc về kiểu String trong java
String s1 = "java";
String s2 = new String("java");
mình đọc trên stackoverflow thì bảo chí có một đối tượng được tao ra thôi. Nhưng khi
s1 == s2
thì nó in ra là false tức là sao hả mọi người?
Tức là tạo ra hai đối tượng riêng biệt luôn chứ nhỉ
Bài liên quan
When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.
When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool. (http://www.journaldev.com/1321/java-string-interview-questions-and-answers)