Java if判断对象为null时,null放在比较运算符的左边还是右边?哪种才是java标准?进来看看你就知道了

我知道在C语言中是将Null放前面的,Delphi Pascal语法中是放后面的。Java中没找到规范定义,但是我下载了谷歌的gson-2.8.5-sources.jar和Jsoup的源码,结果惊人的发现,全是放在后面的。由此可以确定Java规范中,对一个对象是否Null判断时null均应该放后面。事实上:从人类的思维角度和可读性来看也是放后面(右边)更优。因为你要判断的对象是value是否==null,而不是判断的null是否==value。所以。请那些所谓的”大神“还有各种培训班老师不要在误导孩子们了,咱们不是用的C。Java是高级语言。啊哈哈。

下面贴几个来搞笑的连接,大家可以自己去看看:

https://www.cnblogs.com/codingmengmeng/p/9985061.html

https://blog.csdn.net/yvhkyiu/article/details/73321754

https://zhidao.baidu.com/question/744398133846658932.html

https://zhidao.baidu.com/question/214366905.html?qbl=relate_question_1&word=java%20if%20null%20%D7%F3%B1%DF%20%D3%D2%B1%DF

https://zhidao.baidu.com/question/982967488860682539.html?sort=11&rn=5&pn=0#wgt-answers

很明显,国内网站上没有一个正确的。全是在误导人,即使不看大厂源码,就在java第一大开发工具idea中,你使用a.nn,a.null代码模板,你看自动生成的代码是怎么样的这个就直接说明问题。可以说国内的程序猿,不知道这些人到底算不算程序员,还是说只是典型的代码搬运工,缺乏独立思考能力和判断能力。习惯了人云亦云的思维方式

图一gson-2.8.5-sources.jar 源码搜索结果

图2,jsoup-1.12.1-sources.jar搜索结果

贴一段gson-2.8.5-sources.jar的源码

  /**
   * Adds a member, which is a name-value pair, to self. The name must be a String, but the value
   * can be an arbitrary JsonElement, thereby allowing you to build a full tree of JsonElements
   * rooted at this node.
   *
   * @param property name of the member.
   * @param value the member object.
   */
  public void add(String property, JsonElement value) {
    if (value == null) {
      value = JsonNull.INSTANCE;
    }
    members.put(property, value);
  }

  /**
   * Adds the specified boolean to self.
   *
   * @param bool the boolean that needs to be added to the array.
   */
  public void add(Boolean bool) {
    elements.add(bool == null ? JsonNull.INSTANCE : new JsonPrimitive(bool));
  }

  /**
   * Adds the specified character to self.
   *
   * @param character the character that needs to be added to the array.
   */
  public void add(Character character) {
    elements.add(character == null ? JsonNull.INSTANCE : new JsonPrimitive(character));
  }

  /**
   * Adds the specified number to self.
   *
   * @param number the number that needs to be added to the array.
   */
  public void add(Number number) {
    elements.add(number == null ? JsonNull.INSTANCE : new JsonPrimitive(number));
  }

  /**
   * Adds the specified string to self.
   *
   * @param string the string that needs to be added to the array.
   */
  public void add(String string) {
    elements.add(string == null ? JsonNull.INSTANCE : new JsonPrimitive(string));
  }

  /**
   * Adds the specified element to self.
   *
   * @param element the element that needs to be added to the array.
   */
  public void add(JsonElement element) {
    if (element == null) {
      element = JsonNull.INSTANCE;
    }
    elements.add(element);
  }


猜你喜欢

转载自blog.csdn.net/wh445306/article/details/112459033
今日推荐