210 likes | 304 Views
CSE 114 – Computer Science I Objects and Reference. Bryce Canyon, Utah. Classes. Class —definition of a kind of object Like an outline or plan for constructing specific objects Class specifies what kind of data objects of that class have
E N D
CSE 114 – Computer Science IObjects and Reference Bryce Canyon, Utah
Classes • Class—definition of a kind of object • Like an outline or plan for constructing specific objects • Class specifies what kind of data objects of that class have • Each object has the same data items but can have different values • Class specifies what methods each object will have • All objects of the same class have the same methods available to them
First Instantiation: Object name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK” Class Definition Second Instantiation: Object name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR” Class Name: Automobile Data: amount of fuel ________ speed ________ license plate ________ Methods (actions): increaseSpeed: How: Press on gas pedal. stop: How: Press on brake pedal. Third Instantiation: Object name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF” Class as an Outline Objects that are instantiations of the class
Objects • Variables that are named instances of a class • the class is their type • Have both data and methods • called members of the object • Data items are also called fields or instance variables • Invoking a method means to call the method, i.e. execute the method. Ex: • objectVariableName.method() • objectVariableName is the calling (invoking) object
Containment • A class contains another class if it instantiates an object of that class • “HAS-A” • PairOfDice HAS-A Die
Die die2 int upValue int numFaces RollGames main method Object Variables as Instance Variables for other Objects PairOfDice dice Die die1 int upValue int numFaces
Primitive Types vs. Objects (Class Types) • Primitive variables: • assigned a memory location when declared • variable’s data stored there • Object variables • assigned a memory location when it is declared • address of where the object data will be is stored there • address starts as null • assigned when object variable is constructed
new • Used when constructing an object • Asks the JVM for a block of memory • What for? • to store the instance variables of the object • What does it return? • the address (#) of the memory block
MEMORY sum 0 Before object variables are constructed, they store the address null (0) MEMORY die1 null Syntax or Runtime Error Primitive vs. Class - Example int sum = 0; Die die1; die1.roll(); // CAN'T DO THIS! // die1 hasn’t been constructed
MEMORY die1 upValue 3 numFaces 8 etc… Primitive vs. Class – Example (cont’d) die1 stores an address, which is the address of the beginning of the memory block where the object’s data is die1 = new Die(8,3); die1.roll(); // Now it’s OK to roll
MEMORY die1 die2 upValue 4 numFaces 8 etc… upValue 4 numFaces 8 etc… == and Objects Die die1 = new Die(8,4); Die die2 = new Die(8,4); if (die1==die2) FALSE! if (die1.equals(die2)) SHOULD BE TRUE! DEPENDING ON equals DEF.
this is optional, but what is it? .equals and Objects • equalsshould return true when the two objects have equivalent state (instance variable) values // INSIDE Die class public boolean equals(Die otherDie) { return ( (upValue == otherDie.upValue) && (this.numFaces==otherDie.numFaces) ); }
Call-by-value Revisited • Note: • method arguments are copies of the original data • Consequence? • methods cannot assign (‘=’) new values to arguments and affect the original passed variables • Why? • changing argument values changes the copy, not the original
Java Primitives Example public static void main(String[] args) { int a = 5; int b = 5; changeNums(a, b); System.out.println(a); System.out.println(b); } public static void changeNums(int x, int y) { x = 0; y = 0; } Output? 5 5
Java Objects (Strings) Example public static void main(String[] args) { String a = "Hateful"; String b = "Career Opportunities"; changeStrings(a, b); System.out.println(a); System.out.println(b); } public static void changeString(String x, String y) { x = "The Magnificent Seven"; y = "The Magnificent Seven"; } • NOTE: When you pass an object to a method, you are passing a copy of the object’s address Output? Hateful Career Opportunities
How can methods change local variables? Output? Hello Hell • By assigning returned values • Ex, in the String class: • substring method returns a new String String s = "Hello"; s.substring(0, 4); System.out.println(s); s = s.substring(0, 4); System.out.println(s);
Parameter Passing Revisited • Primitive variables are passed using call-by-value. • a copy of the value is sent to the method • any change to that value in the method does NOT affect the original primitive variable • What about with objects? • a copy of the address is sent • any changes made via mutator methods can change the original object • Danger: Reconstructing an object parameter for a method does not reconstruct the originally passed variable
swap 1 a 3 x 3 die y 3 1 upValue 6 numSides Parameter Passing Revisited (cont’d) • Example Method: public void swap(int x, Die y) { int temp = x; x = y.getUpValue(); y.setUpValue(temp); } • Example Method Call: int a = 3; Die die = new Die(6,1); swap(a,die); System.out.print("a is " + a); System.out.print(", die is " + die.getUpValue()); x and y are temporary variables, and are stored in different places in memory than a and die OUTPUT: a is 3, die is 3
These are only temporary variables Changing passed parameters public class ParameterPassing { public static void change (int idNum, String name, StringBuffer surname) { idNum = 15; name = "Oscar"; surname.replace(0, 4, "Jobs"); } public static void main(String[] args) { int id = 0; String fName = "Steve"; StringBuffer lName = new StringBuffer("????"); change(id, fName, lName); System.out.println(fName + " " + lName + " is employee # " + id); } } What is the output? OUTPUT: Steve Jobs is employee # 0
MEMORY … MEMORY "S" chars[0] … … … main "e" chars[4] id 0 … fName lName … … "?" chars[0] … … … … … "?" chars[3] … change … idNum "J" 15 "s" 0 "O" chars[0] name … … surname "r" chars[4] … … What’s happening in memory?
MEMORY die1 die2 upValue 3 numFaces 8 etc… = and Objects • = Assigns an ADDRESS TO OBJECT VARIABLE! Die die1 = new Die(8,4); Die die2; die2 = die1; • Doesn't make a new copy of object! • Now die1 and die2 both refer to the same object! die1.roll(); • Causes same change in die1 AND die2 6