String str = new String("abc");
Wow
String str = "abc";
What's the difference?
Writing a String string refers to that string. However, the new String ("~~") is to create a new string object.
For example,
String a = "abc";
String b = "abc";
System.out.println(a == b); // true
where a and b strings are the same object.
String c = new String("abc");
String d = new String("abc");
System.out.println(c == d); // false
where c and d refer to different objects. If possible, we recommend that you use the String string. It's easier to read and gives the compiler the opportunity to optimize code.
First of all, we need to understand Heap and the String constant pool that exists in it.
String a = new String("abc"); The above sentence creates a general object in heap and then has a ref of the object.
String b = "abc"; I understand that the above sentence is stored in the String constant pool in the heap, or if it is a string that already exists, it points to the index number of the stored array.
So, the result for a == b is false. Because it's not referring to the same object.
But a.This is true because equals(b) is the result of comparing the string itself.
578 Understanding How to Configure Google API Key
571 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
910 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
609 GDB gets version error when attempting to debug with the Presense SDK (IDE)
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.