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/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); }
本文来自博客园,作者:IT情深,转载请注明原文链接:https://www.cnblogs.com/wh445306/p/16751713.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义