Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Avinash G.A
OCP Java SE 6 Programmer, OCP Java EE 5 Web Component Developer, OCE Java EE 6 Web Services Developer, VMware Certified Core Spring 3.x Developer, EMC Proven Professional (ISM-V2)
Avinash G.A
OCP Java SE 6 Programmer, OCP Java EE 5 Web Component Developer, OCE Java EE 6 Web Services Developer, VMware Certified Core Spring 3.x Developer, EMC Proven Professional (ISM-V2)
Avinash Ga wrote:yes matthew, one more thing for the above code, if we do
then you wont get a null pointer. instead the result will be false.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Avinash G.A
OCP Java SE 6 Programmer, OCP Java EE 5 Web Component Developer, OCE Java EE 6 Web Services Developer, VMware Certified Core Spring 3.x Developer, EMC Proven Professional (ISM-V2)
Rob Spoor wrote:That's a matter of opinion. I don't mind that way at all, and prefer it to having to first check if str is not null - a check that is performed again within the equals method you're calling (only then for the String literal "str").
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Dennis Deems wrote:It's irrelevant that String.equals checks the argument for null, since it doesn't share its findings with us.
The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Jeff Verdegan wrote:
Dennis Deems wrote:It's irrelevant that String.equals checks the argument for null, since it doesn't share its findings with us.
No, it's not irrelevant, and it does share its findings. From the javadocs for String's equals() method:
The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
In other words, String shares with us the fact that someString.equals(null) gives false.
If null is a condition we care about, then absolutely we need to check for it.
Dennis Deems wrote:
Jeff Verdegan wrote:
Dennis Deems wrote:It's irrelevant that String.equals checks the argument for null, since it doesn't share its findings with us.
No, it's not irrelevant, and it does share its findings. From the javadocs for String's equals() method:
The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
In other words, String shares with us the fact that someString.equals(null) gives false.
That's not sharing its findings. That's silently returning the value it deems appropriate. False is also given for most non-null values.
We have no way of knowing if the argument is null unless we examine it.
Perhaps in your enthusiasm to hit the "reply" button you ignored the first sentence in my post:
If null is a condition we care about, then absolutely we need to check for it.
seemed like you were talking in the general case, and not just when we care about null.It's irrelevant that String.equals checks the argument for null, since it doesn't share its findings with us.
just sounded like you were looking for an argument, so I thought I'd be kind and oblige.If we don't care, then sure throw readability under the bus of expedience. Just don't ask me to debug it.
Consider Paul's rocket mass heater. |