I feel that there are too many methods for judging empty space in Java, and it feels a bit messy, so I will briefly summarize some of the methods I often use in the project. It is not all, it is just something I use frequently and is sufficient in normal times.
1. Quit
1.1 Determine whether the string is empty
Here are four common ways I use to determine whether a string is empty:
package ; import .; public class EmptyTest { public static void main(String[] args) { // 1. Determine whether the string is empty String str = null; // Method 1 if((str)) { //Judge not empty ("Nine 1"); } // Method 2 if(str == null || ()) { ("Empty 2"); } // Method 3 if(str == null || "".equals(str)) { // Use ("") directly if str is null, it will cause NullPointerException ("Non-Open 3"); } // Method 4 if(str == null || () == 0) { ("Empty 4"); } } }
1.2 Determine whether the List collection is empty
package ; import ; import ; import ; public class EmptyTest { public static void main(String[] args) { List<Object> list = new ArrayList<>(); // Method 1: if((list) || ()) { ("Nine 1"); } // Method 2: if((list) || () == 0) { ("Empty 2"); } } }
1.3 Determine whether the Map collection is empty
package ; import ; import ; public class EmptyTest { public static void main(String[] args) { Map map = new HashMap(); if(map == null || ()) { ("Empty"); } } }
2. Verification
1. Double equals (==)
Let’s talk about double equality first, in fact, == is rarely used in development. The == operator is used to compare whether the values of two variables are equal. However, its behavior depends on the type of variable being compared
2. Basic data types
When == is used to compare variables of basic data types such as int, float, charr, etc., it compares the actual values of these variables. For example, int a = 5; int b = 5; then a == b returns true because their values are all 5
3. Reference Type
When == is used to compare reference types (such as classes, interfaces, arrays, etc.), it compares whether two references point to the same object address in memory, rather than comparing the content or value of the object. For example, if you have two String objects created separately, even if they contain the same sequence of characters, comparing them with == will return false unless the two references are actually pointing to the same object instance.
If you want to compare whether the contents of two objects are equal, you should use the .equals() method. For example, for the String class, "hello".equals("hello") will return true because it compares the contents of strings rather than their memory addresses.
2.1 Determine whether two strings are equal
package ; import ; public class empty { public static void main(String[] args) { String str1 = "wft"; String str2 = "wft"; /** * Method 1 * Note: If one of the strings may be null, calling equals() directly may result in a NullPointerException. To avoid this, place the equals() call on a non-null object. Or adopt method 2 */ if((str2)) { ("equal"); } /** * Method 2 */ if((str1, str2)) { ("equal"); } } }
2.2 Determine whether two List sets are equal
package ; import ; import ; public class EmptyTest { public static void main(String[] args) { List<String> list1 = ("a", "b", "c"); List<String> list2 = ("a", "b", "c"); if((list2)) { // Pay attention to the situation of the null pointer ("equal"); } } }
2.3 Determine whether two List sets are equal
package ; import ; import ; public class EmptyTest { public static void main(String[] args) { Map<String, String> map1 = new HashMap<>(); ("key1", "value1"); ("key2", "value2"); Map<String, String> map2 = new HashMap<>(); ("key2", "value2"); ("key1", "value1"); if((map2)) { ("equal"); } } }
This is the introduction to this article about the detailed explanation of commonly used methods such as judgment and judgment in Java. For more related content such as judgment and judgment in Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!