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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377138.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377138.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377138.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《MySQL——分區表小記》

分區表的組織形式 以年份為分割方式&#xff0c;對表進行分割&#xff1a; CREATE TABLE t (ftime datetime NOT NULL,c int(11) DEFAULT NULL,KEY (ftime) ) ENGINEInnoDB DEFAULT CHARSETlatin1 PARTITION BY RANGE (YEAR(ftime)) (PARTITION p_2017 VALUES LESS THAN (201…

實戰Windows下安裝boost

Boost大部分組件無需編譯可直接包含頭文件使用&#xff0c;但還有一些庫需要編譯成靜態庫或動態庫才能使用。可使用下文將提到的bjam工具&#xff1a;bjam --show-libraries 查看必須編譯才能使用的庫。 編譯安裝環境&#xff1a;Win7&#xff0c;VS2008(msvc-9.0) 1. 下載boos…

postgresq dur_DUR的完整形式是什么?

postgresq dur杜爾(DUR)&#xff1a;您還記得嗎&#xff1f; (DUR?: Do You Remember?) DUR? is an abbreviation of "Do You Remember?". DUR&#xff1f; 是“您還記得嗎&#xff1f;”的縮寫。 。 It is an expression, which is commonly used in messaging…

gsettings-desktop-schemas : 破壞: mutter (< 3.31.4) 但是 3.28.4-0ubuntu18.04.2 正要被安裝解決方案

完整報錯&#xff1a; dyydyy-Lenovo-ThinkBook-14-IIL:~$ sudo apt install build-essential 正在讀取軟件包列表... 完成 正在分析軟件包的依賴關系樹 正在讀取狀態信息... 完成 有一些軟件包無法被安裝。如果您用的是 unstable 發行版&#xff0c;這也許是 因…

程序內存檢測

本文參考自&#xff1a;http://www.cnblogs.com/hebeiDGL/p/3410188.html static System.Windows.Threading.DispatcherTimer dispacherTimer;static string total "DeviceTotalMemory";static string current "ApplicationCurrentMemoryUsage";static s…

動態規劃天天練1

本來很久以前就打算每天練一道動態規劃題的&#xff0c;但每每由于作業太多而中斷&#xff0c;現在終于停課了......廢話不多說&#xff0c;第一道題就給了我迎頭一棒&#xff0c;不僅想了很久&#xff0c;連題解都看了很久。。。水平相當不足啊啊&#xff0c;不多說廢話&#…

AAS的完整形式是什么?

AAS&#xff1a;活著和微笑 (AAS: Alive And Smiling) AAS is an abbreviation of "Alive And Smiling". AAS是“活著和微笑”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Y…

《MySQL 8.0.22執行器源碼分析(1)——execute iterator一些記錄》

目錄一條語句的函數調用棧順序8.0使用迭代器模式改進executorint *handler*::ha_rnd_next(*uchar* **buf*)int *TableScanIterator*::Read()int FilterIterator :: Read&#xff08;&#xff09;int HashJoinIterator::Read()int NestedLoopIterator :: Read&#xff08;&#…

關于autoupgader的狗屎問題

由于win7和xp的權限問題&#xff0c;導致這個自動升級玩意不正常。這個狗屎問題很簡單&#xff0c;把exe文件的兼容性設定該一下。真是氣死灑家了。轉載于:https://www.cnblogs.com/usegear/p/3679097.html

strcspn函數

函數原型&#xff1a;extern int strcspn(char *str1,char *str2) 參數說明&#xff1a;str1為參照字符串&#xff0c;即str2中每個字符分別與str1中的每個字符比較。 所在庫名&#xff1a;#include <string.h> 函數功能&#xff1a;以str1為參照&#xff0c…

c語言 sqlite_SQLite與C語言

c語言 sqliteSQLite數據庫簡介 (Introduction to SQLite database) SQLite is a relational database; it is used for embedded devices. Now a day it is using worldwide for different embedded devices. SQLite是一個關系數據庫。 它用于嵌入式設備。 如今&#xff0c;它已…

《MySQL 8.0.22執行器源碼分析(2)解讀函數 ExecuteIteratorQuery》

函數代碼 bool SELECT_LEX_UNIT::ExecuteIteratorQuery(THD *thd) {THD_STAGE_INFO(thd, stage_executing);DEBUG_SYNC(thd, "before_join_exec");Opt_trace_context *const trace &thd->opt_trace;Opt_trace_object trace_wrapper(trace);Opt_trace_object…

C語言,如何產生隨機數

1. 基本函數 在C語言中取隨機數所需要的函數是: int rand(void);void srand (unsigned int n); rand()函數和srand()函數被聲明在頭文件stdlib.h中,所以要使用這兩個函數必須包含該頭文件: #include <stdlib.h> 2. 使用方法 rand()函數返回0到RAND_MAX之間的偽隨機數(pse…

MongoDB源碼概述——內存管理和存儲引擎

數據存儲&#xff1a; 之前在介紹Journal的時候有說到為什么MongoDB會先把數據放入內存&#xff0c;而不是直接持久化到數據庫存儲文件&#xff0c;這與MongoDB對數據庫記錄文件的存儲管理操作有關。MongoDB采用操作系統底層提供的內存文件映射&#xff08;MMap&#xff09;的方…

OBTW的完整形式是什么?

OBTW&#xff1a;哦&#xff0c;順便說一下 (OBTW: Oh, By The Way) OBTW is an abbreviation of "Oh, By The Way". OBTW是“哦&#xff0c;順便說一下”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media network…

SharePoint 2010 Form Authentication (SQL) based on existing database

博客地址 http://blog.csdn.net/foxdaveSharePoint 2010 表單認證&#xff0c;基于現有數據庫的用戶信息表本文主要描述本人配置過程中涉及到的步驟&#xff0c;僅作為參考&#xff0c;不要僅限于此步驟。另外本文通俗易懂&#xff0c;適合大眾口味兒。I. 開啟并配置基于聲明的…

《MySQL 8.0.22執行器源碼分析(3.1)關于RowIterator》

目錄RowIteratorInit()Read()SetNullRowFlag()UnlockRow()StartPSIBatchMode()EndPSIBatchModeIfStarted()real_iterator()RowIterator 使用選定的訪問方法讀取單個表的上下文&#xff1a;索引讀取&#xff0c;掃描等&#xff0c;緩存的使用等。 它主要是用作接口&#xff0c;但…

hdu 2432法里數列

這題本來完全沒思路的&#xff0c;后來想一想&#xff0c;要不打個表找找規律吧。于是打了個表&#xff0c;真找到規律了。。。 打表的代碼如下&#xff1a; int n; void dfs(int x1, int y1, int x2, int y2) {if (y1 y2 < n) {dfs(x1, y1, x1 x2, y1 y2);printf("…

python學習筆記四——數據類型

1.數字類型&#xff1a; 2.字符串類型&#xff1a; 切片&#xff1a;a[m:n:s] m:起始值 n:結束值&#xff08;不包括n&#xff09; s:步長&#xff0c;負數表示從后向前取值 3.序列&#xff1a;列表&#xff0c;元組和字符串都是序列 序列的兩個主要特點是索引操作符和切片…

小狐貍ChatGPT系統 不同老版本升級至新版數據庫結構同步教程

最新版2.6.7下載&#xff1a;https://download.csdn.net/download/mo3408/88656497 小狐貍GPT付費體驗系統如何升級&#xff0c;該系統更新比較頻繁&#xff0c;也造成了特別有用戶數據情況下升級時麻煩&#xff0c;特別針對會員關心的問題出一篇操作教程&#xff0c;本次教程…