3.3 CONSTRUCTING OBJECTS: DIFFERENCES AND SIMILARITIES BETWEEN C AND JAVA


3.3 CONSTRUCTING OBJECTS: DIFFERENCES AND SIMILARITIES BETWEEN C++ AND JAVA

The left column of Table 3.1 shows the two most common ways of constructing a C++ object and the right column the only way a Java object can be constructed. The question now is, which of the two C++ ways for object construction is similar to how an object is constructed in Java?

Table 3.1

C++

Java

Class definition:
    class User { .. };

Class definition:
    class User { .. }

Object construction:    (1)
    User u(.);

 

Object construction:    (2)
    User* p = new User(. );

Object construction:
    User q = new User( . );

Consider first the C++ case: With the constructor invocation labeled as (1), we can think of u itself as the User object. The constructor invocation fills up a block of memory that could be called the u object, as depicted in Figure 3.2. On the other hand, the constructor invocation labeled as (2) in the C++ column results in the creation of a memory location where p's value-a memory address-is stored and in the filling up of a block of memory where the User object created on the right-hand side of the assignment operator is stored. The memory address stored in p points to the block of memory where the User object resides, as depicted in Figure 3.3.


Figure 3.2

click to expand
Figure 3.3

Consider now the Java case. The constructor invocation shown in the Java column again reserves a memory location where q's value is stored and in the filling up of a block of memory where the User object resides. In this case, the memory location reserved for q's value holds the object reference for the User object, as depicted in Figure 3.4. Think of the object reference stored in q as a disguised pointer (a disguised memory address), disguised in the sense that you cannot dereference it in the same sense that you can dereference a C++ pointer.

click to expand
Figure 3.4

Obviously then the construction invocation labeled (2) for C++ in Table 3.1 is very similar to the construction invocation for Java. The only difference is that for the case of C++ the variable p holds a pointer (a memory address) to the object, whereas for the case of Java the variable q holds a pointer (in the form of an object reference) to the object.

Does this imply that an object reference in Java is identical to a pointer in C++? They are similar but not identical. For the case of Java, if the JVM decided to move the User object to a different place in the memory for reasons of memory management, the object reference held by q would still be able to find the object. On the other hand, if the object pointed to by p were to be shifted to some other place in the memory, the value of p would need to be changed explicitly under program control.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net