面向对象程序设计 java 讲义 Pointers_Notes资料_第1页
面向对象程序设计 java 讲义 Pointers_Notes资料_第2页
面向对象程序设计 java 讲义 Pointers_Notes资料_第3页
面向对象程序设计 java 讲义 Pointers_Notes资料_第4页
面向对象程序设计 java 讲义 Pointers_Notes资料_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论