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(""));
Method 2. Compare string lengths, efficient High, it is the best method I know:
if(s == null || s.length() == 0);
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());
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 == "");
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"); }
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") ;
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 } }
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!

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)