Possible duplicate:The difference between these two conditions?
I'm doing some code cleanup, and NetBeans made a suggestion to change
if(!billAddress1.equals("")) to if (!"".equals(billAddress1)) .
if(!billAddress1.equals(""))
if (!"".equals(billAddress1))
What is the difference between the two and the advantages of using the proposed version over the readability of the original version?
billAddress1.equals("") will throw a NullPointerException if billAddress1 is null , "".equals(billAddress1) wont.
billAddress1.equals("")
billAddress1
null
"".equals(billAddress1)
// Could cause a NullPointerException if billAddress1 is null if(!billAddress1.equals("")) // Will not cause a NullPointerException if billAddress1 is null if (!"".equals(billAddress1))
!"".equals(billAddress1) will never call NPE , so it allows for a more compact syntax, eliminating billAddress1 == null , which otherwise would be required.
!"".equals(billAddress1)
NPE
billAddress1 == null
The latter will not throw a Null pointer exception if the value is null.
One saves you from NPE, as others have pointed out. But if you are sure that it will not be zero, the best way to check if a string is empty is to use the String.isEmpty() method, which seems to be trying to make the code.
String.isEmpty()
The first method may raise a NullPointerException.
Source: https://habr.com/ru/post/1447322/More articles:Git "ends left" when switching back to a branch from a disconnected head - gitClosing ProgressDialog onClick - androidHow to get input scheme in exec function in Pig UDF - apache-pigContext action bar overflows - androidValgrind invalid write / read errors, no solution found in other issues - cHow to create a delay before ajax call? - javascriptAdding a radio group to a user dialog - androidOpenGL: GL_TEXTURE_CUBE_MAP and GL_REFLECTION_MAP - c ++Generating instrument data using a Python module - pythonTwitter Bootstrap Preferred way to align content with space - cssAll Articles