Skip to content

Commit 71444f5

Browse files
committed
Light edits to "Lets look at a Java Program"
1 parent 7fc069f commit 71444f5

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

‎_sources/Java4Python/firstjavaprogram.rst‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Now lets look at the same program written in Java:
3939
}
4040

4141
What 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
4343
around the edges that make it harder to see the core of the program. Do
4444
not worry! An important skill for a computer scientist is to learn what
4545
to ignore and what to look at carefully. You will soon find that there
@@ -88,7 +88,7 @@ from compiling:
8888

8989
- Early detection of errors
9090

91-
- Faster Program Execution
91+
- Faster program execution
9292

9393
The job of the compiler is to turn your java code into language that the
9494
Java Virtual Machine (JVM) can understand. We call the code that the JVM
@@ -106,7 +106,7 @@ Now that we have run our hello world program, lets go back and look at
106106
it carefully to see what we can learn about the Java language. This
107107
simple example illustrates a few very important rules:
108108

109-
1. Every Java program must define a class, all code is inside a class.
109+
1. Every Java program must define a class, and all code is inside a class
110110

111111
2. Everything in Java must have a type
112112

@@ -115,7 +115,13 @@ simple example illustrates a few very important rules:
115115

116116
Lets take the hello world example a line at a time to see how these
117117
rules are applied. On line 1 we see that we are declaring a class called
118-
Hello. As rule 1 says all Java code resides inside a class. Unlike
118+
Hello:
119+
120+
::
121+
122+
public class Hello {
123+
124+
As rule 1 says all Java code resides inside a class. Unlike
119125
Python where a program can simply be a bunch of statements in a file,
120126
Java programs must be inside a class. So, we define a class ``Hello``,
121127
which is not a very useful class because it has no instance variables, and only one
@@ -130,19 +136,19 @@ is:
130136

131137
::
132138

133-
public static void main(String[] args)!
139+
public static void main(String[] args)
134140

135141
Everything on this line is significant, and helps in the identification
136142
of this method. For example the following lines look similar but are in
137143
fact treated by Java as completely different methods:
138144

139-
- ``public void main(String[] args)``
145+
- ``public void main(String[] args)``
140146

141-
- ``public static void main(String args)``
147+
- ``public static void main(String args)``
142148

143-
- ``public static void main()``
149+
- ``public static void main()``
144150

145-
- ``void main(String args)``
151+
- ``void main(String args)``
146152

147153
Just digging in to this one line will take us deep into the world of
148154
Java, so we are going to start digging but we are not going to dig too
@@ -166,7 +172,7 @@ instance, for example the math module contains many methods: sin, cos,
166172
etc. You probably evaluated these methods using the names
167173
``math.cos(90)`` or ``math.sin(60)``.
168174

169-
The next word, ``void`` tells the Java compiler that the method ``main``
175+
The next word, **void** tells the Java compiler that the method ``main``
170176
will not return a value. This is roughly analogous to omitting the
171177
return statement in a Python method. In other words, the method will run
172178
to completion and exit but will not return a value that you can use in
@@ -176,7 +182,7 @@ will return. This is in keeping with the rule that says everything in
176182
Java must have a type. In this case we use the special type called
177183
``void`` which means no type.
178184

179-
Next we have the proper name for the method: ``main``. The rules for
185+
Next we have the proper name for the method: **main**. The rules for
180186
names in Java are similar to the rules in Python. Names can include
181187
letters, numbers, and the ``_``. Names in Java must start with a letter.
182188

@@ -191,9 +197,14 @@ that if you call ``main`` somewhere else in your code and and pass it an
191197
array of integers or even a single string, the compiler will flag it as
192198
an error.
193199

194-
That is a lot of new material to digest in only a single line of Java.
200+
That is a lot of new material to digest in only a single line of Java!
195201
Lets press on and look at the next line:
196-
``System.out.println("Hello World!");``. This line should look a bit
202+
203+
::
204+
205+
System.out.println("Hello World!");
206+
207+
This line should look a bit
197208
more familiar to you. Python and Java both use the dot notation for
198209
finding names. In this example we start with ``System``. System is a
199210
class. Within the system class we find the object named ``out``. The
@@ -206,10 +217,13 @@ method in Java.
206217

207218
Now there is one more character on this line that is significant and
208219
that is the ``;`` at the end. In Java the ``;`` signifies the end of a
209-
statement. Unlike Python where statements are almost always only one
210-
line long, Java statements can spread across many lines. The compiler
220+
statement. Java statements can spread across many lines, but the compiler
211221
knows it has reached the end of a statement when it encounters a ``;``.
212-
This is a very important difference to remember. In Java the following
222+
In Python, it is not required (or recommend) to use semicolons in this way,
223+
but whitespace is meaningful.
224+
In contrast, in Java semicolons are **required** to end statements, but
225+
whitespace is not considered meaningful.
226+
This is a very important difference to remember! In Java, the following
213227
statements are all legal and equivalent. I would not encourage you to
214228
write your code like this, but you should know that it is legal.
215229

@@ -228,7 +242,7 @@ write your code like this, but you should know that it is legal.
228242
;
229243

230244
The last two lines of the hello world program simply close the two
231-
blocks. The first or outer block is the class definition. The second or
245+
blocks using ``}``. The first or outer block is the class definition. The second or
232246
inner block is the function definition.
233247

234248
If we wanted to translate the Java back to Python we would have

0 commit comments

Comments
 (0)