官术网_书友最值得收藏!

3.4 Reference Types

We have now looked at 8 of the 9 data types in Java. The final type is what is known as a reference type. Basically, anything that is not a primitive (an int, a float, etc.) is a reference. That means that arrays are references, as are instances of classes. The notion of a reference is similar to that of a pointer in C++. Let’s say you have created a class called MyClass. When you want to create an instance of that class, you’ll probably want to name it so that you can work with it like any variable. In doing so, the variable that you create does not have the object you’ve created in it. Rather, it has a reference to that object in it. Let’s look at an example and see if we can make this make sense:

1: // 創建一個新的對象
2: MyClass anInstanceOfMyClass = new MyClass();

The variable anInstanceOfMyClass does not really contain an instance of MyClass. Rather, it contains the memory address where that instance really exists. The notion of a reference is very important in Java when it comes to multiple accesses to the same object and parameter passing. For now, it’s sufficient enough to know that when you declare an array or the instance of a class, the variable you store it in is a reference, rather than the object itself.

anInstanceOfMyClass引用包含著被創建對象的內存首地址。

主站蜘蛛池模板: 鄂温| 两当县| 墨江| 锡林浩特市| 蓝山县| 禹州市| 鄂尔多斯市| 永仁县| 安化县| 桃园市| 常宁市| 庄浪县| 尚志市| 红河县| 昭觉县| 辛集市| 永仁县| 台北市| 广昌县| 台中县| 红桥区| 淄博市| 彭州市| 商南县| 邯郸县| 松原市| 交口县| 颍上县| 来安县| 南郑县| 元江| 基隆市| 祥云县| 西青区| 汉阴县| 南溪县| 海南省| 荣成市| 扶沟县| 平潭县| 上杭县|