Use equals () tips

Use equals () tips

Often you need to compare two strings are equal, if and when the String object is null, but the use of equals () method to compare, the error will be thrown NullPointerException. Or to add an extra step of: determining str1 not empty

public  static  void main (String [] args) { 
        String str1 = null ;
         // if the two strings are equal 
        IF (str1 =! null ) {# to be added is determined: str1 is not empty, otherwise given a NullPointerException
             Boolean of isEqual = "Hello" .equals (str1); 
            System.out.println (of isEqual); 
        } 
    }

 

In fact, you can put a string on the front, so even if str1 is empty, then no error will, is determined not only equal

    public  static  void main (String [] args) { 
        String str1 = null ;
         // if the two strings are equal 
        Boolean of isEqual = "Hello" .equals (str1); 
        System.out.println (of isEqual);   // output false 
    }

 

Guess you like

Origin www.cnblogs.com/eathertan/p/12517836.html