11Lets look at a Java Program
22---------------------------
33
4- A time honored tradition in Computer Science is to write a program
4+ A time- honored tradition in Computer Science is to write a program
55called “hello world.” The “hello world” program is simple and easy.
66There are no logic errors to make, so getting it to run relies only on
7- understanding the syntax. To be clear lets look a a “complicated”
7+ understanding the syntax. To be clear, lets look at a “complicated”
88version of hello world for Python:
99
1010::
@@ -39,7 +39,7 @@ Now lets look at the same program written in Java:
3939 }
4040
4141What we see is that at the core there are a few similarities, such as a
42- main and the string “Hello World” However there is a lot more stuff
42+ main and the string “Hello World” However, there is a lot more stuff
4343around the edges that make it harder to see the core of the program. Do
4444not worry! An important skill for a computer scientist is to learn what
4545to ignore and what to look at carefully. You will soon find that there
@@ -97,10 +97,10 @@ the Python interpreter interprets your Python. However since byte code
9797is much closer to the native language of the computer it can run faster.
9898
9999When the compiler does the translation it can find many different kinds
100- of errors. For example if you make a typo the compiler will find the
100+ of errors. For example, if you make a typo, the compiler will find the
101101typo and point it out to you before you ever run the program. We will
102102look at some examples of compiler errors shortly. Chances are you will
103- create some on your own very soon too.
103+ create some on your own very soon, too.
104104
105105Now that we have run our hello world program, lets go back and look at
106106it carefully to see what we can learn about the Java language. This
@@ -117,13 +117,13 @@ Lets take the hello world example a line at a time to see how these
117117rules are applied. On line 1 we see that we are declaring a class called
118118Hello. As rule 1 says all Java code resides inside a class. Unlike
119119Python where a program can simply be a bunch of statements in a file,
120- Java programs must be inside a class. So, we define a class, ``Hello ``
121- is not a very useful class it has no instance variables, and only one
122- method. You will also notice the curly brace ``{ `` In Java blocks of
120+ Java programs must be inside a class. So, we define a class ``Hello ``,
121+ which is not a very useful class because it has no instance variables, and only one
122+ method. You will also notice the curly brace ``{ ``. In Java, blocks of
123123code are identified by pairs of curly braces. The block starts with a
124124``{ `` and ends with a ``} ``. You will notice that I indented my code
125125that followed the left brace, but in Java this is only done by
126- convention it is not enforced.
126+ convention, it is not enforced.
127127
128128On the next line we start our method definition. The name of this method
129129is:
@@ -158,17 +158,17 @@ The next word, **static** tells Java that this is a method that is part
158158of the class, but is not a method for any one instance of the class. The
159159kind of methods we typically wrote in Python required an instance in
160160order for the method to be called. With a static method, the object to
161- the left of the . is a class, not an instance of the class. For example
161+ the left of the `` . `` is a class, not an instance of the class. For example,
162162the way that we would call the ``main `` method directly is:
163- ``Hello.main(parameter1) ``. For now you can think of static methods the
163+ ``Hello.main(parameter1) ``. For now, you can think of static methods the
164164same way you think of methods in Python modules that don’t require an
165165instance, for example the math module contains many methods: sin, cos,
166166etc. You probably evaluated these methods using the names
167167``math.cos(90) `` or ``math.sin(60) ``.
168168
169169The next word, ``void `` tells the Java compiler that the method ``main ``
170170will not return a value. This is roughly analogous to omitting the
171- return statement in a Python method. In other words the method will run
171+ return statement in a Python method. In other words, the method will run
172172to completion and exit but will not return a value that you can use in
173173an assignment statement. As we look at other examples we will see that
174174every Java function must tell the compiler what kind of an object it
@@ -180,13 +180,13 @@ Next we have the proper name for the method: ``main``. The rules for
180180names in Java are similar to the rules in Python. Names can include
181181letters, numbers, and the ``_ ``. Names in Java must start with a letter.
182182
183- Finally we have the parameter list for the method. In this example we
184- have one parameter. The name of the parameter is ``args `` however,
185- because everything in Java must have a type we also have to tell the
183+ Finally, we have the parameter list for the method. In this example we
184+ have one parameter. The name of the parameter is ``args ``, however,
185+ because everything in Java must have a type, we also have to tell the
186186compiler that the value of ``args `` is an array of strings. For the
187- moment You can just think of an array as being the same thing as a list
188- in Python. The practical benefit of declaring that the method main must
189- accept one parameter and the parameter must be a an array of strings is
187+ moment you can just think of an array as being the same thing as a list
188+ in Python. The practical benefit of declaring that the method `` main `` must
189+ accept one parameter and the parameter must be an array of strings is
190190that if you call ``main `` somewhere else in your code and and pass it an
191191array of integers or even a single string, the compiler will flag it as
192192an error.
@@ -207,7 +207,7 @@ method in Java.
207207Now there is one more character on this line that is significant and
208208that is the ``; `` at the end. In Java the ``; `` signifies the end of a
209209statement. Unlike Python where statements are almost always only one
210- line long java statements can spread across many lines. The compiler
210+ line long, Java statements can spread across many lines. The compiler
211211knows it has reached the end of a statement when it encounters a ``; ``.
212212This is a very important difference to remember. In Java the following
213213statements are all legal and equivalent. I would not encourage you to
0 commit comments