SlideShare ist ein Scribd-Unternehmen logo
1 von 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.
Lesson 16
Private methods can only be accessed from
within the class itself.
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.
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 type
Circle 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.
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
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).
Strings
String s1 = “Hello”;
String s2 = “Hello”; //s1 and s2 are String
constants
System.out.println(s1 = = s2); // prints true
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.
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?
1.true
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?
2-5. All False
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?
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.
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?
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 private
8. Is int cv = oscarMayer.method3( );
legal? If not, why? Yes
9. 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.
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.)
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( );
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
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
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);
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
15. What’s wrong with the following code?
BankAccount b;
b.deposit(1000);
15. What’s wrong with the following code?
BankAccount b;b.deposit(1000);
b was never instantiated
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
16. What’s wrong with the following code
BankAccount b new BankAccount(32.75, “Melvin”);
//= sign missing between b and new
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
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 );
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 object
System.out.println(myString = = ourString); //true…both
references to same object
System.out.println(myString.equals(yourString)); //true…
contents are same
System.out.println(myString.equals(ourString)); //true…
contents are same
System.out.println( myString = = hisString ); //false… different
objects

Weitere ähnliche Inhalte

Ähnlich wie Lesson16

Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptOUM SAOKOSAL
 
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfN Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfakashreddy966699
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil Witecki
 
Course.DS_Store__MACOSXCourse._.DS_StoreCourseassert.docx
Course.DS_Store__MACOSXCourse._.DS_StoreCourseassert.docxCourse.DS_Store__MACOSXCourse._.DS_StoreCourseassert.docx
Course.DS_Store__MACOSXCourse._.DS_StoreCourseassert.docxvanesaburnand
 
Java level 1 Quizzes
Java level 1 QuizzesJava level 1 Quizzes
Java level 1 QuizzesSteven Luo
 
JAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESJAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESNikunj Parekh
 
08review (1)
08review (1)08review (1)
08review (1)IIUM
 
Explanations to the article on Copy-Paste
Explanations to the article on Copy-PasteExplanations to the article on Copy-Paste
Explanations to the article on Copy-PastePVS-Studio
 
IntroductionFor this program, you will implement an interface that.pdf
IntroductionFor this program, you will implement an interface that.pdfIntroductionFor this program, you will implement an interface that.pdf
IntroductionFor this program, you will implement an interface that.pdfebrahimbadushata00
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & AnswersRatnala Charan kumar
 
The Ring programming language version 1.2 book - Part 78 of 84
The Ring programming language version 1.2 book - Part 78 of 84The Ring programming language version 1.2 book - Part 78 of 84
The Ring programming language version 1.2 book - Part 78 of 84Mahmoud Samir Fayed
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Workhorse Computing
 
Hardened JavaScript
Hardened JavaScriptHardened JavaScript
Hardened JavaScriptKrisKowal2
 

Ähnlich wie Lesson16 (20)

Easy mockppt
Easy mockpptEasy mockppt
Easy mockppt
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core Concept
 
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdfN Queen Problem using Branch And Bound - GeeksforGeeks.pdf
N Queen Problem using Branch And Bound - GeeksforGeeks.pdf
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
Course.DS_Store__MACOSXCourse._.DS_StoreCourseassert.docx
Course.DS_Store__MACOSXCourse._.DS_StoreCourseassert.docxCourse.DS_Store__MACOSXCourse._.DS_StoreCourseassert.docx
Course.DS_Store__MACOSXCourse._.DS_StoreCourseassert.docx
 
Java level 1 Quizzes
Java level 1 QuizzesJava level 1 Quizzes
Java level 1 Quizzes
 
Easy mock
Easy mockEasy mock
Easy mock
 
JAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESJAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICES
 
Jist of Java
Jist of JavaJist of Java
Jist of Java
 
08review (1)
08review (1)08review (1)
08review (1)
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
Explanations to the article on Copy-Paste
Explanations to the article on Copy-PasteExplanations to the article on Copy-Paste
Explanations to the article on Copy-Paste
 
JS
JSJS
JS
 
IntroductionFor this program, you will implement an interface that.pdf
IntroductionFor this program, you will implement an interface that.pdfIntroductionFor this program, you will implement an interface that.pdf
IntroductionFor this program, you will implement an interface that.pdf
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
The Ring programming language version 1.2 book - Part 78 of 84
The Ring programming language version 1.2 book - Part 78 of 84The Ring programming language version 1.2 book - Part 78 of 84
The Ring programming language version 1.2 book - Part 78 of 84
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Hardened JavaScript
Hardened JavaScriptHardened JavaScript
Hardened JavaScript
 

Mehr von cybernaut

Health promotion week_2-1(1)
Health promotion week_2-1(1)Health promotion week_2-1(1)
Health promotion week_2-1(1)cybernaut
 
Presentation1
Presentation1Presentation1
Presentation1cybernaut
 
Module3 new technologies
Module3 new technologiesModule3 new technologies
Module3 new technologiescybernaut
 
Module1 Intro
Module1 IntroModule1 Intro
Module1 Introcybernaut
 

Mehr von cybernaut (7)

Health promotion week_2-1(1)
Health promotion week_2-1(1)Health promotion week_2-1(1)
Health promotion week_2-1(1)
 
Apps
AppsApps
Apps
 
Presentation1
Presentation1Presentation1
Presentation1
 
KCGI
KCGIKCGI
KCGI
 
Module3 new technologies
Module3 new technologiesModule3 new technologies
Module3 new technologies
 
Module2
Module2Module2
Module2
 
Module1 Intro
Module1 IntroModule1 Intro
Module1 Intro
 

Kürzlich hochgeladen

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 

Lesson16

  • 1. 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.
  • 3. Private methods can only be accessed from within the class itself.
  • 4. 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.
  • 5. 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 type Circle 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.
  • 6. 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
  • 7. 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).
  • 8. Strings String s1 = “Hello”; String s2 = “Hello”; //s1 and s2 are String constants System.out.println(s1 = = s2); // prints true
  • 9. 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.
  • 10. 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?
  • 12. 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?
  • 14. 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?
  • 15. 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.
  • 16. 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?
  • 17. 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 private 8. Is int cv = oscarMayer.method3( ); legal? If not, why? Yes 9. 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.
  • 18. 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.)
  • 19. 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( );
  • 20. 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
  • 21. 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
  • 22. 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);
  • 23. 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
  • 24. 15. What’s wrong with the following code? BankAccount b; b.deposit(1000);
  • 25. 15. What’s wrong with the following code? BankAccount b;b.deposit(1000); b was never instantiated
  • 26. 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
  • 27. 16. What’s wrong with the following code BankAccount b new BankAccount(32.75, “Melvin”); //= sign missing between b and new 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
  • 28. 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 );
  • 29. 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 object System.out.println(myString = = ourString); //true…both references to same object System.out.println(myString.equals(yourString)); //true… contents are same System.out.println(myString.equals(ourString)); //true… contents are same System.out.println( myString = = hisString ); //false… different objects