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
{{ message }}
Repository navigation
Commit 5ae6e4d
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: source/ch_1_introduction.ptx
+77-75Lines changed: 77 additions & 75 deletions
Original file line number
Diff line number
Diff line change
@@ -3,89 +3,80 @@
3
3
<!-- Generated by Docutils 0.19 -->
4
4
<chapterxml:id="introduction">
5
5
<title>Introduction</title>
6
-
<introduction>
7
-
<p>
8
-
This book assumes that you are already familiar with the <urlhref="http://www.python.org"visual="http://www.python.org">Python</url> programming language.
9
-
We will use Python as a starting point for our journey into <urlhref="http://java.sun.com"visual="http://java.sun.com">Java</url>.
10
-
We will begin by looking at a very simple Java program, just to see what the language looks like and how we get a program to run.
11
-
Next, we will look at the main constructs that are common to most programming languages:
12
-
</p>
13
6
14
-
<blockquote>
7
+
8
+
<sectionxml:id="sec-introduction">
9
+
<title>Introduction to Java for Python Programmers</title>
10
+
15
11
<p>
16
-
<ul>
17
-
<li>
18
-
<p>
19
-
Data Types
20
-
</p>
21
-
</li>
22
-
23
-
<li>
24
-
<p>
25
-
Loops
26
-
</p>
27
-
</li>
28
-
29
-
<li>
30
-
<p>
31
-
Reading user input
32
-
</p>
33
-
</li>
34
-
35
-
<li>
36
-
<p>
37
-
Conditionals
38
-
</p>
39
-
</li>
40
-
</ul>
12
+
This book assumes that you are already familiar with the <urlhref="http://www.python.org"visual="http://www.python.org">Python</url> programming language.
13
+
We will use Python as a starting point for our journey into <urlhref="http://java.sun.com"visual="http://java.sun.com">Java</url>.
14
+
We will begin by looking at a very simple Java program, just to see what the language looks like and how we get a program to run.
15
+
Next, we will look at the main constructs that are common to most programming languages:
41
16
</p>
42
-
</blockquote>
43
17
44
-
<p>
45
-
Once we have the basics of Java behind us we will move on to look at the features of Java that are both unique and powerful.
46
-
</p>
18
+
<blockquote>
19
+
<p>
20
+
<ul>
21
+
<li>
22
+
<p>
23
+
Data Types
24
+
</p>
25
+
</li>
26
+
27
+
<li>
28
+
<p>
29
+
Loops
30
+
</p>
31
+
</li>
32
+
33
+
<li>
34
+
<p>
35
+
Reading user input
36
+
</p>
37
+
</li>
38
+
39
+
<li>
40
+
<p>
41
+
Conditionals
42
+
</p>
43
+
</li>
44
+
</ul>
45
+
</p>
46
+
</blockquote>
47
47
48
-
<blockquote>
49
48
<p>
50
-
<ul>
51
-
<li>
52
-
<p>
53
-
Classes
54
-
</p>
55
-
</li>
56
-
57
-
<li>
58
-
<p>
59
-
Interfaces
60
-
</p>
61
-
</li>
62
-
63
-
<li>
64
-
<p>
65
-
Collections
66
-
</p>
67
-
</li>
68
-
69
-
<li>
70
-
<p>
71
-
Graphical User Interface Programming
72
-
</p>
73
-
</li>
74
-
75
-
<li>
76
-
<p>
77
-
Generic Programming
78
-
</p>
79
-
</li>
80
-
</ul>
49
+
Once we have the basics of Java behind us we will move on to look at the features of Java that are both unique and powerful.
<title>Why Learn another programming Language?</title>
6
9
7
-
<introduction>
8
10
<p>
9
11
Python is a nice language for beginning programming for several reasons.
10
12
First the syntax is sparse, and clear.
@@ -17,19 +19,19 @@
17
19
18
20
<p>
19
21
These languages have some advantages of their own.
20
-
First, is speed: Java and C++ code will generally give better performance than Python code <footnote_referenceauto="1"docname="Java4Python/whylearnjava"refid="pythonspeed">1</footnote_reference>.
22
+
First, is speed: Java and C++ code will generally give better performance than Python code. (See <xrefref="note-python-performance"/>.)
21
23
Second is their maintainability.
22
24
A lot of what makes Python easy to use is that you must remember certain things.
23
25
For example if you set variable <c>x</c> to reference a turtle, and forget later that <c>x</c> is a turtle but try to invoke a string method on it, you will get an error.
24
26
Java and C++ protect you by forcing you to be upfront and formal about the kind of object each variable is going to refer to.
25
27
</p>
26
28
27
-
<p>
28
-
In one sense Python is representative of a whole class of languages, sometimes referred to as “scripting languages.” Other languages in the same category as Python are Ruby and Perl.
29
-
Java is representative of what I will call industrial strength languages.
30
-
Industrial strength languages are good for projects with several people working on the project where being formal and careful about what you do may impact lots of other people.
31
-
Languages in this category include Rust, C++, C#, and Ada.
32
-
</p>
29
+
<p>
30
+
In one sense Python is representative of a whole class of languages, sometimes referred to as “scripting languages.” Other languages in the same category as Python are Ruby and Perl.
31
+
Java is representative of what I will call industrial strength languages.
32
+
Industrial strength languages are good for projects with several people working on the project where being formal and careful about what you do may impact lots of other people.
33
+
Languages in this category include Rust, C++, C#, and Ada.
34
+
</p>
33
35
34
36
<p>
35
37
Programming languages will always change.
@@ -39,7 +41,19 @@
39
41
And there are some features that are unique.
40
42
If you know what is common in languages that is a good place to start.
41
43
</p>
42
-
</introduction>
44
+
<notexml:id="note-python-performance">
45
+
<title>A Note about Python Performance</title>
46
+
47
+
48
+
<p>
49
+
50
+
Although Python code is generally slower than Java and C++ code, in practice Python programs can achieve equivalent performance.
51
+
This can be done by compiling Python code to C code (see: <urlhref="https://cython.org"visual="https://cython.org">Cython</url>) or by calling high-performance libraries from Python (e.g., <urlhref="https://numpy.org"visual="https://numpy.org">NumPy</url>, <urlhref="https://scikit-learn.org/stable/"visual="https://scikit-learn.org/stable/">scikit-learn</url>, etc.).
52
+
So native language performance is just one criteria to consider when deciding which language to use for a program.
53
+
</p>
54
+
</note>
55
+
56
+
</section>
43
57
44
58
<sectionxml:id="why-learn-java-why-not-c-or-c">
45
59
<title>Why Learn Java? Why not C or C++?</title>
@@ -80,12 +94,7 @@
80
94
But for these and other reasons, we’ve decided to use Java for this course.
81
95
Learning Java will be a good preparation for learning these and other languages!
Although Python code is generally slower than Java and C++ code, in practice Python programs can achieve equivalent performance.
86
-
This can be done by compiling Python code to C code (see: <urlhref="https://cython.org"visual="https://cython.org">Cython</url>) or by calling high-performance libraries from Python (e.g., <urlhref="https://numpy.org"visual="https://numpy.org">NumPy</url>, <urlhref="https://scikit-learn.org/stable/"visual="https://scikit-learn.org/stable/">scikit-learn</url>, etc.).
87
-
So native language performance is just one criteria to consider when deciding which language to use for a program.
Copy file name to clipboardExpand all lines: source/ch_3_firstjavaprogram.ptx
+12-5Lines changed: 12 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,8 @@ public class Hello {
36
36
</p>
37
37
38
38
<p>
39
+
<idx>interpreter</idx>
40
+
<idx>compile</idx>
39
41
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:
40
42
</p>
41
43
@@ -89,7 +91,9 @@ $
89
91
</p>
90
92
91
93
<p>
92
-
The job of the compiler is to turn your java code into language that the Java Virtual Machine (JVM) can understand.
94
+
<idx>JVM</idx>
95
+
<idx>byte code</idx>
96
+
The job of the compiler is to turn your java code into language that the Java Virtual Machine (<term>JVM</term>) can understand.
93
97
We call the code that the JVM understands <term>byte code</term>.
94
98
The JVM interprets the byte code much like the Python interpreter interprets your Python.
95
99
However since byte code is much closer to the native language of the computer it can run faster.
@@ -202,11 +206,14 @@ public static void main(String[] args)
202
206
</p>
203
207
204
208
<p>
209
+
<idx><c>public</c></idx>
210
+
<idx><c>protected</c></idx>
211
+
<idx><c>private</c></idx>
205
212
The first word, <term>public</term> indicates to the Java compiler that this is a method that anyone can call.
206
-
We will see that Java enforces several levels of security on the methods we write, including <term>public</term>, <term>protected</term>, and <term>private</term> methods.
213
+
We will see that Java enforces several levels of security on the methods we write, including public, <term>protected</term>, and <term>private</term> methods.
207
214
</p>
208
215
209
-
<p>
216
+
<p><idx><c>static</c></idx>
210
217
The next word, <term>static</term> tells Java that this is a method that is part of the class, but is not a method for any one instance of the class.
211
218
The kind of methods we typically wrote in Python required an instance in order for the method to be called.
212
219
With a static method, the object to the left of the <c>.</c> is a class, not an instance of the class.
@@ -215,7 +222,7 @@ public static void main(String[] args)
215
222
You probably evaluated these methods using the names <c>math.cos(90)</c> or <c>math.sin(60)</c>.
216
223
</p>
217
224
218
-
<p>
225
+
<p><idx><c>void</c></idx>
219
226
The next word, <term>void</term> tells the Java compiler that the method <c>main</c> will not return a value.
220
227
This is roughly analogous to omitting the return statement in a Python method.
221
228
In other words, the method will run to completion and exit but will not return a value that you can use in an assignment statement.
@@ -224,7 +231,7 @@ public static void main(String[] args)
224
231
In this case we use the special type called <c>void</c> which means no type.
225
232
</p>
226
233
227
-
<p>
234
+
<p><idx><c>main</c></idx>
228
235
Next we have the proper name for the method: <term>main</term>.
229
236
The rules for names in Java are similar to the rules in Python.
230
237
Names can include letters, numbers, and the <c>_</c>.
Copy file name to clipboardExpand all lines: source/ch_4_javadatatypes.ptx
+21-6Lines changed: 21 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -4,13 +4,9 @@
4
4
<chapterxml:id="java-data-types">
5
5
<title>Java Data Types</title>
6
6
7
-
<introduction>
8
-
</introduction>
9
-
10
7
<sectionxml:id="numeric">
11
8
<title>Numeric</title>
12
9
13
-
<introduction>
14
10
<p>
15
11
One of the great things about Python is that all of the basic data types are objects.
16
12
Integers are objects, floating point numbers are objects, lists are objects, everything.
@@ -124,7 +120,6 @@ public class TempConv {
124
120
</li>
125
121
</ul>
126
122
</p>
127
-
</introduction>
128
123
129
124
130
125
<subsection>
@@ -472,6 +467,11 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
472
467
Here is the Java code needed to write the exact same program:
473
468
</p>
474
469
470
+
<note>
471
+
<p>
472
+
The Java code below contains the first mention of the <idx><c>null</c></idx> <c>null</c> literal. Similar to Python’s <c>None</c> object, <c>null</c> in Java is used to indicate that a variable does not currently reference any object in memory. This is often used for variables that have not yet been initialized or for methods that return no object.
<datafilelabel="datafile-alice1"filename="alice30.txt"editable="no"cols="30"rows="15"><pre> Down, down, down. Would the fall NEVER come to an end! 'I wonder how many miles I've fallen by this time?' she said aloud. 'I must be getting somewhere near the centre of the earth. Let me see: that would be four thousand miles down, I think--' (for, you see, Alice had learnt several things of this sort in her lessons in the schoolroom, and though this was not a VERY good opportunity for showing off her knowledge, as there was no one to listen to her, still it was good practice to say it over) '--yes, that's about the right distance--but then I wonder what Latitude or Longitude I've got to?' (Alice had no idea what Latitude was, or Longitude either, but thought they were nice grand words to say.) </pre></datafile>
661
+
<p>
662
+
This program reads the file <c>alice30.txt</c> (which follows), and it then splits it into a list of words. Next it creates a dictionary called <c>count</c> which maps each word to the number of times that word occurs in the text. Finally, it prints out the words in alphabetical order along with their frequency.
0 commit comments