1 / 16

Reference, primitive, call by XXX 必也正名乎

Reference, primitive, call by XXX 必也正名乎. 誌謝 : 部份文字取於前輩 TAHO 的文章 CS340100, NTHU Yoshi. Outline. Primitive and reference types Call by value/reference Wrapper classes Equality. In java, there are two types…. Primitive types and Reference types

lynda
Download Presentation

Reference, primitive, call by XXX 必也正名乎

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reference, primitive, call by XXX 必也正名乎 誌謝: 部份文字取於前輩TAHO的文章 CS340100, NTHU Yoshi

  2. Outline • Primitive and reference types • Call by value/reference • Wrapper classes • Equality

  3. In java, there are two types… • Primitive types and Reference types • 這兩種 Type 限制了 variable 所能握有(hold) 的 value 種類

  4. Recall that… objRef1=0x3200 objRef2=0x4650 heap objRef3=0x3288 (Reference type variables) intValue=3 booleanValue=true (Primitive type variables) (Object instances inside)

  5. What is “value”? • 所謂的 value 當然就是指 "值",也就是資料的直接內容 • 但這個”值”所代表的涵意,如何解釋? • 當x = 3 • 是一個數值 (ex: int x = 3) • 還是一個記憶體位置 (ex: 0x3000) • 取決於我們怎麼去解釋它,這就是primitive types與reference types的主要差異

  6. Primitive types • 譯為”基本型別” • 這種型別的 value 被稱之為 primitive value • 這種 value 所代表的就是一個數量,一個大小,或是一種純量,並不是用來代表其他任何東西的 • There are 8 types which are primitive types • byte, short, int, float, double, boolean, long, char

  7. Reference types • 譯為”參考型別” • 這種型別的 value 被稱為 reference value • 這個 reference value 並非是實際上應用的東西,它不是一個數量 • 它是一個參考 (object reference),一個用來”指向物件”的參考 • 根據此”reference value”來找到heap中的物件實體 • 但是 reference value 並不等於是物件!! • 由於reference value太長,很多人簡稱”reference” • 造成誤解,因為C++中的reference與java中的reference定義並不相同!!

  8. Type, variable, value • 當 variable hold 的是 primitive value 時,我們稱這個 variable 是 primitive type 的變數 • 反之 當 variable hold 的是 reference value 時,我們稱這個 variable 是 reference type 的變數 • 這句話是說 它是個 "參考型別的變數",而非"它是個參考"

  9. Example • inti = 1; • String str = "test"; • 其中 i跟 str是 "變數“ • “test” 是個字串的生成表示式,會生成 String 物件 • 1 是個整數的直譯字(literal) • int跟 String 是 type • 我們是看不到 value 的,他們是被 hold 在變數中的 • 平時我們說”str物件”,只是簡稱,正確應說”str reference value所指向的物件”

  10. 所謂call by value void m1 (Person p) { p = null; } //some other place Person personObj = new Person(“John”); m1(personObj); • 把personObj中所存的reference value (一個數值),當做參數傳給m1 • 在m1當中,把p變成null,並不會對實體物件有動作,只是把p所儲存的值清掉 • 影響的範圍,只在m1中而已

  11. Discussion • Java是call by value還是call by reference • 對primitive types而言,call by value • 對reference types而言,call by “reference” value • 其實也是 call by value,只是這個value是記憶體位置的值 • 所以,java只有call by value (官方文件也是如此說) • 與C 語言同理 • 逼免造成混亂,最好不要誤用 reference, alias 等名詞,因為在不同的程式語言有不同涵意

  12. In C++ Primer • "A reference is an Alias" • "Because a reference is just another name for the object to which it is bound, all operations on a reference are actually operations on the underlying object to which the reference is bound." • (此處object代表東西,不是我們一般Java認知的物件) • X = NULL,你是把X裡頭的值變NULL,還是把X所指向的物件變成NULL? • 問題的關鍵在於”你到底在對誰動作”

  13. 所以我們正名 • Reference一詞,在java中為”reference value”之簡稱,與C++中完全無關 • Alias等同於reference (In C++),所以我們也不用 • 既然如此,我們就不用C++的call by reference, call by value來解釋Java • 逼免造成混亂 • 我們使用Java Language Spec (JLS) 4.1節中的官方定義

  14. Wrapper Classes • There are eight classes named Integer, Long, Double, Float, Boolean, Short, Byte, Char, respectively • What are the differences between the primitive types and them?

  15. Let’s see the javadoc • The Integer class wraps a value of the primitive type int in an object • An object of type Integer contains a single field whose type is int. • In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int

  16. References • http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html

More Related