面向?qū)ο蟪绦蛟O(shè)計(jì) java 講義 Pointers_Notes資料_第1頁(yè)
面向?qū)ο蟪绦蛟O(shè)計(jì) java 講義 Pointers_Notes資料_第2頁(yè)
面向?qū)ο蟪绦蛟O(shè)計(jì) java 講義 Pointers_Notes資料_第3頁(yè)
面向?qū)ο蟪绦蛟O(shè)計(jì) java 講義 Pointers_Notes資料_第4頁(yè)
面向?qū)ο蟪绦蛟O(shè)計(jì) java 講義 Pointers_Notes資料_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、COMP 110/410Prasun Dewan Ó Copyright Prasun Dewan, 2009. 8. Memory Representation of Primitive Values and ObjectsBy now, we have seen how to assign primitive and object values to primitive and object variables. In this chapter, we will focus a bit more in-depth on such assignments. In particula

2、r, we will study the difference between the way these two kinds of values are stored in memory, and the implication this difference has for the assignment statement.Storing Primitive Values and VariablesConsider how the assignment:int i = 5;is processed by the computer. From a programmers point of v

3、iew, of course, the variable i gets assigned the value 5. However, let us look at under the hood and see what Java exactly does when processing this statement.Java creates an integer value, 5, and stores it in a memory block. A memory block is simply a set of contiguous memory cells that store some

4、program value. It also creates a memory block for the variable i. When the assignment statement is executed, the contents of the value block are copied into the variable block.The two blocks have the same size because the value and the variable have the same type. As a result, the value “fits” exact

5、ly in the variable. The size of the block for an integer value is 1 memory word or 32 bits, as we saw earlier. Figure 1 illustrates this discussion.The statement:double d = 5.5is processed similarly except that Java manipulated blocks of 2 words instead of 1 word, because the size of a double is 2 w

6、ords. Assignment of one variable to another is handled similarly:double e = d;Figure 1. Storing integer values and variables in memoryFigure 2. Storing double values and variables in memoryThe contents of the RHS variable are copied into the block allocated for the LHS variable. Figure 2 illustrates

7、 this discussion. Each memory block is identified by its memory address, which is listed on its left in the figure. While we think in terms of high-level specifiers such as i and 5, the processor, in fact, works in terms of these addresses. The compiler converts these names to addresses, so that the

8、 human and processor can speak different languages. It is for this reason that a compiler is also called a translator.In summary:· values and variables of same type take same amount of fixed storage. · the storage consists of one or more consecutive memory words that together form a memory

9、 block. · values and variables of different types may take different storage.Figure 3. Storing integer values and variables in memoryStoring Object Values and VariablesWe cannot apply these rules directly to objects. The reason is that, as we saw before, instances of the same class can have dif

10、ferent physical structures, which, in turn, means they occupy different sized memory blocks. For example, as we saw earlier, instances of AnAnotherLine can have different physical structures as their location instance variable can be assigned ACartesianPoint or APolarPoint. If a variable and value o

11、f an object type occupied the same amount of space, then what should we do if the variable holding some instance, i1, of a class, another instance, i2, of the class that is of a different size? One solution is to move the variable to a different area of memory. This means, given a variable, we would

12、 have to search some table to determine its location, which makes variable accesses very time inefficient. Another alternative is to allocate the variable the maximum sized object that can assigned to it. However, this approach is space inefficient in fact, when you study recursive structures, you w

13、ill see that this approach is not even feasible as there may not be any limits on the maximum-sized structure that can be assigned to a variable.A compromise solution is to mix elements of the above two solutions. As in the second solution, we will allocate to an object variable a fixed-size memory

14、block. However, this storage area will be very small exactly one memory word. As in the first solution, we will incur some runtime overhead in accessing an object variable. However, this overhead will be smaller than the search overhead of the first solution. In our solution, the memory block associ

15、ated with an object does not contain a copy of the value assigned to it. Instead, it contains a “pointer” to the value.To illustrate and explain in detail, consider again the ACartesianPoint class we saw before:public class ACartesianPoint implements Point int x,y;public ACartesianPoint(int initX, i

16、nt initY) x = initX;y = initY;Consider the following statement in which a new instance of ACartesianPoint is created and stored in a variable of type Point:Point p1 = new ACartesianPoint(50,100);As before, both values and the variables are allocated memory. However, each assignment copies into the v

17、ariables block, not the contents of the value block, but instead its address. All Java addresses are 1 word long, so all object variables are allocated a 1-word block, regardless of their types, which was not the case with the primitive variables. For example, double variable, d, and integer variabl

18、e, i, we saw above were of different sizes.All objects, however, are not of the same size. When a new object is created, a composite memory block consisting of a series of consecutive blocks, one for each instance variable of the object, is created. Thus, since ACartesianPoint has two int instance v

19、ariables, a block consisting of two integer variables is created. The sizes of two objects of different types can be different depending on the number and the types of their instances variables. Figure 3 illustrates the above discussion.Since an object variable stores addresses, it also called a poi

20、nter variable or reference variable, and the address stored in it a pointer or reference. Variable reference is more complicated when the variable is a pointer variable. Consider:Java accesses memory at the address associated with i, and uses the value stored in the println. In contrast, consider:Ja

21、va accesses memory at the address associated with p1, finds another address there, and then uses this address to find the integer value. Thus, we do not go directly from a variable address to its value, but instead, indirectly using the value address or pointer. In some languages, the programmer is

22、responsible for doing the indirection or dereferencing. For instance, in Pascal, given an integer pointer variable p1, we need to type: p1to refer to the value to which it refers. Thus, the equivalent statement in Pascal would be:writeln(p1)Java, however, automatically does the dereferencing for us.

23、 In fact, we cannot directly access the address stored in it. Thus, we are not even aware that the variable is a pointer variable. Sometimes, the term pointer is used for a variable that must be explicitly dereferenced and reference for a variable that is automatically dereferenced. For this reason,

24、 some people say that Java has no pointer variables. However, we will use these two terms interchangeably. Dereferencing increases memory access time this is the reason why we do not use it for primitive variables, which explains why primitives and objects are treated differently.The special value,

25、null, we saw before, can be assigned to a pointer variable:Point p2 = null;In fact, if we do not initialize a pointer variable, this is the value stored in its memory block. It denotes the absence of a legal object assigned to the variable. This value is not itself a legal object, and does not have a class. If we try to access a member of this value:null.toString();we get a NullPointerException, which some of you may

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論