== compares the addresses of objects / lines / nothing
.equals() , designed to use the internal state of objects for comparison.
So:
new Object() == new Object() => false - two separate objects at different addresses in memory.
new String("a") == new String("a") => false - the same situation - two separate addresses for string objects.
new String("a").equals(new String("a")) => true - the addresses are different, but Java accepts one state of the object ('a') and compares it with another state of the object ('a'), it will consider their equal and will report the truth.
Using the equals () method, you can encode the comparison in any way suitable for your program.
intern() is a slightly different story. It is intended to return the same object (address) for the same char sequence. It is useful to reduce the amount of memory required when creating multiple lines.
new String("aaa").intern() will look in the computer's memory if someone created the string "aaa" before and returns the first instance of String ... If it was not found, the current one will be credited as the first and on and on "aaa ".intern () and new String("aaa").intern() and ("a"+"aa").intern() will return this" first "instance.
Beware: "aaa".intern() does not work very fast, and if you put all the lines, you will save some memory, but lose a lot of work with the processor.
ya_pulser
source share