Tuesday, June 26, 2018

Java Scanner Class

Don't use == for equality of string as it compares the objects not the string itself.



Use num.equals("end") or num.equalsIgnoreCase("end") if you want to be able to type end or END



I would not use "end".equals(num), although considered better from a performance perspective in most cases, it does not clearly state the business requirement and it is more important to make it more readable.



But be aware of num being null, if that is possible, num.quals("end") could throw an exception and you should write if (num!=null && num.equals("end")) { ... }




Note that "end".equals(num) does not need the null check, but I still believe this is not very readable, so I would go with if (num!=null && num.equals("end")) { ... }

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...