Java中Boolean和boolean的差別是什麼
前言
有人問,Boolean 類型的值不是只有 true 和 false 兩種嗎?為什麼他定義的屬性出現了 null 值?
我們應該先明確一點,boolean 是 Java 的基本資料型別,Boolean 是 Java 的一個類別。 boolean 類型會在「賦零值」階段賦予屬性 false。而 Boolean 是一個類,會在「賦零值」階段為物件賦 null。
如果是靜態屬性,會在類別載入時被賦值。如果是普通類別屬性,會在實例化物件時賦值。這兩點可以了解一下「類別載入機制」和「物件建立過程」。
類別載入機制:
載入:根據類別的全名獲取類別的二進位位元組流,將類別載入記憶體並在堆中產生一個代表這個類別的Class 對象,作為方法區資料的存取入口
驗證:驗證class 檔案中的位元組流是否符合JVM 規範
準備:在方法區中為類別的靜態屬性分配內存,並初始化預設值(boolean 的預設值是false,Boolean 的預設值是null)
解析:將常數池中的符號引用轉換為直接引用,可以理解為物件引用轉成指標
初始化:真正開始執行類別中的程式碼,靜態屬性賦值與靜態區塊
物件實例化過程:
檢查類別是否已經載入(雙親委派)
給物件分配內存空間(指標碰撞)
零值初始化(false / null)
設定物件頭(物件分代年齡等資訊)
執行
方法(屬性初始化,語句區塊和建構方法)
所以說,Boolean只是被載入了,還沒被實例化,在被實例化之前並沒有分配內存,所以是null
接下來我們可以看看Boolean 的屬性和構造方法,了解一下它如何包裝boolean
// final boolean类型的属性,通过构造方法注入值 private final boolean value; // 构造方法 Boolean a = true 实际上就是调用这个方法 public Boolean(boolean value) { this.value = value; } // 构造方法 public Boolean(String s) { this(parseBoolean(s)); }
對於其他的屬性和方法,可以自行查看都比較簡單
關於Boolean 使用過程中有一個風險點,阿里巴巴開發手冊也寫得非常好
針對Boolean與true
單純從源碼角度看不出來那個性能上更加好;大布爾也是初始化了兩個static物件原始碼截圖如下:public class Test { /** * 方法一 * * @return */ public static Boolean A() { return Boolean.TRUE; } /** * 方法二 * * @return */ public static boolean D() { return true; } public static String get() { long i = 0L; long j = 0L; for (int n = 0; n < 100000; n++) { long startTime = System.nanoTime(); D(); long endTime = System.nanoTime(); long booleanTime = endTime - startTime; long start = System.nanoTime(); A(); long end = System.nanoTime(); long booleanca = end - start; if (booleanca > booleanTime) { i = i + 1; } else { j = j + 1; } } return i+" "+j; } public static void main(String[] args) { System.out.println("---100000次的比较结果---->"+get()); } }執行結果如下圖:
以上是Java中Boolean和boolean的差別是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

testthepdfinanotherapptoderineiftheissueiswiththefileoredge.2.enablethebuilt inpdfviewerbyTurningOff“ eflblyopenpenpenpenpenpdffilesexternally”和“ downloadpdffiles” inedgesettings.3.clearbrowsingdatainclorwearbrowsingdataincludingcookiesandcachedcachedfileresteroresoreloresorelorsolesoresolesoresolvereresoreorsolvereresoreolversorelesoresolvererverenn

Importjava.ioandjava.net.SocketforI/Oandsocketcommunication.2.CreateaSocketobjecttoconnecttotheserverusinghostnameandport.3.UsePrintWritertosenddataviaoutputstreamandBufferedReadertoreadserverresponsesfrominputstream.4.Usetry-with-resourcestoautomati

容器化Java應用:創建Dockerfile,使用基礎鏡像如eclipse-temurin:17-jre-alpine,複製JAR文件並定義啟動命令,通過dockerbuild構建鏡像並用dockerrun測試本地運行。 2.推送鏡像到容器註冊表:使用dockertag標記鏡像並推送到DockerHub等註冊表,需先登錄dockerlogin。 3.部署到Kubernetes:編寫deployment.yaml定義Deployment,設置副本數、容器鏡像和資源限制,編寫service.yaml創建

VSCode中可通過快捷鍵快速切換面板與編輯區。要跳轉至左側資源管理器面板,使用Ctrl Shift E(Windows/Linux)或Cmd Shift E(Mac);返回編輯區可用Ctrl `或Esc或Ctrl 1~9。相比鼠標操作,鍵盤快捷鍵更高效且不打斷編碼節奏。其他技巧包括:Ctrl KCtrl E聚焦搜索框,F2重命名文件,Delete刪除文件,Enter打開文件,方向鍵展開/收起文件夾。

runthewindowsupdatetrubloubleshooterviaSettings>更新&安全> is esseShootsoAtomationfixCommonissues.2.ResetWindowSupDateComponentsByStoppingRealatedServices,RenamingTheSoftWaredWaredWaredSoftwaredSistribution andCatroot2Folders,intrestrestartingthertingthertingtherserviceSteStoceTocle

JavaserializationConvertSanObject'SstateIntoAbyTeSteAmForStorageorTransermission,andDeserializationReconstructstheObjectStheObjectFromThstream.1.toenableserialization,aclassMustimustimplementTheSerializableizableface.2.UseObjectObjectObjectObjectOutputputputputputtreamToserialializeanobectizeanobectementeabectenobexpent,savin

AwhileloopinJavarepeatedlyexecutescodeaslongastheconditionistrue;2.Initializeacontrolvariablebeforetheloop;3.Definetheloopconditionusingabooleanexpression;4.Updatethecontrolvariableinsidethelooptopreventinfinitelooping;5.Useexampleslikeprintingnumber

要有效使用Mockito進行Java單元測試,首先需添加Mockito依賴,Maven項目在pom.xml中加入mockito-core依賴,Gradle項目添加testImplementation'org.mockito:mockito-core:5.7.0';接著通過@Mock註解(配合@ExtendWith(MockitoExtension.class))或mock()方法創建模擬對象;然後使用when(...).thenReturn(...)等方式對模擬對象的方法行為進行存根,也可配置異
