Finding the Code Responsible for Behavior
|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, overloading and overriding.|
|Prerequisite Knowledge||Invocation of Java methods (especially System.out.println and overloading), some inheritance concepts, some familiarity with the toString() method and overriding.|
|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 4) what an overload is (println) 5) what an override is (toString)|
Students should be able to create an ArrayList and print it to the console (without a for loop). Students should also be able to read the Java API and understand inheritance at a high level.
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
- 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?
- 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 dissect.
Part 2 - Look at the Source Code in OpenJDK for Java
- OpenJDK (Open Java Development Kit) is a free and open source implementation of the Java programming language.
- If you do not already have it downloaded, discover how you can download the source code for Java from OpenJDK.
- Understand the sub-directory structure. How does this relate to package/import statements?
- Open the java source code for the class that you determined contains the toString that is executed to produce the output [a, b, c, d].
Part 3 - Find the code
- Find the toString in the Java source code that is executed to produce the output [a, b, c, d].
- Read and understand this method.
- Copy and paste the code in the method, and explain the method in English.
- Indicate the line of code that produces the "[".
- Indicate the line of code that produces the "]".
- Indicate the line of code the places the comma and space between the elements in the ArrayList.
Part 4 - Open source vs. licensed code
- Oracle makes this source code available. However, it is licensed and owned by Oracle. Therefore, can you modify this code? Is this open source??
- What would be the proper way to change the way an ArrayList is output to a console.
A document with answers to questions
|Knowledge Area/Knowledge Unit||PL|
|Topic||PL/Object Oriented Programming|
|Level of Difficulty||Easy|
|Estimated Time to Completion||20 minutes|
|Materials/Environment||Access to Java OpenJDK 7 and access to Java 7 API|
|License||Licensed CC BY-SA|
Suggestions for the Open Source Project:
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License