/
Reference, primitive, call by XXX Reference, primitive, call by XXX

Reference, primitive, call by XXX - PowerPoint Presentation

2coolprecise
2coolprecise . @2coolprecise
Follow
343 views
Uploaded On 2020-07-02

Reference, primitive, call by XXX - PPT Presentation

必也正名乎 誌謝 部份文字取於前輩 TAHO 的文章 In java there are two types Primitive types and Reference types 這兩種 Type 限制了 variable 所能握有 ID: 792399

call reference variable primitive reference call primitive variable type object java hold str types string person null types

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Reference, primitive, call by XXX" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

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

誌謝

:

部份文字取於前輩

TAHO

的文章

Slide2

In java, there are two types…

Primitive types

and

Reference types

這兩種

Type

限制了

variable

所能握有

(hold)

value

種類

Slide3

Recall that…

heap

objRef1=0x3200

objRef3=0x3288

objRef2=0x4650

(

Object instances inside)

intValue

=3

booleanValue

=true

(Reference type variables)

(Primitive type variables)

Slide4

What is “value”?

所謂的

value

當然就是指

"

",也就是資料的直接內容但這個”值

”所代表的涵意,如何解釋?當 x = 3是一個數值

(ex: int x = 3)還是一個記憶體位置 (ex: 0x3000)取決於我們怎麼去解釋它,這就是

primitive types與reference types的主要差異

Slide5

Primitive types

譯為

基本型別

這種型別的

value 被稱之為 primitive value這種 value

所代表的就是一個數量,一個大小,或是一種純量,並不是用來代表其他任何東西的

Slide6

Reference types

譯為

參考型別

這種型別的

value 被稱為 reference value這個

reference value 並非是實際上應用的東西,它不是一個數量它是一個參考 (object reference),一個用來

”指向物件”的參考根據此”reference value”來找到

heap中的物件實體但是 reference value

並不等於是物件!!由於reference value太長,很多人簡稱

”reference”造成誤解,因為C++中的reference與

java中的reference定義並不相同

!!

Slide7

Type, variable, value

variable hold

的是

primitive value

時,我們稱這個

variable 是 primitive type 的變數反之 當

variable hold 的是 reference value 時,我們稱這個 variable 是 reference type

的變數這句話是說 它是個 "參考型別的變數"

,而非 "它是個參考"

Slide8

Example

int

i

= 1;

String

str = "test";其中 i

跟 str 是 "變數“

“test” 是個字串的生成表示式,會生成 String 物件1 是個整數的直譯字(literal)

int 跟 String 是 type我們是看不到

value 的,他們是被 hold 在變數中的平時我們說

”str 物件”,只是簡稱,正確應說

”str reference value所指向的物件”

Slide9

所謂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中而已

Slide10

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 等名詞,因為在不同的程式語言有不同涵意

Slide11

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?問題的關鍵在於”你到底在對誰動作

Slide12

所以我們正名

Reference

一詞,在

java

中為

”reference value”

之簡稱,與C++中完全無關

Alias等同於reference (In C++)

,所以我們也不用既然如此,我們就不用C++的call by reference, call by value來解釋

Java逼免造成混亂我們使用Java Language Spec (JLS) 4.1節中的

官方定義