话说if (null == x)

本文探讨了Java项目中if语句的一种特殊写法,并解释了其源自C/C++的原因。文章分析了这两种语言中if语句的不同处理方式,强调了Java语言特性如何避免了C/C++中常见的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近看到Java项目的代码中看到这样的语句,
Java代码  收藏代码
  1. if (null == model)  
  2. {  
  3.      ...  
  4. }  

     我问开发这段代码的同事问什么这么写,同事说是编程规范规定的。

 

     但是在Java项目中,不需要这样写,也不应该这样写。

 

     if 的判断语句倒着写,这种写法是从C\C++语言开始的,(获取其它语言更早就有了 :-) )

     在C\C++语言中,数字等于0表示false, 不等于0表示true, 下面的两个if判断代码都是合法的,但是含义却南辕北辙

Cpp代码  收藏代码
  1. // 1 赋值 + 判断  
  2. int count = -1;  
  3. if (count = read())  
  4. {  
  5.      ...  
  6. }  
  7.   
  8. // 2 判断  
  9. int expectedCount = 20;  
  10. if (expectedCount == read())  
  11. {  
  12.      ...  
  13. }  

  人总是会犯错误的,一个不小心,第二段代码就成了

Cpp代码  收藏代码
  1. int expectedCount = 20;  
  2. if (expectedCount = read()) // == 变成了 =  
  3. {  
  4.      ...  
  5. }  

  为了防止这种低级错误,于是就有了编程规范。

Cpp代码  收藏代码
  1. int expectedCount = 20;  
  2. if (read() = expectedCount) // 直接编译错误,就避免了错误的产生  
  3. {  
  4.      ...  
  5. }  

  所以在C、C++项目中有这样的编程规范是正确的。

 

但是在Java项目中,

Java 语言中布尔变量就只有true和false, 数字不能表示布尔.下面的代码编译器是直接报错的,所以就避免了低级错误的发生.

Java代码  收藏代码
  1. int expectedCount = 20;  
  2. if (expectedCount = read()) // 编译器直接报错  
  3. {  
  4.      ...  
  5. }  

 所以在Java项目中,不需要 倒着写; 这样还会降低可读性,所以不应该 倒着写;

<insert id="insertMlbSmtPickQpl" parameterType="MlbSmtPickQpl" useGeneratedKeys="true" keyProperty="id"> insert into mlb_smt_pick_qpl <trim prefix="(" suffix=")" suffixOverrides=","> <if test="model != null">model,</if> <if test="site != null">site,</if> <if test="process != null">process,</if> <if test="lineName != null">line_name,</if> <if test="produceStationName != null">produce_station_name,</if> <if test="stationType != null">station_type,</if> <if test="equipmentType != null">equipment_type,</if> <if test="equipmentManu != null">equipment_manu,</if> <if test="equipmentModel != null">equipment_model,</if> <if test="ct != null">ct,</if> <if test="pickQpl != null">pick_qpl,</if> <if test="createTime != null">create_time,</if> <if test="createBy != null">create_by,</if> <if test="updateTime != null">update_time,</if> <if test="updateBy != null">update_by,</if> <if test="productType != null">product_type,</if> <if test="parentTaskNo != null">parent_task_no,</if> <if test="childTaskNo != null">child_task_no,</if> <if test="version != null">version,</if> <if test="actId != null">act_id,</if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="model != null">#{model},</if> <if test="site != null">#{site},</if> <if test="process != null">#{process},</if> <if test="lineName != null">#{lineName},</if> <if test="produceStationName != null">#{produceStationName},</if> <if test="stationType != null">#{stationType},</if> <if test="equipmentType != null">#{equipmentType},</if> <if test="equipmentManu != null">#{equipmentManu},</if> <if test="equipmentModel != null">#{equipmentModel},</if> <if test="ct != null">#{ct},</if> <if test="pickQpl != null">#{pickQpl},</if> <if test="createTime != null">#{createTime},</if> <if test="createBy != null">#{createBy},</if> <if test="updateTime != null">#{updateTime},</if> <if test="updateBy != null">#{updateBy},</if> <if test="productType != null">#{productType},</if> <if test="parentTaskNo != null">#{parentTaskNo},</if> <if test="childTaskNo != null">#{childTaskNo},</if> <if test="version != null">#{version},</if> <if test="actId != null">#{actId},</if> </trim> </insert>改为foreach遍历写法
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值