CS 136 - Lecture 4

  1. Miscellaneous Java constructs
    1. Objects represented as references
    2. Parameter Passing
    3. Garbage Collection
    4. "This"
    5. Information Hiding
    6. Other method qualifiers
    7. Casts
    8. Arrays
    9. Strings

Miscellaneous Java

Objects represented as references

Programs manipulate references to objects, rather than the objects themselves

Thus if

    firstCard = new Card(ACE,SPADES);
    secondCard = firstCard;
Then both refer to the same card. Assignment is sharing!

Objects initially have value null.

Can be given a value by assignment of

Note that if x and y refer to objects then x==y iff x and y refer to the same object.

Generally use equals method if want to know if the contents are the same.

    thirdCard = new Card(ACE,SPADES);
then thirdCard != firstCard, but thirdCard.equals(firstCard).

Parameter Passing

All parameters in Java are passed by value. That is, any assignment to a parameter inside a method body is forgotten when the method returns.

Messages can be sent to object parameters. If this changes the state (fields) of an object, this will be remembered when return.


Garbage Collection

Need never dispose of objects!

Java is garbage collected: When can no longer access an object, space is reclaimed.


"This"

Keyword "this" can be used in a method to refer to the object executing the method (i.e., the receiver of the message).

An object can send a message to itself by writing this.m(...) or just m(...) ("this" is assumed)

Similarly, can write this.suit or just suit to get access to an instance variable.

More interestingly, can send "this" as a parameter to another object.


Information Hiding

Public: World has access to this feature

Protected: Features accessible within methods of the class (or extensions), but not accessible outside of the object.

All fields should be protected.
 

Thus firstCard.suit and firstCard.getSuitString() are undefined since both features declared to be protected.

Core Java tends to prefer private to protected. In CS 134 you used private. In CS 136 We'll generally use protected.

Private fields are not visible in extensions or outside object.

Protected and private declarations are used to hide implementation details from clients.

Makes it easier to change implementation later if needed.


Other method qualifiers

Static: One copy associated with the class - not associated with individual objects.

Typically get access through class or interface name, rather than object name:

    CardInterface.SPADES, CardInterface.ACE;
Final: May not be changed. Used to declare constants:
    static final int SPADES = 3;

Casts

Casts occasionally needed if static type of object does not reflect actual type.

In equals only call with another CardInterface object, but type of parameter forced to be Object.

If cast is incorrect then program halts with error message.


Arrays

Declare arrays:

int[] scores;

Note: Never include the size of the array in the declaration

int[10] scores // Illegal in Java!

Arrays are objects and must be instantiated (created) like other objects:

int[] scores = new int[10];
or

int[] scores;
scores = new int[10];

Every array knows its size: can be obtained by referring to its length value field

scores.length will return 10
Note: One could use the condition n < scores.length to control a for loop that processes each element of the array.

Java supports array expressions, which can be used to initialize arrays:

int[] a = {1,2,3,4,13};

initializes a to be an array of 5 integers (with indices 0 to 4).


Notes:

1. (Like Pascal) Java does not support multi-dimensional arrays.

2. (Like Pascal) Java does support arrays of arrays (though with less syntactic sugar).

int[][] table;
is equivalent to
(int[])[] table

That is, table is an array of arrays of integers.

3. (Unlike in Pascal) one cannot write subscripts separated by "," - must write table[3][5].

table = new int[10][15];
creates table as 2-dimensional array.

4. Can also do trickier things like create 2-dim'l array of elts where successive rows have different numbers of elements. See text for examples.


Strings

    public int length();
    public char charAt(int index);  
        // Require 0 <= index < length, o'wise raise exception
    public boolean equals(Object anObject);     
    public boolean equalsIgnoreCase(String anotherString);  
    public int compareTo(String anotherString);     
        // returns neg, 0 or pos int depending on comparison 
    public int indexOf(int ch)  
        // index of first occurrence of ch or -1 if not there