Basic Java review for ADT

Object construction

This statement declares variable c of class (or type) Complex

Complex c;

c is not a Complex object, it can only hold a variable reference to a Complex object.

To create a Complex object:

c = new Complex();

State of c is defined by default constructor of Complex. new returns a reference to new object, which assigned to c.

Combining these two statements we get usual form

Complex c = new Complex();

Object References

Complex c;

c is not an object of class Complex, it is variable whose value is reference to c Complex object (instant of class Complex). In Java all variables of class type store only references to objects, so when we say «object c» we mean «the object to which c refers».

Declaration above reserves a word of memory at address (for example) 12795130. The word is initialized to the null reference. When the value of a variable of class type is null, it does not refer to any object.

c = new Complex();
Now the address returned by new is stored in c.

Aliasing

Complex c = new Complex();
Complex c1;
c1 = c;

The = operator assigns the value of c (an object reference) to c1. Now, c1 and c are aliased (they refer to) the same object.

The = operator does not copy objects. c1=c2 makes both them point to the same place in memory.

Object references as Method parameters

public Complex plus(Complex c)
{
    Complex sum = new Complex (real + c.real, imag + c.imag);
    return sum;
}

Parameter c of plus() is declared as Complex. The argument of the plus() message is a reference to a Complex object.

When the plus() message is sent to the object referred to by c1:

c3 = c1.plus(c2);
the value stored in argument c2 is passed by value to formal parameter c. Java always passes arguments to methods by value.

The references as method parameters

The value stored in c2 is a reference to an object. Therefore, formal parameter c is aliased to the object referred to by c2 when the plus() method is invoked.

In other words, when a method parameter has class type, the argument (a reference to an object) is passed by value to the parameter. Because the value is a reference to an object, that’s why some books say that Java objects are passed by reference to methods

Tracing the execution of plus()

 c3 = c1.plus(c2);

After the plus() message is sent to c1, but before

Complex sum = new Complex(...);
is executed.

c1 points to an object. c2 points to an object. c point to the same object c2 points to. c3 is null.

After Complex sum = new Complex(…); is executed

c1 points to an object. c2 points to an object. c point to the same object c2 points to. c3 is null. sum points to a new object.

After plus() returns, the object reference it returns is assigned to c3:

c3 = c1.plus(c2);

Variable sum and parameter c «disappear».

c1 points to an object. c2 points to an object. c3 points to an object.

About instance variable visibility

plus() directly accesses the real and imag variables of the Complex object that receives the message, and of the Complex object referenced by parameter c.

A method defined in one class can access the private variables of any instance of the same class. A method defined in one class cannot access the private variables of objects that are instances of other class.

Overloading methods

Overloading permits several methods to have the same name. The must be distinguished by different argument lists; having only a different return type will not be sufficient and will cause compile error.

When invoking an overloaded method, Java determines which method to invoke based on the method's signature. Signature is the number of parameters in the method's parameter list, and their types.

invokes the 0-argument (default) constructor

Complex x = new Complex();
invokes the 1-argument constructor
Complex y = new Complex(-7.3);
invokes the 2-argument constructor
Complex z = new Complex(1.2, 3.7);

Methods can be overloaded too. Suppose we want to be able to add a real number to a complex number We could create a new complex number from the real number (i.e., use the 1-argument constructor), then pass that complex number to plus() Instead, why not have a second plus() method, one that accepts a real number argument?

/*** Return the sum of this 
  * Complex number and its
  * argument. 
  ** @param r A real number
  *	  to be added to this
  *    complex number.* @return  A new Complex
  *    number equal to the sum
  *    this Complex number 
  *    and r.*/

public Complex plus(double r)
{
   // r is same as r + 0.0i 
   Complex sum = new Complex(real + r, imag); 
   return sum;
}

Overloading eliminates the need for multiple names for essentially the same function - error prone and tedious:

int SquareInt (int val)
{ 
   return val * val;
}

float SquareFloat (float val) 
{ 
   return val * val;
}

Arrays

There are two types of array in Java: array of primitive values and array of references to objects:

of primitive values

int [] intArray; //no memory allocated yet!
intArray = new int[4]; // space for four elements allocated

Or declare, allocate and initialize in one step:

int [] intArray = {25, 43, 57, 79};

of objects

Arrays of objects require an additional step (to allocate memory for the objects). First, allocate a reference to an array of references to PairClass objects (like declaring an array of primitive type).

PairClass[] pairArray; //no memory allocated yet!
pairArray = new PairClass[4]; // space for four elements allocated
pairArray[0] = new PairClass(5,19); //**additional step**. allocate space for the PairClass object.

We can declare and allocate space for references to objects in 1 step.

PairClass [] pairArray = new PairClass [4];

Or even declare, allocate array of references and allocate space for objects in 1 step.

PairClass [] pairArray =
{ new PairClass (5,9), new PairClass (9,4)
   new PairClass (6,3), new PairClass (2,1) };

Array length

length is public instance variable for array objects.

for (int i = 0; i < pairArray.length; i++)
{
pairArray [i] = new PairClass ();
pairArray [i].setA (0);
pairArray [i].setB (i);
}

The length instance variable is final. It can be assigned only once (when array is instantiated). This is true for all final variables.

Proper way:

final int a = 3;

The same as:

final int a;
a=3; //legal, but not good at all
a=4; //now compile error because of attempt to reassign

plus()

Poor version

public void plus(Complex c, Complex sum)
{
   sum.setReal(real + c.real);
   sum.setImag(imag + c.imag);
}

Two parameters:

  • reference to the object that is to be added to the object that receives the message
  • a reference to the object that will contain the sum

Invoking the method:

Complex c1 = new Complex(5.1, -8.6);
Complex c2 = new Complex(4.9);
Complex c3 = new Complex();

// c3 = c1 + c2
c1.plus(c2, c3);

Good version

public Complex plus(Complex c)
{ 
  Complex sum =
   new Complex( real + c.real, 
                imag + c.imag );
    return sum;
}

Complex c1 = new Complex( 5.1, -8.6);
Complex c2 = new Complex( 4.9 );
Complex c3;

// c3 = c1 + c2
C3 = c1.plus( c2 );

 
abstract_data_types_and_algorithms/basic_java_review.txt · Последние изменения: 2009/09/10 05:42 От freetonik
 
За исключением случаев, когда указано иное, содержимое этой вики предоставляется на условиях следующей лицензии:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki