diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index 8e7cdb8..b00d4cf 100644 --- a/source/ch2_firstjavaprogram.ptx +++ b/source/ch2_firstjavaprogram.ptx @@ -163,37 +163,45 @@
Now, we have a full class definition and have utilized its methods. Class definitions in Java will be covered thoroughly in chapter 6. For now, it is important to know that Python programs can be written without using classes at all. Java, on the other hand, requires all code to reside in a class. This will be discussed in the next section.
- - - + +
A time-honored tradition in Computer Science is to write a program called “Hello World.” The “Hello World” program is simple and easy.
There are no logic errors to make, so getting it to run relies only on understanding the syntax.
- To be clear, lets look at a “complicated” version of hello world for Python:
+ To be clear, lets look at a “complicated” version of hello world for Python in
def main():
print("Hello World!")
- Remember that we can define this program right at the Python command line and then run it:
+ Remember that we can define this program right at the Python command line and then run it as per
>>> main() -Hello World!+
+>>> main()
+Hello World!
+ Now let's look at the same program written in Java:
-
public class Hello {
public static void main(String[] args) {
@@ -202,19 +210,21 @@ public class Hello {
}
- What we see is that at the core there are a few similarities, such as a main and the string “Hello World”. However, there is a lot more stuff around the edges that make it harder to see the core of the program. Do not worry! An important skill for a computer scientist is to learn what to ignore and what to look at carefully. You will soon find that there are some elements of Java that will fade into the background as you become used to seeing them.
+ Based on
$ javac Hello.java
$ ls -l Hello.*
@@ -222,6 +232,7 @@ $ ls -l Hello.*
-rw-r--r-- 1 bmiller bmiller 117 Jul 19 17:46 Hello.java
The command
- Now that we have compiled our java source code we can run the compiled code using the
$ java Hello
Hello World!
$
Now you may be wondering what good is that extra step? What does compiling do for us? There are a couple of important benefits we get from compiling: @@ -310,7 +324,7 @@ $ On line 1 we see that we are declaring a class called Hello:
- +
public class Hello {
@@ -447,12 +461,12 @@ System.out.println("Hello World!");
Java statements can spread across many lines, but the compiler knows it has reached the end of a statement when it encounters a ; .
In Python, it is not required (or recommend) to use semicolons in this way, but whitespace is meaningful.
In contrast, in Java semicolons are required to end statements, but whitespace is not considered meaningful.
- This is a very important difference to remember! In Java, the following statements are all legal and equivalent.
+ This is a very important difference to remember! In Java, the statements in are all legal and equivalent.
I would not encourage you to write your code like this, but you should know that it is legal.
-
-
+
+
System.out.println("Hello World");
System.out.println("Hello World")
@@ -467,6 +481,7 @@ System.
;
+
The last two lines of the hello world program simply close the two blocks using } .
@@ -475,11 +490,11 @@ System.
- If we wanted to translate the Java back to Python we would have something like the following class definition.
+ If we wanted to translate the Java back to Python we would have something like class definition.
-
-
+
+
class Hello(object):
@staticmethod
@@ -487,20 +502,24 @@ class Hello(object):
print("Hello World!")
+
Notice that we used the decorator @staticmethod to tell the Python interpreter that main is going to be a static method.
- The impact of this is that we don’t have to, indeed we should not, use self as the first parameter of the main method! Using this definition we can call the main method in a Python session like this:
+ The impact of this is that we don’t have to, indeed we should not, use self as the first parameter of the main method! Using this definition we can call the main method in a Python session like .
-
+
+
>>> Hello.main("")
Hello World!
>>>
+
+