• With the help of "==" operator is useful for reference comparison and it compares two objects.

    借助“ ==”运算符,对于参考比较非常有用,它可以比较两个对象。

  • "==" operator returns true if both references (objects) points to the same memory location otherwise it will return false if both objects point to different memory location.

    如果两个引用(对象)都指向相同的内存位置,则“ ==”运算符将返回true;否则,如果两个对象都指向不同的内存位置,则它将返回false。

  • null is a keyword introduced in java which is used to check whether an object is null or not.

    null是java中引入的关键字,用于检查对象是否为null。

  • Meaning of null in a different form is "no object" or "unknown".

    null的不同形式含义是“ no object”“ unknown”

  • We will see a program to check whether an object is null or not.

    我们将看到一个检查对象是否为空的程序。

Example:

例:

public class ToCheckNullObject {public static void main(String[] args) {// We created a string object with null
String str1 = null;
// By using == operator to compare two objects
// and with the help of null we will be easily identify
// whether object is null or not
if (str1 == null) {System.out.println("Given object str1 is null");
System.out.println("The value of the object str1 is " + str1);
} else {System.out.println("Given object  str1 is not null");
System.out.println("The value of the object str1 is " + str1);
}
// We created a string object with specified value
String str2 = "Welcome in Java World";
// By using == operator to compare two objects
// and with the help of null we will be easily identify
// whether object is null or not
if (str2 == null) {System.out.println("Given object str2 is null");
System.out.println("The value of the object str2 is " + str2);
} else {System.out.println("Given object str2 is not null");
System.out.println("The value of the object str2 is " + str2);
}
// We created a string object with specified value
String str3 = " ";
// By using == operator to compare two objects and
// with the help of null we will be easily identify
// whether object is null or not
if (str3 == null) {System.out.println("Given object str3 is null");
System.out.println("The value of the object str3 is " + str3);
} else {System.out.println("Given object str3 is not null");
System.out.println("The value of the object str3 is " + str3);
}
// We created an integer object with null
Integer i1 = null;
// By using == operator to compare two objects and
// with the help of null we will be easily identify
// whether object is null or not
if (i1 == null) {System.out.println("Given object i1 is null");
System.out.println("The value of the object i1 is " + i1);
} else {System.out.println("Given object i1 is not null");
System.out.println("The value of the object i1 is " + i1);
}
// We created an integer object with specified value
Integer i2 = 100;
// By using == operator to compare two objects and
// with the help of null we will be easily identify
// whether object is null or not
if (i2 == null) {System.out.println("Given object i2 is null");
System.out.println("The value of the object i2 is " + i2);
} else {System.out.println("Given object i2 is not null");
System.out.println("The value of the object i2 is " + i2);
}
}
}

Output

输出量

D:\Programs>javac ToCheckNullObject.java
D:\Programs>java ToCheckNullObject
Given object str1 is null
The value of the object str1 is null
Given object str2 is not null
The value of the object str2 is Welcome in Java World
Given object str3 is not null
The value of the object str3 is
Given object i1 is null
The value of the object i1 is null
Given object i2 is not null
The value of the object i2 is 100

翻译自: https://www.includehelp.com/java/how-to-check-an-object-is-null-in-java.aspx

如何在Java中检查对象是否为空?相关推荐

  1. java怎么判断对象不为空_java判断对象是否为空的方法

    java判断对象是否为空的方法 发布时间:2020-06-25 14:39:17 来源:亿速云 阅读:134 作者:Leah 这篇文章将为大家详细讲解有关java判断对象是否为空的方法,文章内容质量较 ...

  2. java判断bean是否为空_总结java中判断对象是否为空的方法

    我们想要判断对象是否为空,像基本类型那样判断是不可以的, ==={} ?这样是错误的,因为这只是比较引用地址是否相同,所以可以采取下面的方法来进行判断. 1.根据for...in遍历对象,如果存在则返 ...

  3. java 实体 text字段,如何在Java中修剪对象的某些字段?

    小编典典 假设你有三个字段f1,f2,f3中class A 创建一个新class B的字段f1 声明一个方法class A是这样 public B getTrimmedObject() 从A设置B的必 ...

  4. java构造方法的签名_如何在 Java 中构造对象(学习 Java 编程语言 034)

    1. 构造器 Java 对象都是在堆中构造的. 先看看 Employee 类的构造器: public class Employee { private String name; private dou ...

  5. java 初始化参数_我们如何在Java中的对象参数中初始化数组?

    您可以使用构造函数或使用setter方法来初始化与其他任何值一样在类内部声明的数组变量. 示例 在下面的Java示例中,我们声明一个数组类型的实例变量,并从构造函数中对其进行初始化.public cl ...

  6. java对象数组排序_如何在Java中对对象数组进行排序?

    小编典典 你有两种方法可以使用Arrays实用程序类 实现一个Comparator并将数组与比较器一起传递给sort方法,该方法将其作为第二个参数. 在对象所属的类中实现Comparable接口,并将 ...

  7. java判断List对象不为空

    写法1:(推荐,我使用这种方法) if(list!=null && !list.isEmpty()){//不为空的情况 }else{//为空的情况 } 写法2: if(null == ...

  8. java判断一个对象是否为空_Java中判断对象是否为空的方法的详解

    首先来看一下工具StringUtils的判断方法: 一种是org.apache.commons.lang3包下的: 另一种是org.springframework.util包下的.这两种StringU ...

  9. java空对象怎么判断,java怎么判断对象为空

    java怎么判断对象为空 发布时间:2020-06-10 09:34:15 来源:亿速云 阅读:116 作者:Leah java怎么判断对象为空?针对这个问题,今天小编总结这篇有关对象判断的文章,希望 ...

最新文章

  1. 这才叫细:带你深入理解Redis分布式锁
  2. 【转】mysql增量备份恢复实战企业案例
  3. BZOJ1010 [HNOI2008]玩具装箱toy 动态规划 斜率优化
  4. NFS服务的配置过程
  5. java 后台自动刷新请求_spring oauth2+JWT后端自动刷新access_token
  6. 从事 Android 开发六年,我学到的那些事!
  7. 一片关于Bootstarp4的文章
  8. 双指针(下标)的应用
  9. 二叉树类图_设计模式前言——UML类图
  10. beeline常用命令
  11. 【金猿案例展】某远程教育机构——以用户为中心 打造优势教学内容和智慧化学习产品...
  12. Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/ Using the URLconf defin
  13. TSINGSEE青犀视频RTMP推流摄像头焦距与监控距离存在什么关系?
  14. 人事管理系统 v4.1.8(源码)
  15. C++知识点打结(二)
  16. 数字图像处理 实验指导书
  17. lisp成套电气设计_电气工程制图课程设计.pdf
  18. 完成java课设的经验_课设后的感想
  19. 程序员必须掌握的英文单词(二)
  20. 获取天气数据 (根据天气接口返回的数据)

热门文章

  1. 上海大学计算机学院客座教授,刘云虹教授受聘上海大学外国语学院客座教授并做学术讲座...
  2. c#string倒数第二位插入字符_c#string倒数第二位插入字符_C#利用String类的IndexOf、LastIndexOf、...
  3. angular五大服务顺序,angularJS $事件处理程序的触发顺序
  4. React - antd4 中在form中为Switch赋值无效
  5. 点击底部input输入框,弹出的软键盘挡住input(苹果手机使用第三方输入法 )
  6. 并发编程-concurrent指南-阻塞队列BlockingQueue
  7. 从mysql向HBase+Phoenix迁移数据的心得总结
  8. 3.4 内置函数(1)
  9. 【腾讯Bugly干货分享】Android内存优化总结实践
  10. Linq list 排序,Dictionary 排序