SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Semelhante a Lesson16

Copy of repast javagettingstarted
Copy of repast javagettingstartedCopy of repast javagettingstarted
Copy of repast javagettingstartedNimish Verma
 
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
 

Semelhante a Lesson16 (20)

Copy of repast javagettingstarted
Copy of repast javagettingstartedCopy of repast javagettingstarted
Copy of repast javagettingstarted
 
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
 

Mais de 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
 

Mais de 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
 

Último

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Último (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

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