Java method to judge whether a string is null
Today, I wrote code and found my code: if (! Username. equals ("")&&! Userpassword. isEmpty ()&&! PhString. isEmpty ()&&! Userpassword2.isEmpty () as above: if there is nothing in the phString here, then phString. the isEmpty () method returns a null pointer exception when phString = "51cto";, phString. the return value of isEmpty () is false. That is to say, the return value of this method is boolean. If length () is 0, true is returned. Otherwise, false is returned. however, this method is not compatible and prone to errors. The following lists several common null judgment methods: Method 1: a method that is used by a maximum of people, intuitive, convenient, but inefficient. method 2: Compare the string length with high efficiency. It is the best method I know. method 3: the method provided by Java SE 6.0 is almost the same as method 2. For compatibility reasons, method 2 is recommended. 1: if (s = null | s. equals (""); 2: if (s = null | s. length () <= 0); 3: if (s = null | s. isEmpty ());