If you compare two Strings in Java, normally you would use the equals method to compare them to see if they are similar. This will be some common use case for input validations. However, if you don't do it properly it may cause you to throw a NullPointerException. This is pretty much like common sense, but I've seen even more experienced developers make this mistake. Let's consider the following code. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class DemoApplication { final static String theStringIknow = "Hello" ; public static void myTestMethod ( String someString ) { //do not do this if ( someString . equals ( theStringIknow )) { System . out . println ( "Same same" ); } } public static void main ( String [] args ) { String testString = "Hello" ; myTestMethod ( testString ); } } In myTestMethod, I'm checking if the String in the a...
Just another tech blog, also my personal blog. I'll be posting interesting articles about tech, programming and tricks, dev life, and any timely topics.