Home > Java > JavaBase > Java determines whether a character is empty

Java determines whether a character is empty

angryTom
Release: 2019-11-11 14:45:36
Original
4608 people have browsed it

Java determines whether a character is empty

java determines whether a character is empty

The following summarizes four ways for you to determine whether a character is empty in java method, hope it helps you.

Method 1. The most popular method, intuitive and convenient, but very inefficient:

if(s == null || s.equals(""));
Copy after login

Method 2. Compare string lengths, efficient High, it is the best method I know:

if(s == null || s.length() == 0);
Copy after login

Method 3, a method only started to be provided by Java SE 6.0, is almost as efficient as Method 2, but for compatibility reasons, It is recommended to use method 2.

if(s == null || s.isEmpty());
Copy after login

Method 4. This is a relatively intuitive and simple method, and the efficiency is also very high. It is almost as efficient as methods 2 and 3:

if (s == null || s == "");
Copy after login

Note: s == null is necessary. (Recommended tutorial: java tutorial)

If the String type is null, operations such as equals(String) or length() will throw a java.lang.NullPointerException.

And the order of s==null must appear in front, otherwise it will also throw java.lang.NullPointerException.

The following Java code:

String str = null;
f(str.equals("") || str == null){//会抛出异常
    System.out.println("success");
}
Copy after login

Supplement:

1. Empty string Differences from null

1. Type

null represents the value of an object, not a string. For example, when declaring a reference to an object, String a = null;

"" represents an empty string, which means its length is 0. For example, declaring a string String str = "" ;

2. Memory allocation

String str = null ; means declaring a reference to a string object, but the pointer is null, that is to say, it is still It does not point to any memory space;

String str = ""; means declaring a reference of string type, whose value is ""empty string. This str reference points to the memory space of the empty string;

In Java, variables and reference variables are stored in the stack (stack), while objects (generated by new) are stored in the heap (heap):

It is as follows:

String str = new String("abc") ;
Copy after login

ps: =The one on the left is stored in the stack (stack), =The one on the right is stored in the heap (heap).

3. Sample program:

public class String_Demo01 {
    public static void main(String[] args) {        
        String str1 = new String() ;
        String str2 = null ;
        String str3 = "" ;
        System.out.println(str1==str2);                //内存地址的比较,返回false
        System.out.println(str1.equals(str2));         //值的比较,返回false
        System.out.println(str2==str3);                //内存地址的比较,返回false
        System.out.println(str3.equals(str2));         //值的比较,返回false
        System.out.println(str1==str3);                //内存地址的比较,返回false
        System.out.println(str1.equals(str3));         //值的比较,返回true
    }
}
Copy after login

Through the above program, the following conclusions can be drawn:

The values ​​of string objects and null are not equal, and the memory addresses are not equal either. ;

The value of the empty string object is not equal to null, and the memory address is not equal;

new String() creates a string object whose default value is "" (String type member The initial value of the variable is null)

The above is the detailed content of Java determines whether a character is empty. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template