Download presentation
Presentation is loading. Please wait.
Published byAshley Daniels Modified over 9 years ago
1
Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference
2
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
3
Class as an Outline Class Name: Automobile Data: amount of fuel ________ speed ________ license plate ________ Methods (actions): increaseSpeed: How: Press on gas pedal. stop: How: Press on brake pedal. Class Definition Objects that are instantiations of the class First Instantiation: Object name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK” Second Instantiation: Object name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR” Third Instantiation: Object name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF”
4
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
5
Containment A class contains another class if it instantiates an object of that class –“HAS-A” PairOfDice HAS-A Die
6
PairOfDice dice Die die1 int upValue int numFaces Die die2 int upValue int numFaces RollGames main method Object Variables as Instance Variables for other Objects
7
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
8
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
9
Primitive vs. Class - Example int sum = 0; Die die1; MEMORY die1 null MEMORY sum0 die1.roll(); // CAN'T DO THIS! // die1 hasn’t been constructed Before object variables are constructed, they store the address null (0) Syntax or Runtime Error
10
Primitive vs. Class – Example (cont’d) die1 = new Die(8,3); die1.roll(); // Now it’s OK to roll MEMORY die1 upValue numFaces 3 8 etc… die1 stores an address, which is the address of the beginning of the memory block where the object’s data is
11
== 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. MEMORY 4 8 4 8 etc… die1 upValue numFaces die2 upValue numFaces etc…
12
.equals and Objects equals should 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) ); } this is optional, but what is it?
13
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
14
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
15
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
16
How can methods change local variables? 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); Output? Hello Hell
17
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
18
swap a die x y 3 6 1 3 upValue numSides 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()); Parameter Passing Revisited (cont’d) OUTPUT: a is 3, die is 3 x and y are temporary variables, and are stored in different places in memory than a and die 31
19
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); } OUTPUT:Steve Jobs is employee # 0 What is the output? These are only temporary variables
20
What’s happening in memory? … chars[0]"O" chars[4]"r" …… "J" "s" 15 … change 0idNum surname name … … 0id lName fName … … … MEMORY main chars[0] "S" chars[4] "e" … … chars[0]"?" chars[3]"?" …… … … … MEMORY … …
21
= 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 MEMORY die2 upValue numFaces 3 8 die1 etc… 6
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.