Richard's J2SE Day 3, Working with Objects

=================================================================
Old notes from "Sams Teach Yourself Java 2 in 21 Days" (still vaild, but different page number)
=================================================================

Creating New Objects (P63)
String name = new string();
VolcanoRobot robbie = new VolcanoRobot();

(P65) Listing 3.1 import java.util.StringTokenizer;
StringTokenizer class > convert string into short strings

Constructor > special method for creating and initializing a new instance of class.
Multiple constructor definitions in a class can each have a different number, or type of arguments.
Memory management in Java is dynamic and automatic. (do not need to de-allocate the memory)

(P68) Listing 3.2 SetPoints.java ... import java.awt.Point (modify instance variables example)

Class Variables (P69)
- variables defined and stored in a class itself, values apply to the class and all its instances: "static" keyword
(P69) FamilyMember.java

Nesting Method Calls (P71)
- A method can return a reference to an object, a primitive data type, or no value at all.
- If the method returns a object, you can call the methods of the object in the same statement.

Class Methods (P72)
- useful for gathering general methods together in one place.
- used for general utility methods that might not operate directly on an instance of that class but do fit with that class.
- e.g. class methods in Math class > no instance of class Math > just use method > Math.max(first, second);
- can use either an instance of the class or the class itself.

References to Objects (P73)
- A reference is an address that indicates where an object's varialbes and methods are stored.
- Listing 3.4 ReferenceTest.java

Casting and Converting Objects and Primitive Types (P75)
- to send arguments to methods or use variables in expressions, you must use varibles of the right data types.
- Exception: Strings handling in println() method is simplified by concatenation operator (+)
- Java has primitive types and object types. 3 forms of casts and conversions

Casting Primitive Types (P76)
- Boolean value must be either true or false and cannot be used in casting.
- Must use explict cast to cast value in large type to a smaller type: (typename)value
- (int) (x / y);

Casting Objects (P77)
- Instances of classes can be cast into instances of other classes
- Restriction: source and destination classes must be related by inheritance; one class must be a subclass of the other.
- To use superclass objects where subclass objects are expected, you must cast them explicitly.
- e.g. VicePresident is a subclass of Employee

Employee emp = new Employee();
VicePresident veep = new vicePresident();
emp = veep; // no cast needed for upward use
veep = (VicePresident) emp; // must cast explicitly (loss in precision)


- Casting one object is necessary wheneven you use Java2D graphics operations.

Converting Primitive Types to Objects and Vice Versa (P78)
- Cannot do
- Alternative: java.lang package include classes that corresponding to each primitive data type: Float, Boolean, Byte etc. Begin with capital letter.
- e.g.
Integer dataCount = new Integer(7801);
int newCount = dataCount.intValue();

- e.g. converting a String to a numeric type
String pennsylvania = "65000";
int penn = Integer.parseInt(pennsylvania);

Comparing Object Values and Classes (P79)
- Comparing Objects: == and != (P80) Listing 3.5 EqualsTest.java
- instead of checking whether one object has the same value as the other object, == and != determine whether both sides of the operator refer to the same object.

Determining the Class of an Object (P81)
- to find out what an object's (assigned to the variable key) class is
- String name = key.getClass().getName();
- getClass() method is defined in the Object class, available for all objects
- instanceof operator has two operands: a reference to an object on the left and a class name on the right.
"Texas" instance of String // true
Point pt = new Point (10, 10);
pt instanceof String // false

Exercises (P84)
ex1.TurnString.java

=================================================================
New notes from "Sams Teach Yourself Java 6 in 21 Days"
=================================================================

StringCheckerandOthers, done in NetBean, only a main() method, dealing with string operations and some other examples