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 07395e1
Browse filesBrowse the repository at this point in the historyBrowse files
<title>Why Learn another programming Language?</title>
9
+
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,7 +19,7 @@
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.
@@ -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_4_javadatatypes.ptx
+21-1Lines changed: 21 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -472,6 +472,11 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
472
472
Here is the Java code needed to write the exact same program:
473
473
</p>
474
474
475
+
<note>
476
+
<p>
477
+
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>
666
+
<p>
667
+
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.
<title>Forgetting to declare your variables</title>
12
12
13
+
<p>
14
+
The 'cannot find symbol' error for the variable <c>count</c> on line 7 indicates that <c>count</c> was used before it was declared within the <c>Histo</c> class. In Java, all variables must be explicitly declared with a data type (e.g., <c>int</c>, <c>String</c>, <c>ArrayList<Integer></c>) before they can be assigned a value or referenced in any way. The arrow in the error message points to where the undeclared variable <c>count</c> was first encountered. To resolve this, <c>count</c> needs to be declared with its appropriate type (e.g., <c>ArrayList<Integer> count;</c>) before any attempt to initialize or use it.
15
+
</p>
13
16
<pre>
14
-
Histo.java:21: cannot find symbol
15
-
symbol : variable count
16
-
location: class Histo
17
+
Main.java:7: error: cannot find symbol
17
18
count = new ArrayList<Integer>(10);
18
19
^
20
+
symbol: variable count
21
+
location: class Main
19
22
</pre>
20
23
21
24
</section>
22
25
23
26
<sectionxml:id="not-importing-a-class">
24
27
<title>Not importing a class</title>
25
28
29
+
<p>
30
+
You may notice that this error message looks similar to the previous one, however, it has an entirely different cause. In Java, classes like <c>Scanner</c> that are part of external packages (like <c>java.util</c>) must be explicitly imported into your source file. Java does not automatically recognize these classes. To resolve this error, you need to add an import statement for the <c>Scanner</c> class at the beginning of your <c>Histo.java</c> file, typically import <c>java.util.Scanner;</c>.
31
+
</p>
32
+
26
33
<pre>
27
-
Histo.java:9: cannot find symbol
28
-
symbol : class Scanner
29
-
location: class Histo
34
+
Histo.java:3: error: cannot find symbol
30
35
Scanner data = null;
31
36
^
37
+
symbol: class Scanner
38
+
location: class Histo
32
39
</pre>
33
40
34
41
</section>
@@ -37,41 +44,54 @@
37
44
<title>Forgetting to use the new keyword to create an object</title>
38
45
39
46
<p>
40
-
Here’s an example of the error message that occurs when you forget to use the new keyword.
41
-
Notice that the message is pretty unhelpful.
42
-
Java <em>thinks</em> you are trying to call the Method Scanner, but there are two problems.
43
-
First Scanner is not really a method it is a constructor.:
47
+
This error message occurs when you forget to use the <c>new</c> keyword to instantiate an object.
48
+
Specifically, on line 8 of <c>Histo.java</c>, <c>data = Scanner(new File("test.dat"));</c> leads to a 'cannot find symbol' error.
49
+
While the message states 'symbol: method Scanner(File)', this can be misleading.
50
+
Java incorrectly interprets <c>Scanner()</c> as an attempt to call a static method named <c>Scanner</c> within the <c>Histo</c> class
51
+
(or an inherited one). However, <c>Scanner</c> is a constructor of the <c>Scanner</c> class, not a standalone method,
52
+
and therefore requires the new keyword (e.g., <c>data = new Scanner(new File("test.dat"));</c>) to create a new <c>Scanner</c> object.
53
+
44
54
</p>
45
55
46
56
<pre>
47
-
Histo.java:14: cannot find symbol
48
-
symbol : method Scanner(java.io.File)
49
-
location: class Histo
57
+
Histo.java:8: error: cannot find symbol
50
58
data = Scanner(new File("test.dat"));
51
-
^
59
+
^
60
+
symbol: method Scanner(File)
61
+
location: class Histo
52
62
</pre>
53
63
54
64
</section>
55
65
56
66
<sectionxml:id="forgetting-a-semicolon">
57
67
<title>Forgetting a Semicolon</title>
58
-
68
+
<p>
69
+
The error "';' expected" on line 7 of <c>Histo.java</c> means that a semicolon is missing at the end of the statement <c>Scanner data = null</c>. In Java, every statement must be terminated with a semicolon (<c>;</c>) to indicate its completion. The arrow points to <c>null</c> because that's where the compiler expected to find the semicolon.
<title>Forgetting to declare the kind of object in a container</title>
70
-
81
+
<p>
82
+
This is a compiler warning, not an error, indicating a potential type safety issue. It occurs because you are calling the <c>add()</c> method on <c>rawList</c>, which is an <c>ArrayList</c> used as a raw type (i.e., without specifying a generic type like <c><String></c> or <c><Integer></c>).
83
+
</p>
84
+
85
+
<p>
86
+
When <c>ArrayList</c> is used as a raw type, the compiler cannot guarantee the type of elements being added or retrieved, leading to "unchecked" operations. The <c>E extends Object</c> part refers to the generic type parameter <c>E</c> that <c>ArrayList</c> should have, which defaults to <c>Object</c> when the type is not specified, making the <c>add()</c> call unchecked. This can lead to <c>ClassCastExceptions</c> at runtime if incompatible types are later retrieved and cast.
87
+
</p>
71
88
<pre>
72
-
Note: Histo.java uses unchecked or unsafe operations. Note:
73
-
Recompile with -Xlint:unchecked for details.
89
+
UncheckedWarningDemo.java:8: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
Copy file name to clipboardExpand all lines: source/meta_backmatter.ptx
-12Lines changed: 0 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -4,18 +4,6 @@
4
4
<backmatterxml:id="meta_backmatter">
5
5
<title> Appendices </title>
6
6
7
-
<colophon>
8
-
<p>
9
-
I started writing this article in 2006 using Python’s restructured text.
10
-
In 2007 I switched to markdown since everything else I was writing used markdown.
11
-
In particular I switched to the variant of markdown used by the excellent <urlhref="http://johnmacfarlane.net/pandoc/"visual="http://johnmacfarlane.net/pandoc/">pandoc</url> program.
12
-
Pandoc does an excellent job of converting markdown to html, latex, pdf, and other formats.
13
-
The markdown source for this article is available <urlhref="http://www.cs.luther.edu/~bmiller/JavaForPython.md"visual="http://www.cs.luther.edu/~bmiller/JavaForPython.md">here</url>.
14
-
Finally the document became so big that I used <c>pandoc</c> to convert it to latex for preparing the final version.
15
-
I used The <c>htlatex</c> program to generate html for the online version of this document.
<personname>Drs. Brad Miller and Jan Pearce</personname>
10
8
</author>
11
9
<date><today/>
12
10
</date>
@@ -35,23 +33,36 @@
35
33
</p>
36
34
37
35
<p>
38
-
This book does not attempt to replace the many good Java reference books that are available, in fact I use this in my course along with <urlhref="https://horstmann.com/corejava/index.html"visual="https://horstmann.com/corejava/index.html">Cay Horstman’s “Core Java” volumes</url>.
36
+
This book does not attempt to replace the many good Java reference books that are available, in fact Dr. Miller used to use this in his course along with <urlhref="https://horstmann.com/corejava/index.html"visual="https://horstmann.com/corejava/index.html">Cay Horstman’s “Core Java” volumes</url>.
39
37
Please feel free to use this book for yourself, or if it fits a class you are teaching you are welcome to use this as a resource for your own class.
40
38
</p>
41
39
42
40
<p>
43
-
I have published this article using a Creative Commons license to encourage you to use it, change it, and modify it for your own purposes.
44
-
I would appreciate knowing what you think if you do use this book, and I would love to see any modifications or additions you make.
41
+
We have published this article using a Creative Commons license to encourage you to use it, change it, and modify it for your own purposes.
42
+
We would appreciate knowing what you think if you do use this book, and we would love to see any modifications or additions you make.
45
43
</p>
46
44
47
-
<p>
48
-
Brad Miller <urlhref="mailto://bmiller@luther.edu"visual="mailto://bmiller@luther.edu">bmiller@luther.edu</url> January, 2008
49
-
</p>
50
45
51
46
<blockquote>
52
47
<p>
53
48
<imagesource="somerights20.png"width="50%"alt="image"/> This work is licensed under a Creative Commons Attribution 3.0 United States License. See <urlhref="http://creativecommons.org"visual="http://creativecommons.org">http://creativecommons.org</url>
54
49
</p>
55
50
</blockquote>
56
51
</preface>
52
+
53
+
<prefacexml:id="history_of_this_book">
54
+
<title>History of this Book</title>
55
+
<p>
56
+
Brad Miller started writing this article in 2006 using Python’s restructured text.
57
+
In 2007 I switched to markdown since everything else he was writing used markdown.
58
+
In particular he switched to the variant of markdown used by the excellent <urlhref="https://pandoc.org/"visual="https://pandoc.org/">pandoc</url> program.
59
+
Pandoc does an excellent job of converting markdown to html, latex, pdf, and other formats.
60
+
61
+
Finally, the document became so big that Brad used <c>pandoc</c> to convert it to latex for preparing the final version.
62
+
He used The <c>htlatex</c> program to generate html for an online version of this document.
63
+
</p>
64
+
<p>
65
+
Brad Miller founded Runestone Academy in 2011 during a sabbatical from Luther College, and the original versions of this book was written in ReStructuredText.
0 commit comments