In Java String determines whether the value is null or whether the null and address are equal

  • 2020-05-05 11:11:48
  • OfStack

String's null or null value judgment deals with
During the development of
, I have often encountered the following incorrect usage:
1, error 1:


if (name == "") {
//do something
}

2.


if (name.equals("")) {
//do something
}


3. Wrong:


if ( ! name.equals("")) {
//do something
}


Let's explain:
The above error usage 1 is the easiest for beginners to make and the least easy to detect, because the syntax itself is fine and the Java compiler does not report errors when compiling. But this condition may cause the program to have bug at run time, never to be true, that is, the statement in the if block will never be executed.

The above usage two, usage three, is a lot of Java familiar hands are also easy to make mistakes, why is it wrong? You might wonder.
Yeah, that's the right way to write it, but without an null condition, what happens if name=null? As a result, your program will throw an NullPointerException exception and the system will be suspended from normal service.
Of course, there is an exception if you have already made an null judgment on name.

The correct way to write it is to add name! = the condition of null, for example:


if (name != null &&  ! name.equals("")) {
//do something
}

Or


if (!"".equals(name)) {// will "" I'll do it in the front, so I don't care name Whether it is null Nothing can go wrong. 
//do something
}


Here's a simple example:

TestNullOrEmpty.java


public class Test {
  public static void main (String args[]){
    String value = null;
    testNullOrEmpty(value);
 
    value = "";
    testNullOrEmpty(value);
 
    value = " ";
    testNullOrEmpty(value);
     
    value = "hello me";
    testNullOrEmpty(value);    
  }
     
  static void testNullOrEmpty(String value){
    if(value == null){
      System.out.println("value is null");
    } else if ("".equals(value)){
      System.out.println("value is blank but not null");
    } else {
      System.out.println("value is \"" + value + "\"");
    }
     
    if (value == "") { //NG  The wrong way to write it  
      // Don't write it that way  
    } 
  }
}

Compile and execute:


c:\>javac TestNullOrEmpty.java

c:\>java TestNullOrEmpty


value is null.
value is blank but not null.
value is " "
value is "hello me!"


compares String addresses with
addresses


package com; 
 
public class A 
{ 
 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) 
  { 
 
    String a = "hello"; 
    String b = "he"; 
    String c = a.substring(0, 2); 
    System.out.println(b.equals(c));//true 
    System.out.println(b==c);//false 
     
    String d = new String("hello"); 
    System.out.println(d.equals(a));//true 
    System.out.println(d==a);//false 
     
    String e = new StringBuilder("hello").toString(); 
    System.out.println(e.equals(a));//true 
    System.out.println(e==a);//false 
     
    System.out.println(e.equals(d));//true 
    System.out.println(e==d);//false 
     
    String f = "hello"; 
    System.out.println(f.equals(a));//true 
    System.out.println(f==a);//true 
    System.out.println(f=="hello");//true 
    System.out.println(f=="hell"+"o");//true 
     
    String g = b+"llo"; 
    System.out.println(g==f);//false 
     
    String h = "he"+"llo"; 
    System.out.println(h==f);//true 
  } 
 
} 

Conclusion:

1. String out of new is to reallocate memory, strings are not Shared, and multiple out of new are not Shared.

2. The string concatenated or intercepted through string function operation is not Shared with the static string variable.

3. There are two ways to get a string from a plus sign.

A     "he"+"llo" is a static string, which is Shared
B     String a = "He";   a+"llo" is not a static string, it is not Shared


Related articles: