Archive for the ‘Java’ Category

The curious instance of the car in the night!

2004-09-21

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

import java.lang.*

2004-09-20

This week I am attending a JavaTM Programming Language class SL-275. This is the first time that I have studied an Object Oriented programming Language. And after the first day today I must say I’m quite impressed.

To prepare myself I read the the second chapter of David Flanagan‘s JAVA In a Nutshell book: “How Java Differs from C“. Which I found to be of great help when experiencing Attributes, Classes and Methods today.

It made me wonder… I first learned Fortran 77 in 1987. I stopped using F77 when I learned Pr1me’s System Programming Language (SPL, a subset of PL1/G). Which I subsequently stopped using after I learnt The Kernighan and Ritchie: C Programming Language; First Edition. So How long is it going to be before ‘C’ is but a memory in my not so long past….

Stace

To be honest I stopped using SPL when I left Pr1me Computer. Primos (Prime Operating System) was mainly written in SPL and PLP with bits in fortran, assembler and Modular. Unlike Solaris which is mainly written in ‘C’.