Java中的null是什么?

  • 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.java

E:\Programs>java NullCaseSensitive
The 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 null
The default value of the String reference is null
The 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.java
AssignNullToPrimitive.java:5: error: incompatible types
char ch = null;
        ^
  required: char
  found:    <null>
AssignNullToPrimitive.java:6: error: incompatible types
int i = null;
        ^
  required: int
  found:    <null>
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 false
Is f is an instanceof Float true
Is d is an instanceof Double false


翻译自: https://www.includehelp.com/java/what-is-null-in-java.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值