Editing 1188: Bonding

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 8: Line 8:
  
 
==Explanation==
 
==Explanation==
This is {{w|source code}} written in the {{w|Java (programming language)|Java programming language}} which models a parent and a child playing a {{w|Catch (game)|game of catch}}.  Normally this game is played with the parent throwing a ball to their child, who catches it and throws it back, and repeated back-and-forth. The comic title "Bonding" refers to the {{w|Paternal bond|building of relationship}} between the parent and the child. The joke lies in the puns using the words <code>try</code>, <code>throw</code>, <code>catch</code>, and <code>Throwable</code>.  These can refer to actions in the real-life game, but are also keywords in the Java language that are used for {{w|exception handling}}, a method of signaling error conditions and responding to them.  Also, the terms "parent" and "child" are usually interpreted more abstractly in programming, as generic terms used in hierarchical {{w|Data structure|data structures}}.
+
This is {{w|source code}} written in the {{w|Java (programming language)|Java programming language}}, which models a parent and a child playing a {{w|Catch (game)|game of catch}}, in which the parent throws a ball to their child, who catches it and throws it back, which repeats. The comic title "Bonding" refers to the {{w|Paternal bond|building of relationship}} between the parent and the child. But the computer program representation of this game is funny, using a play on the keywords <code>throw</code> and <code>catch</code> (and <code>Throwable</code>), which, in Java, do not correspond to throwing and catching a ball, but to a so-called {{w|exception handling}}, a method of signaling error conditions and responding to them.
  
The program, as written, will {{w|Recursion (computer science)|recursively}} call the <code>aim</code> method alternately on the parent and the child indefinitely, causing each to take turns throwing and catching the <code>Ball</code> object.  Note that unlike the real game, this program actually has the same person both throwing and catch the same ball on their turn.  The ball is passed onto the other person by ''aiming'' it at them, which causes the person to both throw and catch the ball, and ''aim'' it back, perpetuating the cycle.  This program will also eventually crash with a {{w|stack overflow}} error.
+
The program, as written, will {{w|Recursion (computer science)|recursively}} call the <code>aim</code> method alternately on the parent and the child indefinitely, until the program crashes with a {{w|stack overflow}}.
  
The title text refers to the [http://www.eclipse.org/ Eclipse IDE], which is a tool commonly used to develop software in Java. "Building character" is something that you would expect a parent to do, in order to instill in his child positive traits, such as confidence and athleticism. This is possibly a reference to {{w|Calvin_and_Hobbes|Calvin and Hobbes}}, where Calvin's dad often encourages him to build character in a number of ways, including playing baseball. This is made more likely by other references combining technology with Calvin and Hobbes, such as xkcd comics [[409: Electric Skateboard (Double Comic)]], [[702: Snow Tracking]] and [[1002: Game AIs]]. However, here, "build" might also be a play on the term of "{{w|Software build|building}}" a program, while "{{w|Character_(computing)|character}}" refers to a data type in programming languages. It may also refer to the common notion that programming in C++ or Java builds character due to their powerful but sometimes finicky libraries.
+
Also, the terms "parent" and "child" are usually interpreted more abstractly in programming, as a generic terms used in hierarchical {{w|Data structure|data structures}}.
 +
 
 +
The title text refers to [http://www.eclipse.org/ Eclipse], which is a tool commonly used to develop software in Java. "Building character" is something that you would expect a parent to do, in order to instill in his child positive traits, such as athleticism. However, here, "build" might be a play on the term of "{{w|Software build|building}}" a program, while "character" refers to a data type. It may also refer to the common notion that programming in C++ or Java builds character due to their powerful but sometimes finicky libraries.
  
 
===Program description===
 
===Program description===
 
To compile this {{w|Java_(programming_language)|Java}} source code, the two [http://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html classes] would need to be in a .java file.
 
To compile this {{w|Java_(programming_language)|Java}} source code, the two [http://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html classes] would need to be in a .java file.
The program defines two classes (types of objects):
+
#The Ball class [http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html extends] [http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html Throwable], making it possible to use an instance of Ball in [http://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html exception handling].
#The Ball class [http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html extends] [http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html Throwable], making it possible to use an instance of Ball in [http://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html exception handling].  In English, this means "a Ball is a kind of Throwable object".
+
#The P (short for Person) class contains the following members:
#The P class, representing a Person, which contains the following members (attributes):
+
:*a [http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html class variable] 'target' to hold another instance of P to aim an instance of Ball at.
#*a [http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html class variable] 'target' to point to another P to aim a Ball at.
+
:*a [http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html constructor] 'P' (in Java the constructor always has the same name as the class) used to create an instance of P and initialize its state (with a target). The keyword [http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html this] refers to the current instance of P.
#*a [http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html constructor] 'P' (in Java the constructor always has the same name as the class) used to create an instance of P and initialize its state (with a target). The keyword [http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html this] refers to the current instance of P.
+
:*a [http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html method] 'aim' that takes an instance of Ball named 'ball' as a parameter.
#*a [http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html method] 'aim' that takes an instance of Ball named 'ball' as a parameter.  This contains the code to actually throw, catch, and pass the ball onto the target.
+
:*a [http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html static] method 'main' which is called when executing this class.
#*a [http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html static] method 'main' which is called when executing this class.  This is the code that sets up the game and starts the process.
 
  
 
The program executes in the following order:
 
The program executes in the following order:
#The static main method is called.  It sets up the game by doing the following:
+
#The static main method is called (with the command line arguments in the 'args' parameter).
##An instance of P named 'parent' is created without a target ({{w|Nullable_type|null}}) using the 'new' keyword.
+
#An instance of P is created without a target ({{w|Nullable_type|null}}) using the 'new' keyword and assigned to a variable named 'parent'.
##Another instance of P named 'child' is created with 'parent' as its target.
+
#Another instance of P is created with 'parent' as target and assigned to a variable named 'child'.
##The parent's target is assigned to be the child.  Unlike with 'child', setting the parent's target could not be done at the moment when 'parent' was created because its target (the child) has not yet been created at the time. This is why the code for parent and child don't look alike despite this being a symmetrical setup.
+
#The target variable of the parent is changed to the child instance.
#The game begins by having the parent aim a new instance of Ball.
+
#An instance of Ball is created and passed as a parameter to the aim method of parent.
#The aim method first sets up a [http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html try] block to handle exceptions.  A "try" block is required in Java in order to "catch" later.
+
#The aim method first sets up a [http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html try] block to handle exceptions.
#Next, the Ball instance 'ball' is [http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html thrown].  This signals an exception situation and triggers the [http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html catch] block below.
+
#Next, the Ball instance 'ball' is [http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html thrown] (= signal an exception situation).
#In the catch block, the aim method of the target of the P instance is called with the Ball instance (now referred to as 'b').
+
#The thrown exception (= the Ball instance) is handled in the [http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html catch] block with the matching exception class.
#The target now executes its own aim method, which is the same code continuing from step 3 except with the current class instance ('this') and its target switched between the parent and the child.
+
#In the catch block the aim method of the target of the P instance is called with the Ball instance (now referred to as 'b').
 +
#The code continues with step 6, but the current class instance ('this') has changed from parent to child (or vv.).
 +
 
 +
Notes about code quality:
 +
#Good Java code should contain documentation in the form of {{w|Javadoc}} comments.
 +
#It is recommended to organize Java source code into [http://docs.oracle.com/javase/tutorial/java/package/packages.html packages] to make code easier to find and use and to avoid name conflicts.
 +
#The class interface should not expose inner logic and data. For this reason class variables are usually declared as [http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html private] (or protected) and separate public methods (called getters and setters) are created to access the data. Making target private would also require using the setTarget setter method in main to set the parent target to child.
 +
#In most cases constructors should call their superclass constructor first. P descents from Object, which has an empty constructor, so this is not strictly required here.
 +
#The aim method references the class variable target directly. To avoid future name conflicts it is recommended to prefix the reference to target with 'this' or use the getter from #2.
  
 
==Transcript==
 
==Transcript==

Please note that all contributions to explain xkcd may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see explain xkcd:Copyrights for details). Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel | Editing help (opens in new window)