博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中的null是什么?
阅读量:2537 次
发布时间:2019-05-11

本文共 5129 字,大约阅读时间需要 17 分钟。

  • As we know null is an important concept in every language not only in Java but here we will study various factors regarding null.

    我们知道null在每种语言中都是重要的概念,不仅在Java中,在这里我们还将研究有关null的各种因素。

  • null is a very critical factor that means we need to focus when we work with null.

    null是一个非常关键的因素,这意味着我们在处理null时需要重点关注。

  • null is a keyword in Java and it is related to NullPointerException and NullPointerException is a package in java.lang package like this java.lang.NullPointerException.

    null是Java中的关键字,它与NullPointerException相关,而NullPointerException是java.lang包中的一个包,例如java.lang.NullPointerException 。

  • We will see NullPointerException throw if we perform operations with or without null in Java.

    如果在Java中执行带或不带null的操作,我们将看到NullPointerException抛出。

In a general way we will discuss a few cases and the cases are given below...

通常,我们将讨论一些情况,下面给出了这些情况。

Case 1: We know that null is cases sensitive

情况1:我们知道null是区分大小写的

Here, we will see why null is case sensitive in Java and null is a keyword in Java that's why null is case sensitive because all the keywords in java are case sensitive.

在这里,我们将了解为什么null在Java中是区分大小写的,null是 Java中的关键字 ,这就是为什么null是区分大小写的原因,因为java中的所有关键字都区分大小写。

Note:

注意:

Case sensitive means the word written in small letters and Capital letters has different meanings for example: null, NULL(Both are different).

区分大小写意味着用小写字母和大写字母写的单词具有不同的含义,例如:null,NULL(两者都不同)。

In java (null) is valid but if we write (NULL, 0, Null), etc these word is invalid and there is no sense).

在Java中(null)是有效的,但是如果我们写(NULL,0,Null)等,则这些单词无效,没有任何意义。

Example:

例:

class NullCaseSensitive{
public static void main(String[] args) throws Exception{
/*We are assigning null in str1 and it will execute without any error*/String str1 = null;System.out.println("The value of str1 is "+str1); /* We are assigning Null in str2 and NULL in str3     and it will give compile time error because     Null and NULL is invalid in java*//*String str2 = Null;System.out.println("The value of str2 is "+str2);String str3 = NULL;System.out.println("The value of str3 is "+str3);*/}}

Output

输出量

E:\Programs>javac NullCaseSensitive.javaE:\Programs>java NullCaseSensitiveThe value of str1 is null

Case 2: We know that Reference variable hold null by default

情况2:我们知道Reference变量默认为null

  • In Java, the Integer reference variable holds null value by default at the time of object instantiation or in other words if we don't assign any value from our end at the time of object instantiation.

    在Java中,默认情况下,Integer参考变量在对象实例化时保留null值,换句话说,如果我们在对象实例化时未从结尾分配任何值,则默认为null。

  • In Java, String reference hold null by default at the time of object instantiation if we don't assign any other value at the time of object instantiation from our end.

    在Java中,如果我们从结束起就没有在对象实例化时分配任何其他值,则对象实例化时String引用默认情况下为null。

  • In Java, Object reference hold null by default at the time of object instantiation if we don't assign any other value at the time of object instantiation from our end.

    在Java中,如果在对象实例化开始时不分配任何其他值,则对象实例化时对象引用默认为null。

Example:

例:

class ReferenceVariable {
// Declaring Reference Variable String str; Object obj; Integer in ;}class Main {
public static void main(String[] args) throws Exception {
ReferenceVariable rv = new ReferenceVariable(); System.out.println("The default value of the Object reference is " + rv.obj); System.out.println("The default value of the String reference is " + rv.str); System.out.println("The default value of the Integer reference is " + rv.in); }}

Output

输出量

The default value of the Object reference is nullThe default value of the String reference is nullThe default value of the Integer reference is null

Case 3: If we assign null to primitive data type then we will get a compile-time error

情况3:如果将null赋给原始数据类型,则将获得编译时错误

Example:

例:

class AssignNullToPrimitive {
public static void main(String[] args) {
char ch = null; int i = null; /* We will get error here because we   can'’'t null to primitive datatype*/ System.out.println("The value of the char is " + ch); System.out.println("The value of the int is " + i); }}

Output

输出量

E:\Programs>javac AssignNullToPrimitive.javaAssignNullToPrimitive.java:5: error: incompatible typeschar ch = null;        ^  required: char  found:    
AssignNullToPrimitive.java:6: error: incompatible typesint i = null; ^ required: int found:
2 errors

Case 4: If we check whether an object is an instance of a class, interface, etc. it returns true if an object is not an instance of null (i.e the value of the expression is not null)else return false

情况4:如果我们检查对象是否是类,接口等的实例,则如果对象不是null的实例(即表达式的值不为null),则返回true,否则返回false

Example:

例:

class CheckObjectInstanceOf {
public static void main(String[] args) throws Exception {
String str = null; Double d = null; Float f = 10.0f; System.out.println("Is str is an instanceof String " + (str instanceof String)); System.out.println("Is f is an instanceof Float " + (f instanceof Float)); System.out.println("Is d is an instanceof Double " + (d instanceof Double)); }}

Output

输出量

Is str is an instanceof String falseIs f is an instanceof Float trueIs d is an instanceof Double false

翻译自:

转载地址:http://ffxzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-01 常用的服务间调用方式讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-03 高级篇幅之Ribbon负载均衡源码分析实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-02 Netflix开源组件断路器
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-01分布式核心知识之熔断、降级
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
查看>>