Interned strings are those strings whose references are stored in the string pool. We make string interned so that strings with similar values share same string object instead of having different string objects.This saves memory and also increase performance. Interning of strings is described in article What do you undesrtand by String interning?
You may have read in article String equality Check: equals() or == ? that for comparing string contents one should always use equals() instead of ==. This is because == do not compare contents, it compares only references.
But for interned strings you can also use == to compare the contents of strings. This is because, for interned strings, same contents means same String object.
For non interned strings,
Same contents ==> same String objects OR different string objects
Here equals() will give true while == may give true OR false. So you cannot depend on == for equality of contents.
For Example,
String s="Eclipse";
String s1="Eclipse";
String s2= new String("Eclipse");
Here s and s1 are both pointing to same string objects while s2 is a different object.
So,
System.out.println(s.equals(s1)); // will print true i.e same contents
System.out.println(s==s1); // will print TRUE i.e SAME objects
But,
System.out.println(s.equals(s2)); // will print true i.e same contents
System.out.println(s==s2); // will print FALSE i.e DIFFERENT
// objects despite same contents
BUT for Interned Strings,
same contents always ==> same string objects.
Here if equals() gives true means == will also give true.So you depend on == for equality of contents also.
For example,
String s="Eclipse";
String s1= new String("Eclipse");
String s2= s1.intern();
Here s and s1 are two different objects. But due to interning s and s2 are pointing to same String object.
Therefore,
System.out.println(s.equals(s1)); // will print true i.e same
// contents
System.out.println(s==s1); // will print FALSE i.e DIFFERENT
// objects
But,
System.out.println(s.equals(s2)); // will print true i.e same
//contents
System.out.println(s==s2); // will print TRUE i.e always SAME
// objects for same content
Thus after making strings interned one can use == to compare strings for data because then strings with same values always refer to same String objects.
But you should always avoid it because using == for string's data comparision is a bad practice. If you forget to make any string interned and become habitual of using == for comparing string data, this can lead to unexpected results and you will waste your coding time in finding bugs caused by this.
You may have read in article String equality Check: equals() or == ? that for comparing string contents one should always use equals() instead of ==. This is because == do not compare contents, it compares only references.
But for interned strings you can also use == to compare the contents of strings. This is because, for interned strings, same contents means same String object.
For non interned strings,
Same contents ==> same String objects OR different string objects
Here equals() will give true while == may give true OR false. So you cannot depend on == for equality of contents.
For Example,
String s="Eclipse";
String s1="Eclipse";
String s2= new String("Eclipse");
Here s and s1 are both pointing to same string objects while s2 is a different object.
So,
System.out.println(s.equals(s1)); // will print true i.e same contents
System.out.println(s==s1); // will print TRUE i.e SAME objects
But,
System.out.println(s.equals(s2)); // will print true i.e same contents
System.out.println(s==s2); // will print FALSE i.e DIFFERENT
// objects despite same contents
BUT for Interned Strings,
same contents always ==> same string objects.
Here if equals() gives true means == will also give true.So you depend on == for equality of contents also.
For example,
String s="Eclipse";
String s1= new String("Eclipse");
String s2= s1.intern();
Here s and s1 are two different objects. But due to interning s and s2 are pointing to same String object.
Therefore,
System.out.println(s.equals(s1)); // will print true i.e same
// contents
System.out.println(s==s1); // will print FALSE i.e DIFFERENT
// objects
But,
System.out.println(s.equals(s2)); // will print true i.e same
//contents
System.out.println(s==s2); // will print TRUE i.e always SAME
// objects for same content
Thus after making strings interned one can use == to compare strings for data because then strings with same values always refer to same String objects.
But you should always avoid it because using == for string's data comparision is a bad practice. If you forget to make any string interned and become habitual of using == for comparing string data, this can lead to unexpected results and you will waste your coding time in finding bugs caused by this.
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment