You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch2_firstjavaprogram.ptx
+42-23Lines changed: 42 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -163,37 +163,45 @@
163
163
<p>
164
164
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.
165
165
</p>
166
-
167
-
168
-
166
+
169
167
</section>
168
+
170
169
<sectionxml:id="sec-lets-look-at-a-java-program">
171
170
<title>Lets look at a Java Program</title>
172
171
<p>
173
172
A time-honored tradition in Computer Science is to write a program called “Hello World.” The “Hello World” program is simple and easy.
174
173
There are no logic errors to make, so getting it to run relies only on understanding the syntax.
175
-
To be clear, lets look at a “complicated” version of hello world for Python:
174
+
To be clear, lets look at a “complicated” version of hello world for Python in <xrefref="python-hello-world"text="type-global"/>.
Remember that we can define this program right at the Python command line and then run it:
187
+
Remember that we can define this program right at the Python command line and then run it as per <xrefref="python-hello-world-run"text="type-global"/>.
187
188
</p>
188
189
189
-
<pre>>>> main()
190
-
Hello World! </pre>
190
+
<listingxml:id="python-hello-world-run">
191
+
<programlanguage="python">
192
+
<code>
193
+
>>> main()
194
+
Hello World! </code>
195
+
</program>
196
+
</listing>
197
+
191
198
<p>
192
199
Now let's look at the same program written in Java:
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.
216
+
Based on <xrefref="java-world"text="type-global"/>, what we see in the code 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.
208
217
</p>
209
218
210
219
<p>
211
220
<idx>interpreter</idx>
212
221
<idx>compile</idx>
213
-
The first question you probably have about this little program is “How do I run it?” Running a Java program is not as simple as running a Python program. The first thing you need to do with a Java program is compile it. The first big difference between Java and Python is that Python is an interpreted language. We could run our Python programs in the Python <term>interpreter</term> and we were quite happy to do that. Java makes running programs a two step process. First we must type the hello world program into a file and save that file using the name <c>Hello.java</c> The file name must be the same as the public class you define in the file. Once we have saved the file we <term>compile</term> it from the command line as follows:
222
+
The first question you probably have about this little program is “How do I run it?” Running a Java program is not as simple as running a Python program. The first thing you need to do with a Java program is compile it. The first big difference between Java and Python is that Python is an interpreted language. We could run our Python programs in the Python <term>interpreter</term> and we were quite happy to do that. Java makes running programs a two step process. First we must type the hello world program into a file and save that file using the name <c>Hello.java</c> The file name must be the same as the public class you define in the file. Once we have saved the file we <term>compile</term> it from the command line as <xrefref="java-javac"text="type-global"/>.
The command <c>javac</c> compiles our java source code into compiled byte code and saves it in a file called <c>Hello.class</c>.
@@ -230,17 +241,20 @@ $ ls -l Hello.*
230
241
</p>
231
242
232
243
<p>
233
-
Now that we have compiled our java source code we can run the compiled code using the <c>java</c> command.
244
+
Now that we have compiled our java source code we can run the compiled code using the <c>java</c> command as per <xrefref="java-run-hello"text="type-global"/>.
234
245
</p>
235
246
236
247
237
-
<programxml:id="java-run-hello"language="java">
248
+
<listingxml:id="java-run-hello">
249
+
<programlanguage="java">
238
250
<code>
239
251
$ java Hello
240
252
Hello World!
241
253
$
242
254
</code>
243
255
</program>
256
+
</listing>
257
+
244
258
245
259
<p>
246
260
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 @@ $
310
324
On line 1 we see that we are declaring a class called Hello:
Java statements can spread across many lines, but the compiler knows it has reached the end of a statement when it encounters a <c>;</c>.
448
462
In Python, it is not required (or recommend) to use semicolons in this way, but whitespace is meaningful.
449
463
In contrast, in Java semicolons are <term>required</term> to end statements, but whitespace is not considered meaningful.
450
-
This is a very important difference to remember! In Java, the following statements are all legal and equivalent.
464
+
This is a very important difference to remember! In Java, the statements in <xrefref="java-messy-code"text="type-global"/> are all legal and equivalent.
451
465
I would not encourage you to write your code like this, but you should know that it is legal.
452
466
</p>
453
467
454
-
455
-
<programxml:id="java-messy-code"language="java">
468
+
<listingxml:id="java-messy-code">
469
+
<programlanguage="java">
456
470
<code>
457
471
System.out.println("Hello World");
458
472
System.out.println("Hello World")
@@ -467,6 +481,7 @@ System.
467
481
;
468
482
</code>
469
483
</program>
484
+
</listing>
470
485
471
486
<p>
472
487
The last two lines of the hello world program simply close the two blocks using <c>}</c>.
@@ -475,32 +490,36 @@ System.
475
490
</p>
476
491
477
492
<p>
478
-
If we wanted to translate the Java back to Python we would have something like the following class definition.
493
+
If we wanted to translate the Java back to Python we would have something like <xrefref="python-static-method"text="type-global"/> class definition.
Notice that we used the decorator <c>@staticmethod</c> to tell the Python interpreter that <c>main</c> is going to be a static method.
493
-
The impact of this is that we don’t have to, indeed we should not, use <c>self</c> as the first parameter of the main method! Using this definition we can call the main method in a Python session like this:
509
+
The impact of this is that we don’t have to, indeed we should not, use <c>self</c> as the first parameter of the main method! Using this definition we can call the main method in a Python session like <xrefref="python-static-method-run"text="type-global"/>.
0 commit comments