Explain why aliases can cause trouble for programmers who are not aware of them.

Explain why aliases can cause trouble for programmers who are not aware of them.

Answer:  Aliases arise in Java when variables refer to the same object.  If one of those variables is used to pass a message to the object in such a way that the object is changed, then the other variables now refer to a changed object.  Unless the programmer is careful, he/she may not realize that a change has been made to the object.  Consider a class Foo that has an instance data x and a method incr that adds one to x.  Assume a and b are both of type Foo, where a’s x is 10.  Consider the following two instructions:  b = a;  b.incr( );  The result is that a/b’s value x is now 11, but if the programmer is not aware of the aliasing, then he/she might expect a’s x to still be 10.

Comments

Popular posts from this blog

Examine the collapse of Han. What factors explain how this occurred? How influential in Han Dynasty on Chinese History?

Summarize effective communication strategies in organizational hierarchies, and review the role and relevance of the organizational grapevine

Examine the age-structure diagrams of Sweden and Kenya below, and answer the following questions. Compare and contrast the two age structure diagrams in terms of two population dynamics - birth rate and death rate.