The null or null value of String is judged
During the development process, I often encounter the following wrong usages:
1. Error usage 1:
if (name == "") {//do something} 2. Error usage 2:
if (name.equals("")) {//do something}
3. Error usage three:
if (!name.equals("")) {//do something}
Let's explain:
The above error usage 1 is the most likely mistake that beginners make and are least likely to be discovered, because their syntax itself is fine, and the Java compiler does not report an error when compiling. However, this condition may cause a bug in the program during runtime and will never be true. In other words, the statements in the if block will never be executed.
The above-mentioned usage 2 and usage 3 are written, including many Java proficients who are easy to make. Why is it wrong? Maybe you will be puzzled.
Yes, their writing is correct, but there is a lack of a null judgment condition. Just imagine, what will happen if name=null? The consequence is that your program will throw a NullPointerException exception, the system will be suspended and no longer provide normal services.
Of course, if you have made a null judgment on name before, the exception is.
The correct way to write it should first add the condition of name != null, such as:
if (name != null && !name.equals("")) {//do something}or
if (!"".equals(name)) {//Write "" in front of it, so that no matter whether the name is null or not, there will be no error. //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 == "") { //Underwriting of NG//Don't use this writing} }} Compile execution:
c:/> javac TestNullOrEmpty.javac:/> java TestNullOrEmpty
value is null.value is blank but not null.value is " "value is "hello me!"
Compare String addresses equal
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 } }Summarize:
1. The String from new is to re-allocate memory, strings are not shared, and multiple new is not shared.
2. The strings and static string variables that are spliced or intercepted are not shared through string functions.
3. There are two situations for strings obtained by plus signs.
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