CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

RE: Java - Backwards - Second Draft


[Replies] [Reply] [View by Thread] [Help]
[Back To SNEAK Development Forum]

Posted by Ret. Gen D-Cypell On 2005-02-26 19:13:56
In Reply to RE: Java - Backwards - Second Draft Posted by Tr flamebalrog On 2005-02-26 02:15:52

Ret. Gen
Ret. Gen D-Cypell



Its not the fact that it is quicker that makes "literal".equals(str) a smart thing to do.

The real reason is that it protects against null pointer exceptions. If str is null...

str.equals("literal") will throw an NPE because you cant call .equals on null. If you put the literal first it obviously can never be null.

In this case it doesnt matter too much because null is checked for anyway.

On 2005-02-26 02:15:52, flamebalrog wrote
>On 2005-02-24 20:18:01, SAJChurchey wrote
>>

>>public static String Backwards(String str){

>>      if(str == null || str.trim().equals(""))

>>        return "";

>>      str = str.trim();

>>      StringBuffer newString = new StringBuffer(str);

>>      return newString.reverse().toString();

>>}

>>

>
>A not-null-and-not-empty-and-not-space-only String (one that will be reversed) is trimmed twice with your code (1st to test it, 2nd after the test). You could avoid it.
>
>Also, I think ' "literalString".equals(expression) ' is faster than ' expression.equals("literalString") '...can anyone confirm ?


Replies:


Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.02289 seconds


About Us | Privacy Policy | Mission Statement | Help