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
+9-10Lines changed: 9 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -163,16 +163,15 @@
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"/>.
176
175
</p>
177
176
178
177
<listingxml:id="python-hello-world">
@@ -185,7 +184,7 @@
185
184
</listing>
186
185
187
186
<p>
188
-
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"/>.
189
188
</p>
190
189
191
190
<listingxml:id="python-hello-world-run">
@@ -214,13 +213,13 @@ public class Hello {
214
213
</listing>
215
214
216
215
<p>
217
-
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.
218
217
</p>
219
218
220
219
<p>
221
220
<idx>interpreter</idx>
222
221
<idx>compile</idx>
223
-
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"/>.
224
223
</p>
225
224
226
225
@@ -242,7 +241,7 @@ $ ls -l Hello.*
242
241
</p>
243
242
244
243
<p>
245
-
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"/>.
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>.
463
462
In Python, it is not required (or recommend) to use semicolons in this way, but whitespace is meaningful.
464
463
In contrast, in Java semicolons are <term>required</term> to end statements, but whitespace is not considered meaningful.
465
-
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.
466
465
I would not encourage you to write your code like this, but you should know that it is legal.
467
466
</p>
468
467
@@ -491,7 +490,7 @@ System.
491
490
</p>
492
491
493
492
<p>
494
-
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-hello-world"text="type-global"/> class definition.
0 commit comments