How to Determine if Java Is an Empty

Asked 1 years ago, Updated 1 years ago, 36 views

I'd like to determine if the value of val is empty.
I don't understand the difference between the two if statements below.
Do they both have the same meaning?
Is it better not to use != or == for reference strings?

[Code 1]

String val=";
if(val!=""){

}

[Code 2]

String val=";
if(val.isEmpty()){

}

java

2022-09-30 21:32

1 Answers

Do they both mean the same thing?

The code example in the question happens to work exactly as the programmer intended, but it is not exactly the same.

public class Main {

    static String empty1="";
    static String empty 2="";
    static String empty3 = new String("");

    static String abc1 = "abc";
    static String abc2 = "abc";

    public static void main(String[]args) {

        // equals determine whether objects are equal to each other
        System.out.println("s1eqs2:"+empty1.equals(empty2));
        System.out.println("s1eqs3:"+empty1.equals(empty3));

        // Determine if == is the same object (instance)
        System.out.println("s1==s1:"+(empty1==empty1));
        System.out.println("s1==s2:"+(empty1==empty2));
        System.out.println("s1==s3:"+(empty1==empty3));

        System.out.println("abc1==abc2:"+(abc1==abc2));
    }
}
s1eq s2:true
s1eqs3 —true
s1 == s1 —true
s1 == s2 —true
s1 == s3 : false
abc1 == abc2 —true

== is used to determine if the objects to be compared are exactly the same object (the same instance, the same reference).

Therefore, " and new String("") do not match the above sample code and return false.
(new String() will generate a new instance)

The reason why empty1 and empty2 are identified in == as pointing to the same instance is because JVM (Java Virtual Machine) has a mechanism called Run-Time Constant Pool, where all character literals are managed in this pool and only one instance has the same value.

In other words, no matter how many " are written on the source code, they all refer to one identical instance.

https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html

The Java programming language requirements that identical string literals (that is, literals that contain the same sequence of code points) must refer to the same instance of class String (JLS 3 3.10.5).

https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5

3.10.5.String Literals

Moreover, a string literal always references to the same instance of class String. This is because string literals-or, more generously, strings that are the values of constant expressions ( 1515.28) - are "internated" so as to share unique instances, using the modulating.

Is it better not to use != or == for reference strings?

For strings, it is recommended to use isEmpty() for the reasons mentioned above.
(Use equals() to compare values between strings.)

Basically, isEmpty() is recommended to determine if a string or collection is empty.
(The string can also be seen as a collection of char types)

One reason is that each collection has a different specific and optimal way of determining if the collection is empty, and each collection is concealed by a method called isEmpty().

In rare cases, you see code that determines whether a collection is empty by the size of 0 or not, but it is not recommended because some internal structures of the collection may take a long time to retrieve the size.

String#isEmpty()

/**
 * Returns {@code true} if, and only if, {@link#length()}is {@code0}.
 *
 * @return {@codetrue} if {@link#length()} is {@code0}, elsewise
 * {@code false}
 *
 * @since 1.6
 */
public boolean isEmpty(){
    return value.length == 0;
}

ArrayDeque#isEmpty()

/**
 * Returns {@code true} if this request contains no elements.
 *
 * @return {@code true} if this request contains no elements
 */
public boolean isEmpty(){
    return head==tail;
}


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.