Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java determine whether the value of String is an empty string or null

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to judge whether the value of String is an empty string or null". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The difference between Java empty string and null

1. Type

Null represents the value of an object, not a string. For example, declare a reference to an object, String a=null.

"" represents an empty string, that is, its length is 0. For example, declare a string String s = "".

2. Memory allocation

String a string null; declares a reference to a string object, but points to null, which means it doesn't point to any memory space yet.

String s = ""; declares a reference of type string whose value is "" empty string, and this s 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 placed in the heap (heap):

It is as follows:

String str = new String ("abc")

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

Code example 1:

String str1=null; String str2= "; / str1==null is true System.out.println (" str1 and null = comparison result is: "+ (str1==null)); / / java.lang.NullPointerException null pointer exception System.out.println (" isEmpty () method of str1 "+ (str1.isEmpty () / / java.lang.NullPointerException null pointer exception System.out.println ("length () method of str2" + (str1.length (); / / java.lang.NullPointerException null pointer exception System.out.println ("equals comparison between str1 and null:" + (str1.equals (null) / / str2==null is false System.out.println ("str2 and null = comparison result is:" + (str2==null)); / / str2== "is true System.out.println (" str2 and "+"+" = = comparison result is "+ (str2==") / / str2.isEmpty () is true System.out.println ("isEmpty () method of str2" + str2.isEmpty ()); / / str2.equals (null) is true System.out.println ("comparison of equals methods of str2 and null:" + (str2.equals (null) / / str2.isEmpty () is true System.out.println ("isEmpty () method of str2" + (str2.isEmpty (); / / str2.length () results in 0 System.out.println ("length () method of str2" + (str2.length ()

Code example 2:

String str1= new String (); String str2= null; String str3 = ""; System.out.println (str1==str2); / / comparison of memory addresses returns false System.out.println (str1.equals (str2)); / / comparison of values returns false System.out.println (str2==str3) / / comparison of memory addresses, return false System.out.println (str3.equals (str2)); / / comparison of values, return false System.out.println (str1==str3); / / comparison of memory addresses, return false System.out.println (str1.equals (str3)); / / comparison of values, return true

The following conclusions can be drawn from the above two code examples:

1 if you want to call a method, you must first have an object, but null is not an object, and there is no space for it in memory, so null cannot call methods in String, isEmpty and length and equals methods cannot be called.

2 the string object is not equal to the value of null, and the memory address is not equal.

3 the value of the empty string object is not equal to that of null, and the memory address is not equal.

4 new String () creates a string object with a default value of "" (the initial value of a member variable of type String is null.

Java method to determine whether a string is all empty

Method 1

The most widely used method is intuitive, convenient, but inefficient:

If (s = = null | | s.equals ("))

Method 2

Comparing string length is efficient and is the best way I know:

If (s = = null | | s.length () = = 0)

Method 3

The efficiency of the method provided by Java SE 6.0is almost equal to that of method 2, but it is recommended to use method 2 for compatibility.

If (s = = null | | s.isEmpty ())

Method 4

This is a relatively intuitive and simple method, and the efficiency is also very high, which is similar to that of methods 2 and 3:

If (s = = null | | s = = "")

Note: s==null is necessary.

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

And the order of the s==null must appear first, otherwise the java.lang.NullPointerException will also be thrown.

Such as the following java code:

String s = null; if (s.equals ("") | | s = = null) {System.out.println ("success");}

The running result is as follows: the Exception in thread "main" java.lang.NullPointerException; prompts a null pointer exception.

That's all for "how Java determines whether the value of String is an empty string or null". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 270

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report