Tuesday 26 June 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...