Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.

Similar presentations


Presentation on theme: "Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java."— Presentation transcript:

1 Using Objects

2 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java and Objects In Java, everything is an object Programs are objects Objects (including programs and objects within programs) CREATE and USE other objects

3 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 3 How do you USE objects? public class BankDemo1 { public static void main( String args[] ) { BankAccount bobAcct; bobAcct = new BankAccount(); int balance1 = bobAcct.getBalance(); System.out.println( "Bob's balance is now: " + balance1 ); System.out.println( "Depositing 50 to Bob's account..." ); bobAcct.deposit( 50 ); System.out.println( "Bob's balance is now: " + bobAcct.getBalance() ); } DECLARE a variable that can “point” to a BankAccount CREATE a new BankAccount and make account point to it CALL getBalance() method of account. Returns an int Call deposit method with amount as argument. CALL getBalance() method again to get the balance again

4 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 4 Declaration vs. Creation Why do you still need to create the object after declaring it? Because object-type variables are reference variables (aka pointers) They don’t store the object itself, they just point to it

5 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 5 Declaration vs. Creation BankAccount bobAcct; bobAcct = new BankAccount( ); Declaration allocates a reference (aka “pointer”) But it does NOT create an object. Reference is “null”, i.e., points to nothing bobAcct

6 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 6 Declaration vs. Creation bobAcct Creating an object … “new” creates an instance “=” makes variable point to it BankAccount bobAcct; bobAcct = new BankAccount( ); BankAccount int balance 0

7 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 7 Reference Variables Why use references? Why not just store the object in the variable itself (like with primitive types such as ints, doubles, etc.)? Answer: objects often need to be “passed around” and used by other objects two objects can need to use the same object at the same time e.g., the client and the bank teller both need to have access to the same BankAccount instance

8 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 8 Example: Two Accounts bobAcct BankAccount bobAcct; bobAcct = new BankAccount( ); BankAccount aliceAcct; aliceAcct = new BankAccount(); bobAcct.deposit( 50 ); aliceAcct.deposit( 60 ); BankAccount int balance 0 aliceAcct BankAccount int balance 0 5060

9 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 9 Example: “ Joint Account ” bobAcct BankAccount bobAcct; bobAcct = new BankAccount( ); BankAccount aliceAcct; aliceAcct = bobAcct; bobAcct.deposit( 50 ); aliceAcct.deposit( 60 ); BankAccount int balance 0 aliceAcct 50 110

10 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 10 Something to think about Suppose int a; a = 150000; int b = a; a = a + 300000; int c = b; … BankAccount x = new BankAccount(); x.deposit( 150000 ); BankAccount y = x; x.deposit( 300000 ); long z = y.getBalance(); What is the final value of c? z? Why? Write a program that tests the code above.

11 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 11 public class BankDemo1 { public static void main( String args[] ) { BankAccount bobAcct; int balance1 = bobAcct.getBalance(); System.out.println( "Bob's balance is now: " + balance1 ); System.out.println( "Depositing 50 to Bob's account..." ); bobAcct.deposit( 50 ); System.out.println( "Bob's balance is now: " + bobAcct.getBalance() ); } A Common Error Forgot to create a new BankAccount and assign it to account What will go wrong?

12 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 12 public class BankDemo1 { public static void main( String args[] ) { BankAccount bobAcct; int balance1 = bobAcct.getBalance(); System.out.println( "Bob's balance is now: " + balance1 ); System.out.println( "Depositing 50 to Bob's account..." ); bobAcct.deposit( 50 ); System.out.println( "Bob's balance is now: " + bobAcct.getBalance() ); } A Common Error You will get a NullPointerException! Forgot to create a new BankAccount and assign it to account

13 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 13 Calling a Method on a Null Pointer NullPointerException: when you try to call a method or access a field on an object variable that is not pointing to anything Remember that you need to create a new object before using it! account null deposit( 250 ) account deposit( 250 ) BankAccount ??

14 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 14 The Balloon Analogy Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie Barker (Wrox) xyz (null) BankAccount x, y, z; x = new BankAccount(); y = x; z = new BankAccount(); y = z; x = z;

15 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 15 The Balloon Analogy xyz (null) BankAccount x, y, z; x = new BankAccount(); y = x; z = new BankAccount(); y = z; x = z; Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie Barker (Wrox)

16 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 16 The Balloon Analogy xyz (null) BankAccount x, y, z; x = new BankAccount(); y = x; z = new BankAccount(); y = z; x = z; Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie Barker (Wrox)

17 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 17 The Balloon Analogy xyz BankAccount x, y, z; x = new BankAccount(); y = x; z = new BankAccount(); y = z; x = z; Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie Barker (Wrox)

18 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 18 The Balloon Analogy BankAccount x, y, z; x = new BankAccount(); y = x; z = new BankAccount(); y = z; x = z; xyz Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie Barker (Wrox)

19 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 19 The Balloon Analogy BankAccount x, y, z; x = new BankAccount(); y = x; z = new BankAccount(); y = z; x = z; xyz Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie Barker (Wrox)

20 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 20 Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie Barker (Wrox) The Balloon Analogy BankAccount x, y, z; x = new BankAccount(); y = x; z = new BankAccount(); y = z; x = z; xyz If there is no reference pointing to an object anymore, it gets “garbage collected”

21 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 21 Points to Remember Remember: Object variables are references (aka pointers) Point to “null” by default Need to call new to create a new object Different variables can point to same object at the same time If no pointers point to an object, then it gets collected by the “garbage collector” You can’t access it anyway if you don’t have a pointer to it This allows Java to automatically save on memory

22 More on using Methods

23 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 23 How Methods Work BankProgram.java State of Memory … int aliceBalance = aliceAccount.getBalance(); … public int getBalance() { return this.balance; } BankAccount.java aliceAccount getBalance() BankAccount aliceBalance 100 int balance 1)Find object pointed to by aliceAccount 2)Find code for class of that object 3)Find code for getBalance() 4)Run code 5)Return value 6)Use returned value When the line above is run … 100

24 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 24 Three Kinds of Variables Field (aka Attribute or Instance Variable) Variables declared inside a class’ code, but outside any methods Part of object’s “permanent” state Use for state that is retained between method calls can be accessed as “this.fieldname” or simply fieldname Local Variable Variables declared inside a method definition Only exists while we’re inside the method Use as a “scratchpad” (temporary storage) during a computation Parameter Variables declared in the parentheses of a method definition Holds a copy of the value or reference passed as an argument to the method call* Is also a local variable – i.e., only exists inside the method * Note: the argument is the variable or value given in the method call, while the parameter is the variable in the method definition that copies that value)

25 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 25 BankAccount Example BankAccount int balance BankAccount() (constructor) int getBalance() void deposit( int amount ) public class BankAccount { private int balance; public BankAccount() { this.balance = 0; } public int getBalance() { return this.balance; } public void deposit( int amount ) { this.balance = this.balance + amount; } … } BankAccount.java acct1: BankAccount 0 balance Suppose acct1 already exists, and another object calls … acct1.deposit( 100 ); What happens? A variable named amount is created with value 100 and passed as the parameter to deposit. (It also becomes a local variable within deposit.) Read the value of this.balance, add to value of amount, and store the value into this.balance again 100 amount 0 100

26 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 26 Using the Debugger in BlueJ In BlueJ, set a breakpoint in your main class (the one with main()) Then, try using “Step Into” BlueJ allows you to watch the local variables and the fields as they are created/disappear, and as they change values

27 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 27 Scoping Rules Variable is visible within the block (curly brace pairs) in which it is declared field is visible within the whole class, local variable is visible within the method, “sub-local” variables declared within blocks inside a method are only visible within their block You can’t declare a variable of the same name twice in the same block But, you can declare a variable of the same name in different blocks This creates two totally independent variables

28 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 28 Variables of the Same Name Non-Overlapping Blocks: no conflict since variables do not exist at the same time parameters or local variables in different methods e.g., int amount parameter in deposit and withdraw Overlapping Blocks: innermost variable is visible, outer variable(s) are “hidden” e.g., a field int x vs. a local variable int x inside the method, the name x refers to the local variable but remember that the field x still exists, and has a different value This is why the parameters of constructors should have different names from the fields they initialize, unless you’re using “this”

29 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 29 State of Memory private void myMethod( int x, float y ) { x = 25; y = 35.4f; } A A A. A. Local variables do not exist before the method execution At before myMethod A A Code x = 10; y = 20; myMethod( x, y ); xx 10 y 20 Assume x and y are fields Assume this is a convenience method in the same class Variables of the Same Name

30 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 30 State of Memory B. B. Note that the parameters are totally independent storage spaces. Values are copied at B B Code x = 10; y = 20; myMethod( x, y ); B B xx 10 y 20 x 10 y 20.0f private void myMethod( int x, float y ) { x = 25; y = 35.4f; } Variables of the Same Name

31 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 31 C C State of Memory C. C. The values of parameters are changed. After is executed C C Code x = 10; y = 20; myMethod( x, y ); xx 10 y 20 x 1025 y 1035.4f private void myMethod( int x, float y ) { x = 25; y = 35.4f; } Variables of the Same Name

32 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 32 Code D D State of Memory D. D. Parameters are erased. Arguments remain unchanged. At after myMethod D D x = 10; y = 20; myMethod( x, y ); xx 10 y 20 private void myMethod( int x, float y ) { x = 25; y = 35.4f; } Variables of the Same Name

33 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 33 Constructors A constructor is a special kind of method that is called with the new command Used for initializing an object to a valid state Name of constructor must be same as name of class No return type (!) if you accidentally put a return type, compiler will not complain. It will define an ordinary method which happens to have the same name as the class Default “no-args” constructor If no constructor is defined, the Java compiler will include a default constructor with no arguments and no body If a constructor is defined, then the compiler will not create a “no-args” constructor

34 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 34 Defining Constructors A constructor will have the following form: public ( ) { } public BankAccount ( int initialBalance ) { this.balance = initialBalance; } Statements Modifier Class Name Parameter Note: No Return Type!

35 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 35 What’s Wrong? public class StudentRecord { private String name; public StudentRecord( String name ) { } … methods (not shown) … } Some people think that you can set a field by simply giving the parameter same name as the field. THIS DOES NOT WORK. The parameter and the field are two different and independent variables, even if they have the same name.

36 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 36 The Correct Way public class StudentRecord { private String name; public StudentRecord( String initialName ) { name = initialName; } … methods (not shown) … } Give the parameter a different name in order to be clear. Don’t forget to set the field through an assignment statement.

37 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 37 Another Way public class StudentRecord { private String name; public StudentRecord( String name ) { this.name = name; } … methods (not shown) … } Give the parameter the same name as the field. (Convenient!) Use “this” to distinguish between “name” the field and “name” the parameter (But be careful! What happens if you forget to say this?)

38 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 38 Another Error to be careful of public class StudentRecord { private String name; public StudentRecord( String Name ) { this.name = name; } … methods (not shown) … } What’s wrong? Will this compile? What will happen when you run this? Tip: To be safe, don’t give the constructor’s parameters the same name as the fields they change, give the parameters a name like “initialName” or “initName”.

39 Passing Parameters

40 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 40 Passing Primitive Types State of Memory public void myMethod( int one, float two ) { one = 25; two = 35.4f; } A A A. A. Local variables do not exist before the method execution At before myMethod A A Code x = 10; y = 20; tester.myMethod( x, y ); xx 10 y 20

41 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 41 State of Memory B. B. The values of arguments are copied to the parameters. Values are copied at B B public void myMethod( int one, float two ) { one = 25; two = 35.4f; } Code x = 10; y = 20; tester.myMethod( x, y ); B B xx 10 y 20 one 10 two 1020.0f Passing Primitive Types

42 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 42 C C State of Memory C. C. The values of parameters are changed. After is executed C C public void myMethod( int one, float two ) { one = 25; two = 35.4f; } Code x = 10; y = 20; tester.myMethod( x, y ); Passing Primitive Types xx 10 y 20 10 one two 10 25 35.4f 10 20.0f

43 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 43 Code D D State of Memory D. D. Parameters are erased. Arguments remain unchanged. At after myMethod D D public void myMethod( int one, float two ) { one = 25; two = 35.4f; } x = 10; y = 20; tester.myMethod( x, y ); xx 10 y 20 Passing Primitive Types 10 one two 10 25 35.4f

44 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 44 Passing Parameters Arguments are passed to a method using the pass-by-value scheme Parameters and arguments do not have to have the same name Whether or not they have the same name, parameters are separate copies of the arguments Parameters are local to the method, i.e., they only exist while inside the method. Changes made to the parameters will not affect the value of corresponding arguments * Note: the argument is the variable or value given in the method call, while the parameter is the variable in the method definition that copies that value)

45 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 45 Passing Objects as Parameters Object-type parameters copy the reference to the passed object calls to methods of the parameter does affect the original object’s state sometimes known as “pass-by-reference” BUT, if we assign parameter variable to another object, then original object is not affected anymore Think of it this way: pass-by-value, but the value being passed is the pointer to the object, not the object itself

46 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 46 Passing Objects State of Memory public void myMethod( BankAccount one, BankAccount two ) { one.deposit( 50 ); two.withdraw( 10 ); } A A A. A. Local variables do not exist before the method execution At before myMethod A A in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 20 int balance

47 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 47 Passing Objects State of Memory public void myMethod( BankAccount one, BankAccount two ) { one.deposit( 50 ); two.withdraw( 10 ); } B. B. Parameters one and two copy the references to the objects pointed to by x and y At entering myMethod B B in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 20 int balance B B one two

48 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 48 Passing Objects State of Memory public void myMethod( BankAccount one, BankAccount two ) { one.deposit( 50 ); two.withdraw( 10 ); } C. C. Method calls to one and two affect the same objects referred to by x and y At after running code C C in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount int balance 150 BankAccount int balance 10 C C one 10 two 10

49 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 49 Passing Objects State of Memory public void myMethod( BankAccount one, BankAccount two ) { one.deposit( 50 ); two.withdraw( 10 ); } D. D. When myMethod returns, parameters one and two disappear. But objects x and y have been changed. D. D. When myMethod returns, parameters one and two disappear. But objects x and y have been changed. At after running code D D in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 150 int balance BankAccount 10 int balance D D one two

50 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 50 Reassigning Parameters State of Memory public void myMethod( BankAccount one, BankAccount two ) { one = two; one.deposit( 50 ); two.withdraw( 10 ); } Suppose we reassign one to something else within the method body At entering myMethod B B in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 20 int balance B B one 10 two 10

51 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 51 State of Memory public void myMethod( BankAccount one, BankAccount two ) { one = two; one.deposit( 50 ); two.withdraw( 10 ); } C. C. Method calls to one and two affect the same object (same as pointed to by y) x is not affected. C. C. Method calls to one and two affect the same object (same as pointed to by y) x is not affected. At after running code C C in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 60 int balance C C one 10 two 10 Reassigning Parameters

52 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 52 Passing Objects State of Memory At after running code D D in BankApplet BankAccount x = new BankAccount( 100 ); BankAccount y = new BankAccount( 20 ); tester.myMethod( x, y ); xx 10 y BankAccount 100 int balance BankAccount 60 int balance D D D. D. When myMethod returns, parameters one and two disappear. Objects y has been changed. D. D. When myMethod returns, parameters one and two disappear. Objects y has been changed. public void myMethod( BankAccount one, BankAccount two ) { one = two; one.deposit( 50 ); two.withdraw( 10 ); } one two

53 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 53 Passing parameters in passALoad public void passALoad( double amount, PrepaidAccount acct ) { if ( amount <= credit ) { credit -= amount; acct.loadCredit( amount ); }

54 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 54 Passing parameters in passALoad The passALoad method employs both pass- by-value and pass-by-reference schemes The amount parameter copies the value of the argument It does not change nor affect the value of the argument during and after method execution

55 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 55 Passing parameters in passALoad The acct parameter on the other hand copies the reference to a particular account object Method calls done through acct affect the actual PrepaidAccount object and change its state The changes remain after the method is executed

56 Strings

57 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 57 Strings In Java, strings are objects and String is a “built- in” class with special treatment String s = new String( “Hello” ); is the same as String s = “Hello”; “Hello” is an example of a String literal representing a String object The + operator can be used for String concatenation The String class has certain methods length() indexOf(…) substring(…) equals(…) equalsIgnoreCase(…)

58 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 58 String Equality String s1 = “Bob”; String s2 = “Bo” + “b”; if ( s1 == s2 ) { System.out.println( “Equal.” ); } What does this print out? Why?

59 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 59 String Equality Recall that since Strings are objects, String variables are references It’s possible to have 2 String objects with the same value, but they are different objects Checking for equality of two references (pointers) checks if they are pointing to the SAME object If they are not the SAME object, then it will NOT be equal, even if they have the same value Use this instead: if ( s1.equals( s2 ) ) …


Download ppt "Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java."

Similar presentations


Ads by Google