Lesson16

29
Anonymous objects It is possible to instantiate an object without a name. public void melloJello(Circle cirA) osborne.melloJello(new Circle(5) ); The code, new Circle(5), instantiates the object; however, in the region of the calling code it doesn’t have a name.

Transcript of Lesson16

Page 1: Lesson16

Anonymous objects

It is possible to instantiate an object without a name.

public void melloJello(Circle cirA) osborne.melloJello(new Circle(5) ); The code, new Circle(5), instantiates the

object; however, in the region of the calling code it doesn’t have a name.

Page 2: Lesson16

Lesson 16

Page 3: Lesson16

Private methods can only be accessed from within the class itself.

Page 4: Lesson16

Declaring and instantiating an object

Normally when we instantiate an object, we do it in one line of code:

Circle cir1 = new Circle(3.0); However, it can be done in two lines: Circle cir1; //Here, cir1 is merely declared to

be of type Circle cir1 = new Circle(3.0); //Here, it is finally

instantiated.

Page 5: Lesson16

Setting two objects equal Circle cir1 = new Circle(5.3); //cir1 has a radius of 5.3 We will now demonstrate how to declare a cir2 object,

but not to instantiate it. Then in another line of code, set it equal to cir1: Circle cir2; //cir2 has only been declared to be of typeCircle cir2 = cir1; //cir2 and cir1 now refer to the same

object. There is only one object. It simply has two references to it.

Thus, cir2.area( ) returns exactly the same as cir1.area( )….and cir1.radius is exactly the same as cir2.radius,…etc.

Page 6: Lesson16

Determining if two objects are equal

System.out.println(cir1 = = cir2);Circle cir1 = new Circle(11); Circle cir2 = new Circle(11);System.out.println(cir1 = = cir2); //false, in spite of the fact they both have a

radius of 11

Page 7: Lesson16

System.out.println( cir1.equals(cir2) ); //false.

(cir1.equals(cir2) ) is equivalent to (cir1 = = cir2).

Circle class inherits the equals method from a superclass Object and simply compares to see if we are referring to the same object.

If the programmer who created the Circle class created an equals method for it, then that overrides the inherited method and compares the contents of the two objects (likely the radii). In this case, the println above would print a true since the contents of the two objects are the same (they both have a radius of 11).

Page 8: Lesson16

Strings

String s1 = “Hello”; String s2 = “Hello”; //s1 and s2 are String

constants System.out.println(s1 = = s2); // prints true

Page 9: Lesson16

The String constant pool

String literals are stored as String constants in a separate memory area called theString constant pool.

When object s1 is created “Hello” it is placed in the String constant pool with the reference s1 pointing to it. Then, for efficiency, when the reference (variable) s2 is created, Java checks the pool to see If the String constant being specified for s2 is already there. Since it is in this case, s2 also points to “Hello” stored in the String constant pool.

Physically, s1 and s2 are two separate String object references, but logically they are pointing to the same object in theString constant pool. So, in (s1 = = s2) from the code above we see that both s1 and s2 are referencing the same object, and a true is returned.

Page 10: Lesson16

Exercises for Lesson 16

Problems 1 – 5 refer to the following code (assume that equals is not an explicit, method of this class):

MoonRock myRock = new MoonRock(3, “Xeon”);MoonRock yourRock = new MoonRock(2,

“Kryptonite”);MoonRock ourRock = new MoonRock(3, “Xeon”);MoonRock theRock;theRock = ourRock;1. Does theRock.equals(ourRock) return a true or

false?

Page 11: Lesson16

1.true

Page 12: Lesson16

MoonRock myRock = new MoonRock(3, “Xeon”);MoonRock yourRock = new MoonRock(2, “Kryptonite”);MoonRock ourRock = new MoonRock(3, “Xeon”);MoonRock theRock;theRock = ourRock;2. Does theRock.equals(yourRock) return a true or

false?3. Does theRock.equals(myRock) return a true or false?4. Does myRock = = ourRock return a true or false? 5. Does myRock.equals(yourRock) return a true or

false?

Page 13: Lesson16

2-5. All False

Page 14: Lesson16

Problems 6 – 11 refer to the following code: public class Weenie{public Weenie( ){ . . . }public String method1(int jj){ . . . }private void method2(String b){ . . . }public int method3( ){ . . . }public double x;public int y;private String z;}Now suppose from within a different class we instantiate a Weenie

object,oscarMayer. All of the code in questions 6 – 11 is assumed to be in this otherclass.

6. Is int zz = oscarMayer.method1(4); legal? If not, why?

Page 15: Lesson16

6. Is int zz = oscarMayer.method1(4); legal? If not, why?

No, method1 returns a String and we are trying to store it in xx, an int.

Page 16: Lesson16

6. Is int zz = oscarMayer.method1(4); legal? If not, why?

7. Is oscarMayer.method2(“Hello”); legal? If not, why?

8. Is int cv = oscarMayer.method3( ); legal? If not, why?

9. Is int cv = oscarMayer.method3(14); legal? If not, why?

10. Is oscarMayer.z = “hotdog”; legal? If not, why?

11. Assume the following code is inside method1:method2(“BarBQ”); Is this legal? If not, why?

Page 17: Lesson16

6. Is int zz = oscarMayer.method1(4); legal? If not, why? No, method1 returns a String and we are trying to store it in zz, an

int.Answers 7. Is oscarMayer.method2(“Hello”); legal? If not, why? No, method2 is private8. Is int cv = oscarMayer.method3( ); legal? If not, why? Yes9. Is int cv = oscarMayer.method3(14); legal? If not, why? No, method3 is not expecting to receive a parameter…yet we’resending a

14.10. Is oscarMayer.z = “hotdog”; legal? If not, why? No, z is private.11. Assume the following code is inside method1:method2(“BarBQ”); Is this legal? If not, why? Yes, we can access a private method from within its class.

Page 18: Lesson16

12. Instantiate an object called surferDude from the Surfer class using two separate lines of code. One line should declare the object and the other line should instantiate it. (Assume no parameters are sent to the constructor.)

Page 19: Lesson16

12. Instantiate an object called surferDude from the Surfer class using two separate lines of code. One line should declare the object and the other line should instantiate it. (Assume no parameters are sent to the constructor.)

Surfer surferDude;surferDude = new Surfer( );

Page 20: Lesson16

13. Which of the following is correct? (Assume beco is an object with a method (method33) that receives a Circle paramater.)

a.Circle cir5 = new Circle(10); beco.method33(cir5);b. beco.method33( new Circle(10) );c. Both a and b

Page 21: Lesson16

13. Which of the following is correct? (Assume beco is an object with a method (method33) that receives a Circle paramater.)

a.Circle cir5 = new Circle(10); beco.method33(cir5);b. beco.method33( new Circle(10) );c. Both a and b

Page 22: Lesson16

14. What is the value of balance after the following transactions?//refer to the BankAccount class you created on p. 15-7

BankAccount acc = new BankAccount(10, “Sally”); acc.deposit(5000); acc.withdraw(acc.balance / 2);

Page 23: Lesson16

14. What is the value of balance after the following transactions?//refer to the BankAccount class you created on p 15-7

BankAccount acc = new BankAccount(10, “Sally”); acc.deposit(5000); acc.withdraw(acc.balance / 2); 2505

Page 24: Lesson16

15. What’s wrong with the following code?BankAccount b;

b.deposit(1000);

Page 25: Lesson16

15. What’s wrong with the following code?BankAccount b;b.deposit(1000);

b was never instantiated

Page 26: Lesson16

16. What’s wrong with the following code?BankAccount b new BankAccount(32.75, “Melvin”);

b = new BankAccount(1000,”Bob”); //ok to assign a new object to b b.deposit(“A thousand dollars”); //Wrong, deposit receives a double, not a String

Page 27: Lesson16

16. What’s wrong with the following codeBankAccount b new BankAccount(32.75, “Melvin”); //= sign missing between b and newb = new BankAccount(1000,”Bob”); //ok to assign a new object to bb.deposit(“A thousand dollars”); //Wrong, deposit receives a double, not a String

Page 28: Lesson16

17. What is printed in the following? String myString = “Yellow”; String yourString = “Yellow”; String hisString = new String(“Yellow”); String ourString = myString;System.out.println(myString = = yourString); System.out.println(myString = = ourString); System.out.println(myString.equals(yourString)); System.out.println(myString.equals(ourString)); System.out.println( myString = = hisString );

Page 29: Lesson16

17. What is printed in the following? String myString = “Yellow”; String yourString = “Yellow”; String hisString = new String(“Yellow”); String ourString = myString;System.out.println(myString = = yourString); // true…both references to

same objectSystem.out.println(myString = = ourString); //true…both references to

same objectSystem.out.println(myString.equals(yourString)); //true…contents are

sameSystem.out.println(myString.equals(ourString)); //true…contents are sameSystem.out.println( myString = = hisString ); //false… different objects