你好,阿米戈!我想告訴你變量的內部結構。如您所知,每個變量都與存儲其值的內存區域相關聯。

“是的,你上次跟我說過。”

“太好了。你還記得就好了。那我繼續說。”

“所有復合類型都由更簡單的類型組成。而它們又由更簡單的類型組成。直到最後,我們最終得到原始類型,無法進一步簡化。這就是它們所謂的 -原始類型。對於例如,int是原始類型,但String是複合類型,將其數據存儲為字符表(其中每個字符都是原始類型 char)。”

“很有趣。繼續。”

“複合類型是由簡單的組合而成的。我們稱這種類型為類當我們在程序中定義一個新類時,我們聲明了一個新的複合數據類型。它的數據要么是其他復合類型,要么是原始類型。”

Java代碼 描述
public class Person
{
   String name;
   int age;
}
聲明了一個新的複合類型 – Person
它的數據存儲在String(複合類型)變量nameint(原始類型)變量中age
public class Rectangle
{
   int x, y, width, height;
}
聲明了一個新的複合類型 – Rectangle。它由四個(原始類型)變量
組成。int
public class Cat
{
   Person owner;
   Rectangle territory;
   int age;
   String name;
}
聲明了一個新的複合類型 – Cat。它有以下變量:
owner,複合類型Person
territory,複合類型Rectangle
age,原始類型int
name,複合類型String

“目前,一切都很清楚,無論它看起來多麼奇怪。”

“大(複合)類型包含許多小(原始)類型。這就是為什麼這些類型的對象佔用大量內存 - 比原始類型的變量更多。有時更多。使用此類變量執行賦值操作過去需要很長時間時間和需要復制大段內存。這就是為什麼複合類型的變量不存儲對象本身,而只是對它的引用,即它的四字節地址。這足以尋址此類對像中的數據。 Java 機器處理所有相關的複雜性。”

“我一點都不明白。”

“我們之前說過,變量就像一個盒子,如果你想在裡面存13這個數字,你可以把13寫在一張紙上,然後放進盒子裡。”

“但想像一下,你需要在盒子裡存放更大的東西(變量)。例如,狗、汽車或你的鄰居。與其試圖將不可推入的東西推入盒子,你可以做一些更簡單的事情:使用照片狗的名字而不是真正的狗,車牌而不是真正的汽車,或者你鄰居的電話號碼而不是你鄰居的電話號碼。”

“我們拿一張紙,寫下鄰居的電話號碼,這就像對一個物體的參考。如果我們把寫有鄰居電話的紙複製下來,放在幾個盒子裡,現在參考就多了對你的鄰居。但是,和以前一樣,你仍然只有一個鄰居。這是有道理的,不是嗎?

“以這種方式存儲數據的一個重要特徵是您可以對單個對像有多個引用

“真有趣!我快明白了。請再告訴我一次——如果我將一個複合類型的變量賦值給另一個相同複合類型的變量,會發生什麼?”

“那麼這兩個變量就會存儲相同的地址。也就是說,如果你改變了一個變量引用的對象的數據,你改變了另一個變量引用的數據。兩個變量引用同一個對象。當然,可能有很多其他變量也存儲對它的引用。”

“如果復合(引用/類)類型的變量不持有對對象的引用,它們會做什麼?這有可能嗎?”

“是的,阿米戈。你的問題比我早。這是可能的。如果引用(複合)類型的變量不存儲對對象的引用,那麼它存儲所謂的“空” reference'。基本上,這意味著它引用了一個地址為 0 的對象。但是,Java 機器從不使用該地址創建對象,因此它始終知道如果引用變量包含 0,則它沒有指向任何對象”

Java代碼 描述
String s;
String s = null;
等效語句。
Person person;
person = new Person();
person = null;
我們創建一個值為 null 的 person 變量。
我們將新創建的 Person 對象的地址分配給它。
我們將 null 分配給變量。
Cat cat = new Cat();
cat.owner = new Person();
cat.owner.name = "God";
我們創建一個 Cat 對象並將其地址存儲在變量 cat 中;cat.owner 等於 null。
我們將 cat.owner 設置為新創建的 Person 對象的地址。
cat.owner.name 仍然等於 null。
我們將 cat.owner.name 設置為“God”

“我沒理解錯嗎?變量分為兩種類型:原始類型和引用類型。原始類型直接存儲值,而引用類型存儲對對象的引用。原始類型包括 int、char、boolean 和許多其他類型。參考類型包括其他一切。我們使用類來創建它們。”

“你完全正確,我的孩子。”

“所以,你說你已經明白了一切。這裡有一些任務可以幫助你鞏固你的知識。”

1
任務
Java Syntax,  等級 2課堂 2
上鎖
Calculate the circumference of a circle
Well, we have a plan. Let's implement it. First, implement a method that calculates the circumference of a circle. To do this, you need to write the correct formula in the method and specify the arguments. But what about pi, which we know is 3.141592... and so on to infinity? We'll keep things simple: let pi = 3.14.
7
任務
Java Syntax,  等級 2課堂 2
上鎖
Our first converter!
You've probably used electronic converters or programs on many occasions to convert a quantity of one unit to a quantity of another unit. For example, dollars to pounds, or kilometers to miles. Now it's time for us to write such a tool. Specifically, we'll write a program that converts from degrees Celsius to degrees Fahrenheit.
3
任務
Java Syntax,  等級 2課堂 2
上鎖
Where does a Person come from?
In Java, people come from the same place that other classes come from: from the programmer's head. It is important for the creator of a class to think through what is important for the class and what is not. If he or she does, then the class will make sense and be useful. Let's begin. Let's create a Person class that allows our Person to have a name, age, weight, and... money. And then we'll create an object.
3
任務
Java Syntax,  等級 2課堂 2
上鎖
Family relations
A programmer can create a man and a woman with a couple of deft movements of his or her fingers. Easy-peasy: we'll write the appropriate classes and create objects. Let's work on a married couple: we'll create Man and Woman objects, and then save a reference to the Woman in man.wife, and a reference to the Man in woman.husband. You see? You don't even need a marriage license.