A problem in Java where string judgments are null or NULL and the address is equal _java

Source: Internet
Author: User

Handling of NULL or null values for string
in the development process, the author often encountered the following errors in use:
1, Error usage one:

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

2, Error usage two:

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


3, Error usage three:

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


Let us explain:
The above error usage 1 is the easiest for beginners, and the least likely to be found, because their syntax is no problem, the Java compiler compiles without error. However, this condition may cause a bug in the program at run time and will never be true, that is to say, the statement in the If block will never be executed.

The above usage two, usage Three's writing, is including many Java proficient also very easy to make the mistake, why is wrong? Maybe you'll be wondering.
Yes, the writing itself is correct, but, less a null judgment condition, imagine, if name=null, what will happen? The consequence is that your program will throw NullPointerException exceptions, the system will be suspended and no longer provide normal service.
Of course, if you have previously made null judgments on name exceptions.

The correct wording should precede the condition of name!= NULL, as an example:

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

Or

if (! "". Equals (name)) {//writes "" to the front so that no error occurs, regardless of whether the name is null or not.
//do Something
}


Here, let's give 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 wrong writing 
      //Don't use this type}}}

Compile execution:

C:\>javac Testnullorempty.java

C:\>java testnullorempty

Value is null.
The value is blank but not null.
The value is "" The
value is "Hello me!"


Compare string address equality

Package com; public class A {/** * @param args */public static void main (string[] args) {String a = "hell 
    O "; 
    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}}

Summarize:

The string that comes out of 1.new is the reallocation of memory, the string is not shared, and the new one is not shared.

2. Strings that are spliced or intercepted through string functions are not shared with static string variables.

3. Strings obtained by the plus sign have two cases.

A "he" + "Llo" is a static string that is shared
B String a = "he"; A + "Llo" is not a static string, is not shared

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.