Reputation: 95
I was going through API code i found this in JAVA Api
java.io.FilePermission.getMask(String actions) {
int mask = NONE;
// Null action valid?
if (actions == null) {
return mask;
}
// Check against use of constants (used heavily within the JDK)
if (actions == SecurityConstants.FILE_READ_ACTION) {
return READ;
} else if (actions == SecurityConstants.FILE_WRITE_ACTION) {
return WRITE;
} else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) {
return EXECUTE;
} else if (actions == SecurityConstants.FILE_DELETE_ACTION) {
return DELETE;
} else if (actions == SecurityConstants.FILE_READLINK_ACTION) {
return READLINK;
}
....
....
.....
}
can any one tell me why they have used '==' operator instead of .equlas() method. :?
Upvotes: 1
Views: 198
Reputation: 921
In the JVM string constant pool essentially cache all string literals.Therefore the same object will remain in the memorry. SO JVM avoid creating a new string object each time a literal is declared, which could get very expensive very quickly. Because of this still we can use ==.
Normally == is used to compare two object references. for the string comparison better to sue the equals() method. but when it deals with constant String you can still use == . but it should be done only if you sure it uses for constants.
Upvotes: 0
Reputation: 34166
It is because of String interning. What happens is that the compiler interns String literals in memory at compile time (this is done to save memory). When you compare string literals with ==
, it will work because they are in the same memory location.
I would recommend you to read this answer, so you get the idea. Here is the example (from that answer) and its explanation (mine):
1 // These two have the same value
2 new String("test").equals("test") --> true
3
4 // ... but they are not the same object
5 new String("test") == "test" --> false
6
7 // ... neither are these
8 new String("test") == new String("test") --> false
9
10 // ... but these are because literals are interned by
11 // the compiler and thus refer to the same object
12 "test" == "test" --> true
13
14 // concatenation of string literals happens at compile time,
15 // also resulting in the same object
16 "test" == "te" + "st" --> true
17
18 // but .substring() is invoked at runtime, generating distinct objects
19 "test" == "!test".substring(1) --> false
Explanation:
Why does line 12 and 16 evaluate to true? Because of
String
interning. What happens in these cases is that the compiler internsString
literals in memory at compile time (this is done to save memory). In line 12 and 16 theString
literals that are beign compared are in the same memory location, so the comparison with==
operator returnstrue
. However, this is not recommended unless you want better performance (comparing memory addresses is a lot “cheaper” than a loop) and you are sure that theString
s being compared are interned literals.
Remember that those SecurityConstants
are strings.
Upvotes: 4
Reputation: 40356
This works as long as actions
is 100% guaranteed to reference one of those constant instances. Note that Java pools string literals so it also works when comparing to the literal "something".
class Example {
static final String VALUE1 = "something";
static final String VALUE2 = "another";
void example () {
String v;
v = VALUE1; assert(v == VALUE1);
v = VALUE2; assert(v == VALUE2);
v = "something"; assert(v == VALUE1);
v = new String("something"); assert(v != VALUE1); // <- not equal!
}
}
Upvotes: 0
Reputation: 32468
That action
should be one of those constants declared in SecurityConstants
class, which already instantiated and passed to that method. So it doesn't matter whether we compare with equals()
or ==
.
Upvotes: 2