We all know that string is the fundamental part of programming, every bit is as important as numbers. In java there is a string methos intern().
So what exactly is intern()? Well, as we know that in java there tow different ways of compare objects. You can use == or equals() method. The == operator compares the references of both objects, whereas equals() compares the data of both objects. So as part in first lesson we have learned that we should use equals() method, not == to compare string objects.
new String("Hello")==new String("Hello") will in fact return false because they are two different string objects. If we use equals() then it will return true. But equals() method can be fairly slow as it compares character-by-character.
Since the == compares identity, i.e. all it has to do is compare tow pointers to see if they are same or not. So it will be much faster then equals() method.
No comments:
Post a Comment