Finding the Code Responsible for Behavior
(→Part 1 - Read the API) |
|||
Line 18: | Line 18: | ||
== Part 1 - Read the API == | == Part 1 - Read the API == | ||
− | # Find the println method in the API. There are many println methods. Which overloaded println is used when you println an ArrayList? What method does | + | # Find the println method in the API. There are many println methods. Which overloaded println is used when you println an ArrayList? What method does the println call according the to API? |
# Since println invokes String.valueOf, navigate to this method. What method does String.valueOf invoke? | # Since println invokes String.valueOf, navigate to this method. What method does String.valueOf invoke? | ||
# Now you can see that String.valueOf invokes the toString method of the object. What class does al belong to? Isn't it ArrayList? | # Now you can see that String.valueOf invokes the toString method of the object. What class does al belong to? Isn't it ArrayList? | ||
# Let's navigate to the ArrayList class. What class does ArrayList inherit its toString method from? This is the toString method we want to look at. | # Let's navigate to the ArrayList class. What class does ArrayList inherit its toString method from? This is the toString method we want to look at. | ||
− | |||
== Part 2 - Look at the Source Code for Java 7 == | == Part 2 - Look at the Source Code for Java 7 == |
Revision as of 14:57, 28 May 2015
Title | Finding the Code Responsible for Behavior | ||||||||||||||||
Overview | Java learners often wonder why certain behaviors are exhibited when a method is invoked. They tend to view the entire mechanism as "magic". This activity will show them that every behavior can be explained by looking at the code inside of the source class files (that are included in Java), while also strengthening their understanding of inheritance | ||||||||||||||||
Prerequisite Knowledge | Invocation of Java methods (especially System.out.println and overloading), some inheritance concepts, some familiarity with the toString() method, . | ||||||||||||||||
Learning Objectives | 1) Fully understand the interaction of the println method with the toString method 2) Inheritance 3) How to locate the code responsible for the behavior of a method
Background:Students should be able to create an ArrayList and it them out to the console without utilizing a for loop. Students should also be able to read the Java API and understand inheritance. Directions:I can easily create an ArrayList of String named al, and add "a", "b", "c" and "d" to it. Then I can quickly print it out using the line of code System.out.println(al); This produces the output [a, b, c, d] Have you ever wondered why the output looks like this? Why are those "weird" brackets included in the output? Why are there commas between each String? Let's discover the answer! Part 1 - Read the API
Part 2 - Look at the Source Code for Java 7
Part 3 - Find the code
Deliverables:A document with answers to questions Assessment:TBD Comments:None Additional Information:
Suggestions for the Open Source Project:Suggestions for an open source community member who is working in conjunction with the instructor.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License |