YTGET

YouTube · Facebook · X · TikTok · Instagram · Threads

用兩個物件遞增遞減實測證明實體變數各自獨立

YouTube 影片筆記

用兩個物件遞增遞減實測證明實體變數各自獨立

原片標題:Core Java in Hindi | Role of instance variables at object creation | Session-8

原片說明
Core Java in Hindi | Role of instance variables at object creation | Session-8 | by Mr. Ravi Shankar Welcome to the Core Java Tutorial Series! In this video, we introduce the essential concepts of Core Java and guide you through building a strong foundation in Java programming. Perfect for beginners and those refreshing their Java skills! 🔹 Course Details : • Topic: Core Java Course Overview | NareshIT For Free Master Class Demos, visit: https://nareshit.com/courses/core-java-online-training Register for Online Training: https://goo.gl/r6kJbB 🔹 Join Our Community For Latest Updates: ► Subscribe to Our Channel: https://goo.gl/q9ozyG ► Like us on Facebook: https://www.facebook.com/nareshit/ ► Follow us on Twitter: https://twitter.com/nareshitech ► Follow us on Linkedin: https://in.linkedin.com/company/naresh-i-technologies ► Follow us on Instagram: https://www.instagram.com/nareshitech/ ► Follow us on Telegram: https://t.me/nareshit • 📚 Learn Core Java with NareshIT and take your Java development expertise to the next level. Don’t forget to like, comment, and subscribe for more tutorials and updates! #corejava #javatutorial #javaprogramming #learnjava #nareshit #javaforbeginners #javacourse #programmingbasics #javadevelopment #javaskills #javalearning #enterprisejava #javatraining #javaseries #javacode #javaframeworks #javatips #springframework #javaconcepts #codingwithjava *💡 About NareshIT: "Naresh IT is having 14+ years of experience in software training industry and the best Software Training Institute for online training, classroom training, weekend training, corporate training of Hadoop, Salesforce, AWS, DevOps, Spark, Data Science, Python, Tableau, RPA , Java, C#.NET, ASP.NET, Oracle, Testing Tools, Silver light, Linq, SQL Server, Selenium, Android, iPhone, C Language, C++, PHP and Digital Marketing in USA, Hyderabad, Chennai and Vijayawada, Bangalore India which provides online training across all the locations ________________________________________________________________________________________________________________ 💡 Our Online Training Features: • 🎈 Training with Real-Time Experts • 🎈 Industry-Specific Scenarios • 🎈 Flexible Timings • 🎈 Soft Copy of Material • 🎈 Recorded Videos of Each Session ________________________________________________________________________________________________________________ Please write back to us at 📧 [email protected]/ 📧 [email protected] or Call us at the USA: ☎+1404-232-9879 or India: ☎ +918179191999

Ravi Shankar 用一個 `int x = 10` 就把「每個物件都有自己的副本」講清楚了,這其實是 Java 裡很容易被跳過、但沒搞懂就會到處踩雷的觀念。這堂課不談語法花招,只反覆證明一件事:`new` 出來的物件,實體變數是各走各的。看完你會知道為什麼 `t1.x` 加一、`t2.x` 減一,兩邊互不影響。

在 Eclipse 寫下 int x = 10
在 Eclipse 寫下 int x = 1003:46

影片時間軸

  1. 00:53說明用 new 建立物件時,一定配置在堆記憶體的新位置
  2. 02:23切到 Eclipse,開新套件 com.ravi.instancecopies 並建立 Test 類別
  3. 03:40在類別裡定義實體變數 int x = 10
  4. 03:59建立 t1 與 t2 兩個 Test 物件
  5. 04:47分別印出 t1.x 與 t2.x,執行結果都是 10
  6. 05:55畫圖說明兩個物件各自落在不同記憶體位址、各有一份 x
  7. 09:40改成 ++t1.x 與 --t2.x,輸出變成 11 與 9,證明副本真的分開
  8. 12:15順帶把 x 改成 static 再跑一次,結果變成 10 和 10,預告靜態變數只有一份
畫出兩個物件各自的記憶體位址
畫出兩個物件各自的記憶體位址05:55

用兩個物件就能證明的事

他一開始先讓程式只印 `t1.x` 和 `t2.x`,兩個都是 10,老實說這段沒什麼說服力,因為就算共用同一份也印得出 10。真正的關鍵在他後面補的那一步:對 `t1.x` 做遞增、對 `t2.x` 做遞減,輸出變成 11 和 9。這才是整支影片的重點,用一個會改變數值的動作,把「兩份獨立副本」從口頭說明變成看得見的結果。

改成 ++t1.x 與 --t2.x 後輸出 11 和 9
改成 ++t1.x 與 --t2.x 後輸出 11 和 909:40

畫圖那一段才是精華

他在紙上把兩個物件分別標成 2000x 和 3000x 兩個位址,各自掛一個 x,再對應回 t1 和 t2 兩個參考變數。這段圖解比前面念定義清楚太多,也解釋了為什麼「建立兩個物件等於建立兩份實體變數」。如果只想看一段,就看這裡。

兩個物件分別落在 2000x 與 3000x
兩個物件分別落在 2000x 與 3000x07:50

他自己先暴雷 static

影片後段他把 x 加上 static 再跑一次,輸出回到 10 和 10,接著說「static 只有一份,但這個我們之後再談」。這段其實有點多餘,因為他沒有解釋為什麼遞增遞減會互相抵銷,只丟了結論就收手。當成預告可以,但別期待在這裡聽懂 static。

宣告 static 後輸出變成 10 和 10
宣告 static 後輸出變成 10 和 1012:41

值得下載嗎

值得,如果你正在學 Java 而且對「物件各有一份實體變數」還停留在背誦階段,這支的圖解與反證法很省時間。但它終究是系列中的第八堂,前面沒跟到的人建議照他的順序補齊再看。

覺得這篇筆記有幫助嗎?
YTGet 編輯室|看完影片整理的筆記,細節以原片為準。

連線中

影片封面

擷取畫面

輸入時間點,例如 3:20、3.20 或 200

影片皆已含音訊。檔案越大準備時間越久,完成前請保持頁面開啟。