字符串方法中空场景的最佳指令

bttbmeg0  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(421)

关闭。这个问题是基于意见的。它目前不接受答案。
**想改进这个问题吗?**更新这个问题,这样就可以通过编辑这篇文章用事实和引文来回答。

21天前关门了。
改进这个问题
我想听听关于这个主题的意见和争论,我还没有决定。
equals方法是值相等/测试的最佳方法?

if ("text".equals(testString))
8xiog9wr

8xiog9wr1#

请参阅下面的代码及其输出,只有方法2可以工作。

class Main
{
    public static void main(String args[])
    {
        String myString = "test";
        String myString2 = null;

        if (myString.equals("test"))
        {
            System.out.println("Approach 1: myString.equals(\"test\") :Working if myString is not null");
        }
        if ("test".equals(myString))
        {
            if ("test".equals(myString2))
            {
                //This line wont execute
            }
            else
            {
                System.out.println("Approach 2: \"test\".equals(myString) :Working even if myString is null");
            }
        }
        if ("test".compareTo(myString) == 0)
        {
            System.out.println("Approach 3: \"test\".compareTo(myString) == 0 :Working if myStrig is not null");
        }
        if (myString.compareTo("test") == 0)
        {
            System.out.println("Approach 4: myString.compareTo(\"test\") == 0 :Working if myString is not null");
        }
        try
        {
            if (myString2.equals("test"))
            {
                //This will never execute
            }
        }
        catch (Exception e)
        {
            System.out.println("Approach 1: myString2.equals(\"test\")  wont work if myString is null");
        }

        try
        {
            if ("test".compareTo(myString2) == 0)
            {
                //This line wont execute
            }
        }
        catch (Exception e)
        {
            System.out.println("Approach 3: \"test\".compareTo(myString2)  wont work if myString is null");
        }
        try
        {
            if (myString2.compareTo("test") == 0)
            {
                //This line wont execute
            }

        }
        catch (Exception e)
        {
            System.out.println("Approach 4: myString2.compareTo(\"test\")  wont work if myString is null");
        }
    }
}

原因1和4不起作用,因为在这两种情况下,输入字符串都为空。对于方法3,它要求非空值,请参阅compareto函数的实现。

public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }
qaxu7uf2

qaxu7uf22#

1和4可能导致 NPEmyString 可能为空。
数字3也可能抛出一个npe,因为预期的返回是一个整数,也就是小于、大于或等于。如果传递的参数为空,则没有任何依据返回这些参数中的任何一个,因为这会产生误导,并且任何一个参数都会与另一个参数一样好。所以最好的选择是 NPE .
对于数字2,equals测试是一个二进制测试,因此它要么相等,要么不相等。”test”用作equals的引用,因此使用它不会抛出npe。在这种情况下,等于的论点是 myString . 一个好的equals实现将首先检查参数是否为null,然后再使用它。因此,没有 NPE 将被抛出。

相关问题