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).
Messages can be sent to object parameters. If this changes the state (fields) of an object, this will be remembered when return.
Java is garbage collected: When can no longer access an object, space is reclaimed.
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.
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.
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;
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.
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 10Note: 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).
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.
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