如何在Java中检查对象是否为空?

  • 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378246.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android编程从零开始,从零开始学习android开发

博主最近开通了Android栏目,现在正在从零开始学习android,遇到的所有值得分享的知识点以及遇到的问题将发布在这个博客的android栏目下。因为我有着深厚的java底子,所以学习起来得心应手,十分的简单,当然也只能算是入门…

CNN基本步骤以及经典卷积(LeNet、AlexNet、VGGNet、InceptionNet 和 ResNet)网络讲解以及tensorflow代码实现

课程来源:人工智能实践:Tensorflow笔记2 文章目录前言1、卷积神经网络的基本步骤1、卷积神经网络计算convolution2、感受野以及卷积核的选取3、全零填充Padding4、tf描述卷积层5、批标准化(BN操作)6、池化Pooling7、舍弃Dropout8、卷积神经网络搭建以及参数分析2、经…

String.valueOf()

1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下列几种 String.valueOf(boolean b) : 将 boolean 变量 b 转换成字符串 String.valueOf(char c) : 将 char 变量 c 转换成…

emacs gdb 调试

写下linux下用emacs调用dgb调试的方法 emacs中使用gdb 说明C等价于ctrl M等价于alt 1,编写 .c函数 test.c 2,gcc一把 看看对不对 带上-g gcc -g -o test .debug test.c 如果要用gdb调试器,必须使用g选项。 3,#ema…

第十九章 趣味编程

第十九章 趣味编程 本章将介绍一些通用的Python编程指南。 为何要有趣 Python有趣的地方之一就是让用户的编程效率非常高效。 极限编程是一种软件开发方法 编程柔术 python的灵活性描述原型设计Python的优点之一是让你能够快速地编写程序。要更深入地了解面临的问题&#…

android按钮在容器下方,使用flex布局解决安卓手机上固定在底部的按钮,在键盘弹起后挡住input输入框的问题...

移动端经常会出现,一个表单里面,确定按钮固定在底部这样的布局,一般会让按钮absolute或者fixed,这样在ios上没有问题,但是在安卓手机上,当表单里面的input输入框获得焦点的时候,按钮会挡在表单上…

dbms支持哪几种数据模型_DBMS中不同类型的数据模型

dbms支持哪几种数据模型资料模型 (Data Model) A data model is a model that defines in which format the data are represented and accessed. Data model mainly defines some of the data elements and relationships that exist between them. 数据模型是定义数据以哪种格…

JS 数组迭代方法

var arr [3,4,5,6,7,"a"]; var isNum function(elem,index,AAA){ return !isNaN(elem);} var toUpperCase function(elem){ return String.prototype.toUpperCase.apply(elem);} var print function(elem,index){ console.log(index"."elem);} /*对数组…

php开源问答_PHP基础知识能力问答

php开源问答This section contains Aptitude Questions and Answers on PHP Basics. 本部分包含有关PHP基础知识的 Aptitude问题和解答。 1) There are the following statements that are given below, which of them are correct PHP? PHP stands for the Preprocessor Hom…

【数据结构基础笔记】【顺序表】

代码参考《妙趣横生的算法.C语言实现》 文章目录前言1、创建顺序表2、顺序表插入元素3、顺序表删除元素4、顺序表实例分析1、静态2、动态5、顺序表总结前言 本章总结:从静态和动态分别进行顺序表的创建、插入、删除、以及实例分析 1、创建顺序表 1、静态地生成一张…

ubuntu安装oracle unzip: No such file or directory

$ln -s /usr/bin/unzip /你的oracle11安装目录/install/unzip$sudo chmod 777 /usr/bin/unzip转载于:https://www.cnblogs.com/qm4050/archive/2011/08/25/2241466.html

一、网络爬虫概述

1,浏览器与网络爬虫的区别 答: 对于浏览器而言:浏览器打开一个网站,会对网站服务器发送一个request请求,服务器收到该请求之后,会给浏览器一个respond响应,该响应携带很多数据,之后…

百度android广告sdk下载,IS_Freedom

美数广告 SDK接入流程1.嵌入广告SDK将 sdk-android-demo/app/libs 中的 meishu-sdk_xxx_release.aar、open_ad_sdk_xxx.aar、Baidu_MobAds_SDK-release-xxx.aar、GDTSDK.unionNormal.xxx.aar、msa_mdid_1.0.13 拷贝到项目的 libs 下,对应的 build.gradle 文件里面添…

关于《加密与解密》的读后感----对dump脱壳的一点思考

偶然翻了一下手机日历,原来今天是夏至啊,时间过的真快。ISCC的比赛已经持续了2个多月了,我也跟着比赛的那些题目学了2个月.......虽然过程很辛苦,但感觉还是很幸运的,能在大三的时候遇到ISCC,不管怎样&…

java vector_Java Vector elements()方法与示例

java vector向量类elements()方法 (Vector Class elements() method) elements() method is available in java.util package. elements()方法在java.util包中可用。 elements() method is used to get an enumeration of the elements that exist in this Vector. elements()方…

【数据结构基础笔记】【链表】

代码参考《妙趣横生的算法.C语言实现》 文章目录前言1、链表基础2、创建一个链表3、插入结点4、删除结点5、销毁链表6、实例分析前言 本章总结:链表的定义、创建、销毁,结点的插入与删除 1、链表基础 链表的物理存储结构是用一组地址任意的存储单元存储…

动态添加,删除行之心理测试系统

动态添加,删除行之考试系统 数据库设计: xl_option 题目选项 20090105134755404(编号) 20090105134904421(外键) 比较符合(选项内容) ②(选项标号) 2(选项分值) xl_subject 题目信息 20090105134943608(编号&#xff…

android bitmap裁剪中间,Android裁剪中心位图

虽然上面的大多数答案提供了一种方法来实现这一点,但已经有一种内置的方法来实现这一点,它是一行代码(ThumbnailUtils.extractThumbnail())int dimension getSquareCropDimensionForBitmap(bitmap);bitmap ThumbnailUtils.extractThumbnail(bitmap, di…

二、request请求库

一、requests介绍与安装 1,requests介绍 答:requests是一个优雅且简单的Python HTTP请求库 2,requests作用 答:requests的作用是发送请求获取响应数据 3,requests安装 答:pip install requests 二、…

Java Vector Capacity()方法与示例

向量类的Capacity()方法 (Vector Class capacity() method) capacity() method is available in java.util package. Capacity()方法在java.util包中可用。 capacity() method is used to return the current capacity (i.e. initially, how many object exists) of this Vecto…