【问题标题】:Checking null value for a String/Object in java?在java中检查字符串/对象的空值?
【发布时间】:2016-03-03 08:04:57
【问题描述】:

有什么好处

String a = null;
if(null != a)

结束

if(a !=null)

我尝试了这两种说法,它们都运行良好。有什么建议我为什么要选择第一个?

【问题讨论】:

标签: java performance null


【解决方案1】:

两者是相同的,但是如果您在布尔值上检查 ==

if(a == true)

if(true == a)

后者会更好,因为通过只输入= 而不是== 会出现拼写错误:

if(a = true) //still compilable but not what you intended
if(true = a) //cannot be compiled, hence you know you typed it wrongly

【讨论】:

  • 在 C(++) 中它很有用。
【解决方案2】:

首先提出问题听起来更自然。

在英语中,您会说“如果答案正确,请检查”。你不会说,“如果正确就是答案”。人们用他们思考和说话的方式编码。

切换顺序的唯一有效用例(我知道)是您调用equals() 但您正在测试的对象可能为空。在这种情况下,这样做会更干净

if ("expected".equals(value))

if (value != null && value.equals("expected"))

【讨论】:

    【解决方案3】:

    优势: 将常量值放在表达式中不会改变程序的行为(除非值评估为假)。在使用单个等号 (=) 进行赋值而不是比较的编程语言中,可能的错误是无意中赋值而不是编写条件语句。

    性能: 对性能没有影响

    可读性:降低

    缺点避免空行为的好处也可以被认为是一个缺点,因为空指针错误可以被隐藏,并且只会在程序中很晚才出现。

    【讨论】:

      【解决方案4】:

      嗯,除了(缺乏)可读性之外没有其他任何东西。

      此外,它仅适用于布尔类型:

      boolean b = true;
      if (b) {
          System.out.println("b, it is"); // << this
      } else {
          System.out.println("not b");
      }
      

      让我们破解:

      boolean b = false;
      if (b = true) {
          System.out.println("b, it is"); // << this
      } else {
          System.out.println("not b");
      }
      

      其他方式:

      boolean b = true;
      if (b = false) {
          System.out.println("b, it is");
      } else {
          System.out.println("not b"); // << this
      }
      

      但是有一个int:

      int a = 5;
      if(a = 0) { // error: incompatible types: int cannot be converted to boolean
          System.out.println("0");
      } else {
          System.out.println("not 0");
      }
      

      在您的示例中也是如此,使用字符串和 a = null 表达式。因此,虽然这个 Yoda 比较在 C 中很有用,但在 Java 中却毫无用处。

      【讨论】:

        【解决方案5】:

        似乎有一个微小的差异。 使用 JMH,SecureRandom 测试和随机测试之间似乎存在微小差异。我认为差异并不显着。

        Benchmark                                                        Mode  Samples  Score  Score error   Units
        c.g.v.YodaCompPerformace.countNullsArrayList                    thrpt      200  1.345        0.009  ops/ms
        c.g.v.YodaCompPerformace.countNullsArrayListSecureRandom        thrpt      200  1.349        0.008  ops/ms
        c.g.v.YodaCompPerformace.countNullsArrayListSecureRandomYoda    thrpt      200  1.358        0.009  ops/ms
        c.g.v.YodaCompPerformace.countNullsArrayListYoda                thrpt      200  1.361        0.009  ops/ms
        

        JHM 代码:

        import java.security.SecureRandom;
        import java.util.ArrayList;
        import java.util.Collections;
        import java.util.List;
        import java.util.Random;
        import java.util.concurrent.TimeUnit;
        import org.openjdk.jmh.annotations.Benchmark;
        import org.openjdk.jmh.annotations.Level;
        import org.openjdk.jmh.annotations.OutputTimeUnit;
        import org.openjdk.jmh.annotations.Scope;
        import org.openjdk.jmh.annotations.Setup;
        import org.openjdk.jmh.annotations.State;
        
        @OutputTimeUnit(TimeUnit.MILLISECONDS)
        @State(Scope.Thread)
        public class YodaCompPerformace {
        
            static List<Integer> arrayListSecureRandom;
            static List<Integer> arrayListRandom;
        
            @Setup(Level.Iteration)
            public void setUpSecureRandom() {
                arrayListSecureRandom = new ArrayList<>(100000);
                for (int count = 0; count < 100000; count++) {
                    if ((count & 1) == 0) {
                        arrayListSecureRandom.add(count);
                    } else {
                        arrayListSecureRandom.add(null);
                    }
                }
                Collections.shuffle(arrayListSecureRandom, new SecureRandom());
            }
        
            @Setup(Level.Iteration)
            public void setUp() {
                arrayListRandom = new ArrayList<>(100000);
                for (int count = 0; count < 100000; count++) {
                    if ((count & 1) == 0) {
                        arrayListRandom.add(count);
                    } else {
                        arrayListRandom.add(null);
                    }
                }
                Collections.shuffle(arrayListRandom, new Random());
            }
            @Benchmark
            public int countNullsArrayListSecureRandom() {
                int countNulls = 0;
                for (Integer i : arrayListSecureRandom) {
                    if (i == null) {
                        countNulls++;
                    }
                }
                return countNulls;
            }
            @Benchmark
            public int countNullsArrayListSecureRandomYoda() {
                int countNulls = 0;
                for (Integer i : arrayListSecureRandom) {
                    if (null == i) {
                        countNulls++;
                    }
                }
                return countNulls;
            }
            @Benchmark
            public int countNullsArrayList() {
                int countNulls = 0;
                for (Integer i : arrayListSecureRandom) {
                    if (i == null) {
                        countNulls++;
                    }
                }
                return countNulls;
            }
            @Benchmark
            public int countNullsArrayListYoda() {
                int countNulls = 0;
                for (Integer i : arrayListSecureRandom) {
                    if (null == i) {
                        countNulls++;
                    }
                }
                return countNulls;
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-29
          • 1970-01-01
          • 1970-01-01
          • 2013-07-06
          • 1970-01-01
          • 2011-12-08
          • 1970-01-01
          相关资源
          最近更新 更多