Tuesday Sep 21, 2004

Day 2 of Java Language course today.

The best bit from today had to be the explanation of an object verses a reference type for which we were given the following example:

John bought a new car. It was his car, all his friends knew it was his car and thus it was johns car:

   public class example {
	public static void main(String[] args) {
		Car johnsCar = New Car();
	...
	}
   }

    -----------            --------
   | johnsCar  |--------->|   Car  |
    -----------            --------

However he shared the car with his wife, and her friends new the car to be Debbie's car. So now the car is know by two names, but there is still only one car. It just has two references, represented in java as:

   public class example {
	public static void main(String[] args) {
		Car johnsCar = New Car();
		Car debbiesCar = johnsCar;
		...
	}
   }

    -----------            --------
   | johnsCar  |--------->|   Car  |
    -----------            --------
                          ^
    ------------         / 
   | debbiesCar |--------
    ------------

Before long the Daughter has passed her driving test and is also allowed to borrow the Car. But to her friends it is know as... Jackie's Car. Its still the same car only it now has three references:

   public class example {
	public static void main(String[] args) {
		Car johnsCar = New Car();
		Car debbiesCar = johnsCar;
		Car jackiesCar = johnsCar;
		...
	}
   }

    -----------            --------
   | johnsCar  |--------->|   Car  |
    -----------            --------
                          ^   ^
    ------------         /   /
   | debbiesCar |--------   /
    ------------           /
                          /
    ------------         / 
   | jackiesCar |--------
    ------------

Unfortunately, Jackie has an accident in the car. Jackie is all right, but the same can not be said for the car. Jackie is no longer allowed to use the car. But of course the car is damaged:

   public class example {
	public static void main(String[] args) {
		Car johnsCar = New Car();
		Car debbiesCar = johnsCar;
		Car jackiesCar = johnsCar;
		...
		jackiesCar = null;
	}
   }

    -----------            --------
   | johnsCar  |--------->|   Car  |
    -----------            --------
                          ^ 
    ------------         / 
   | debbiesCar |-------- 
    ------------          

    ------------          
   | jackiesCar |
    ------------

John then buys Jackie a car of her own. Thus we have two cars, one with one reference and one with two:

   public class example {
 	public static void main(String[] args) {
		Car johnsCar = New Car();
		Car debbiesCar = johnsCar;
		Car jackiesCar = johnsCar;
		...
		jackiesCar = new Car();
	}
   }

    -----------            -------
   | johnsCar  |--------->|   Car |
    -----------            -------
                          ^ 
    ------------         / 
   | debbiesCar |-------- 
    ------------          
    ------------          -----
   | jackiesCar |------->| Car |
    ------------          -----

Of course it's not a true story... If it was true then Debbie would have Dis-associated herself from John's car until he got it fixed! (ouch)!

Stace

This blog copyright 2009 by ace