Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 46 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
46
Dung lượng
493,75 KB
Nội dung
Lecture 21 Covers – Objects and references – Passing parameters with class parameters Reading: Savitch 4.3 21/1 Lecture overview Fundamental difference between simple and object variables Difference in assignments Difference in comparison tests Difference in parameter passing 21/2 ► Fundamental difference between simple and object variables 21/3 Two kinds of variables A variable, in its most general sense, can be an attribute, a local variable, or a parameter There are kinds of variables: – Simple variables (a.k.a variables of primitive type) – Object variables (a.k.a variables of class type, reference variables, object references) 21/4 Fundamental difference A simple variable holds the actual value An object variable holds the address of the object, not the object itself This fundamental difference can cause them to behave differently In particular, they behave very differently in – assignments – parameter passing – comparison tests 21/5 remarks Why we have that fundamental difference between the two kinds of variables? Don’t confuse object variables with objects 21/6 Variables of primitive types Name by which we refer to a value of a primitive type Stores the actual value Often referred to as simple variables 21/7 Variables of class type Name by which to refer to an object but A variable of class type stores the location (memory address) of an object The memory address stored is called a reference to an object Often referred to as reference variables or object variables 21/8 Object references vs objects An object reference or object variable is a variable that refers to an object An object reference can be an attribute, an argument, or a local variable Object references are different from objects – In what follows, we have one object but two object references BankAccount x = new BankAccount("123", "Smith"); BankAccount y = x; 21/9 Object references vs simple variables Why the difference? – Primitive types store a single value in a fixed amount of memory – Objects might be different sizes – e.g Strings can be any length, and different length strings use different amounts of memory 21/10 int x = 1; change( x ); System.out.println( "x = " + x ); where public void change( int i ) { i = i * 2; } main method x change method i 21/32 int x = 1; change( x ); System.out.println( "x = " + x ); where public void change( int i ) { i = i * 2; } main method x change method i 21/33 int x = 1; change( x ); System.out.println( "x = " + x ); where public void change( int i ) { i = i * 2; } main method x 21/34 BankAccount x = new BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void change( BankAccount a ) { a.deposit( 100 ); } 21/35 BankAccount x = new BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void change( BankAccount a ) { 12C4 accountNumber: “123” a.deposit( 100 ); customerName: “Smith” } balance: main method x 12C4 21/36 BankAccount x = new BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void change( BankAccount a ) { 12C4 accountNumber: “123” a.deposit( 100 ); customerName: “Smith” } balance: main method change method x 12C4 i 21/37 BankAccount x = new BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void change( BankAccount a ) { 12C4 accountNumber: “123” a.deposit( 100 ); customerName: “Smith” } balance: main method change method x 12C4 i 12C4 21/38 BankAccount x = new BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void change( BankAccount a ) { 12C4 accountNumber: “123” a.deposit( 100 ); customerName: “Smith” } balance: 100 main method change method x 12C4 i 12C4 21/39 BankAccount x = new BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void change( BankAccount a ) { 12C4 accountNumber: “123” a.deposit( 100 ); customerName: “Smith” } balance: 100 main method x 12C4 21/40 Differences in parameter passing Let x be a simple variable When x is passed to a method – The value of x is passed – Whatever happens to that value inside the method will not affect the variable x 21/41 Differences in parameter passing Let x be an object variable When x is passed to a method – The reference value of x (i.e the address of the object x refers to) is passed – Thus, variable x and the argument point to the same object – Therefore, any changes made to the object will be reflected through x as well 21/42 A closer look at argument passing Given the method public void exchange( BankAccount a1, BankAccount a2 ) { BankAccount temp = a1; a1 = a2; a2 = temp; } What is the effect of the code segment? BankAccount x = new BankAccount("123", "Smith"); BankAccount y = new BankAccount("456", "Jones"); exchange( x, y ); 21/43 Class exercise Q: What is output by this piece of code? public static void changeValues( int x, double y, String s, DigitalClock b ) { x = 99; y = 101.1; s = "Kangaroo"; b.setHours(12); b.setMinutes(25); } … int a = 0; double b = 22.2; String s = "Koala"; DigitalClock dc = new DigitalClock( ); changeValues(a, b, s, dc); System.out.println( a + " " + b + " " + s + " " + dc); 21/44 Pass-by-value In Java, argument passing is always passby-value, i.e values are passed from the caller to the called method For a simple argument, the data value of the actual argument is passed For an object argument, the reference value (i.e the address) of the actual argument is passed 21/45 Next lecture Programming with methods Static attributes Static methods The Math class 21/46 [...]... customerName: “Smith” balance: 100 21/ 20 ► Differences in comparison tests 21/ 21 Differences in testing for equality Compare the effect of the following code segments int x = 1; int y = 1; boolean b = (x == y); BankAccount x = new BankAccount("123", "Smith"); BankAccount y = new BankAccount("123", "Smith"); boolean b = (x ==y); 21/ 22 int x = 1; int y = 1; boolean b = (x == y); x 1 y 1 21/ 23 BankAccount x = new... y = x; – Assigns the value of x to y – After that, x and y “operate” independently 21/ 13 int x = 1; int y = x; y = 2; System.out.println( x + " " + y ); x 1 21/ 14 int x = 1; int y = x; y = 2; System.out.println( x + " " + y ); x 1 y 1 21/ 15 int x = 1; int y = x; y = 2; System.out.println( x + " " + y ); x 1 y 2 21/ 16 Differences in assignments Let x, y be object variables Then the statement y =... by customer name 21/ 27 ► Differences in parameter passing 21/ 28 Differences in parameter passing Compare the output of the following code segments int x = 1; change( x ); System.out.println( "x = " + x ); where public void change( int i ) { i = i * 2; } 21/ 29 int x = 1; change( x ); System.out.println( "x = " + x ); where public void change( int i ) { i = i * 2; } main method x 1 21/ 30 int x = 1;... balance: 100 3294 accountNumber: “123” customerName: “Smith” balance: 100 21/ 24 Testing objects for equality Always define an equals method to compare the content of two objects – Returns a boolean To order objects, define a compareTo method – Returns an integer 21/ 25 Equals method Write an equals method for the BankAccount class 21/ 26 CompareTo method Write a compareTo method for the BankAccount... object 21/ 17 BankAccount x = new BankAccount("123", "Smith"); BankAccount y = x; y.deposit(100); System.out.println( x + " " + y ); x 12C4 12C4 accountNumber: “123” customerName: “Smith” balance: 0 21/ 18 BankAccount x = new BankAccount("123", "Smith"); BankAccount y = x; y.deposit(100); System.out.println( x + " " + y ); x 12C4 y 12C4 12C4 accountNumber: “123” customerName: “Smith” balance: 0 21/ 19... int i ) { i = i * 2; } main method x 1 change method i 21/ 31 int x = 1; change( x ); System.out.println( "x = " + x ); where public void change( int i ) { i = i * 2; } main method x 1 change method i 1 21/ 32 int x = 1; change( x ); System.out.println( "x = " + x ); where public void change( int i ) { i = i * 2; } main method x 1 change method i 2 21/ 33 int x = 1; change( x ); System.out.println( "x =...► Differences in assignments 21/ 11 Differences in assignments Compare output from the following code segments int x = 1; int y = x; y = 2; System.out.println( x + " " + y ); BankAccount x = new BankAccount("123", "Smith"); BankAccount y = x; y.deposit(100); System.out.println( x + " " + y ); 21/ 12 Differences in assignments Let x, y be simple variables Then... = 1; change( x ); System.out.println( "x = " + x ); where public void change( int i ) { i = i * 2; } main method x 1 21/ 34 BankAccount x = new BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void change( BankAccount a ) { a.deposit( 100 ); } 21/ 35 BankAccount x = new BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void... BankAccount("123", "Smith"); change( x ); System.out.println( "x = " + x ); where public void change( BankAccount a ) { 12C4 accountNumber: “123” a.deposit( 100 ); customerName: “Smith” } balance: 0 main method x 12C4 21/ 36 ... System.out.println( x + " " + y ); x 21/ 14 int x = 1; int y = x; y = 2; System.out.println( x + " " + y ); x y 21/ 15 int x = 1; int y = x; y = 2; System.out.println( x + " " + y ); x y 21/ 16 Differences in assignments... x 12C4 y 12C4 12C4 accountNumber: “123” customerName: “Smith” balance: 100 21/ 20 ► Differences in comparison tests 21/ 21 Differences in testing for equality Compare the effect of the following... can be any length, and different length strings use different amounts of memory 21/ 10 ► Differences in assignments 21/ 11 Differences in assignments Compare output from the following code segments