In a Java Bean class:
(I came across this recently in the code-base)
...
public String string( ) {
if (someCondition) {
return new String("someString")
}
return null;
}
In the code above, new String("string") creates two objects:one, for someString itself - JVM handles strings in a unique way, every string literal is a string object, per se and two, for new String(" ") operation - someString can be a field, an instance variable or a hard-coded string value.
It's pointless to return an explicit copy of String instance when none is needed and the case in point is the java bean class which is to be written only for setting and getting properties and nothing else.
(I came across this recently in the code-base)
...
public String string( ) {
if (someCondition) {
return new String("someString")
}
return null;
}
In the code above, new String("string") creates two objects:one, for someString itself - JVM handles strings in a unique way, every string literal is a string object, per se and two, for new String(" ") operation - someString can be a field, an instance variable or a hard-coded string value.
It's pointless to return an explicit copy of String instance when none is needed and the case in point is the java bean class which is to be written only for setting and getting properties and nothing else.
No comments:
Post a Comment