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 758e1fa
Browse filesBrowse the repository at this point in the historyBrowse files
<title>Why Learn another programming Language?</title>
9
9
10
-
<p>
11
-
Python is a nice language for beginning programming for several reasons.
12
-
First the syntax is sparse, and clear.
13
-
Second, the underlying model of how objects and variables work is very consistent.
14
-
Third, you can write powerful and interesting programs without a lot of work.
15
-
However, Python is representative of one kind of language, called a dynamic language.
16
-
You might think of Python as being fairly informal.
17
-
There are other languages, like Java and C++ that are more formal.
18
-
</p>
10
+
<p>
11
+
Python is a nice language for beginning programming for several reasons.
12
+
First the syntax is sparse, and clear.
13
+
Second, the underlying model of how objects and variables work is very consistent.
14
+
Third, you can write powerful and interesting programs without a lot of work.
15
+
However, Python is representative of one kind of language, called a dynamic language.
16
+
You might think of Python as being fairly informal.
17
+
There are other languages, like Java and C++ that are more formal.
18
+
</p>
19
19
20
-
<p>
21
-
These languages have some advantages of their own.
22
-
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>.
23
-
Second is their maintainability.
24
-
A lot of what makes Python easy to use is that you must remember certain things.
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.
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.
27
-
</p>
20
+
<p>
21
+
These languages have some advantages of their own.
22
+
First, is speed: Java and C++ code will generally give better performance than Python code. (See <xrefref="note-python-performance"/>.)
23
+
Second is their maintainability.
24
+
A lot of what makes Python easy to use is that you must remember certain things.
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.
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.
27
+
</p>
28
28
29
29
<p>
30
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.
@@ -33,16 +33,27 @@
33
33
Languages in this category include Rust, C++, C#, and Ada.
34
34
</p>
35
35
36
+
<p>
37
+
Programming languages will always change.
38
+
As the field of computer science advances there will be new programming languages and you will need to learn them.
39
+
It is important to learn several programming languages so that you know what to expect.
40
+
There are certain features that most programming languages have in common; variables, loops, conditionals, functions.
41
+
And there are some features that are unique.
42
+
If you know what is common in languages that is a good place to start.
43
+
</p>
44
+
<notexml:id="note-python-performance">
45
+
<title>A Note about Python Performance</title>
46
+
47
+
36
48
<p>
37
-
Programming languages will always change.
38
-
As the field of computer science advances there will be new programming languages and you will need to learn them.
39
-
It is important to learn several programming languages so that you know what to expect.
40
-
There are certain features that most programming languages have in common; variables, loops, conditionals, functions.
41
-
And there are some features that are unique.
42
-
If you know what is common in languages that is a good place to start.
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.
43
53
</p>
54
+
</note>
44
55
45
-
</section>
56
+
</section>
46
57
47
58
<sectionxml:id="why-learn-java-why-not-c-or-c">
48
59
<title>Why Learn Java? Why not C or C++?</title>
@@ -83,12 +94,7 @@
83
94
But for these and other reasons, we’ve decided to use Java for this course.
84
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.
89
-
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.).
90
-
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
+16-1Lines changed: 16 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -653,7 +653,22 @@ main()
653
653
</code> <tests> </tests>
654
654
</program>
655
655
656
-
<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>
656
+
<p>
657
+
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.
0 commit comments