Skip to content

Commit cb2ec13

Browse files
authored
fix typos, grammar
1 parent 62ed6a0 commit cb2ec13

1 file changed

Lines changed: 47 additions & 47 deletions

File tree

‎_sources/Java4Python/javadatatypes.rst‎

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Numeric
66

77
One of the great things about Python is that all of the basic data types
88
are objects. Integers are objects, floating point numbers are objects,
9-
lists are objects, everything. In Java that is not the case. In Java
9+
lists are objects, everything. In Java that is not the case. In Java,
1010
some of the most basic data types like integers and floating point
1111
numbers are not objects. The benefit of having these primitive data
1212
types be non-objects is that operations on the primitives are fast. The
@@ -24,12 +24,12 @@ non-object primitives ended up with Objectified versions.
2424
boolean Boolean
2525
================== ========
2626

27-
In older versions of Java it was the programmers responsibility to
27+
In older versions of Java, it was the programmers responsibility to
2828
convert back and forth from a primitive to an object whenever necessary.
29-
This processing of converting a primitive to an object was called
29+
This process of converting a primitive to an object was called
3030
“boxing.” The reverse process is called “unboxing.” In Java 5, the
3131
compiler became smart enough to know when to convert back and forth and
32-
is called “autoboxing.” In this book we will typically use the Object
32+
is called “autoboxing.” In this book, we will typically use the Object
3333
version of all the numeric data types and let the compiler do its thing.
3434

3535
Lets go back in time and look at another of our very early Python
@@ -46,7 +46,7 @@ temperature to Celsius.
4646

4747
main()
4848

49-
Next, lets look at the Java Equivalent.
49+
Next, lets look at the Java equivalent.
5050

5151
.. activecode:: convert1
5252
:language: java
@@ -85,32 +85,32 @@ at them in the following order:
8585
Import
8686
~~~~~~
8787

88-
In Java you can use any class that is available without having to import
89-
the class subject to two very important conditions:
88+
In Java, you can use any class that is available without having to import
89+
the class, subject to two very important conditions:
9090

9191
1. The javac and java must know that the class exists.
9292

9393
2. You must use the full name of the class
9494

95-
You first question might be how do the ``java`` and ``javac`` commands
95+
Your first question might be how do the ``java`` and ``javac`` commands
9696
know that certain classes exist. The answer is the following:
9797

9898
1. Java knows about all the classes that are defined in .java and .class
9999
files in your current working directory.
100100

101-
2. Java knows about all the classes that are shipped with java.
101+
2. Java knows about all the classes that are shipped with Java.
102102

103103
3. Java knows about all the classes that are included in your
104104
``CLASSPATH`` environment variable. Your ``CLASSPATH`` environment
105105
variable can name two kinds of structures.
106106

107-
1. A jar file that contains java classes
107+
1. A jar file that contains Java classes
108108

109-
2. Another unix directory that contains java class files
109+
2. Another unix directory that contains Java class files
110110

111111
You can think of the import statement in Java as working a little bit
112112
like the ``from module import xxx`` statement in Python. However, behind
113-
the scenes the two statements actually do very different things. The
113+
the scenes, the two statements actually do very different things. The
114114
first important difference to understand is that the class naming system
115115
in Java is very hierarchical. The *full* name of the Scanner class is
116116
really ``java.util.Scanner``. You can think of this name as having two
@@ -125,8 +125,8 @@ compiler that we are going to use a shortened version of the class’s
125125
name. In this example we are going to use the class
126126
``java.util.Scanner`` but we can refer to it as just ``Scanner``. We
127127
could use the ``java.util.Scanner`` class without any problem and
128-
without any import statement provided that we always referred to it by
129-
its full name. As an Experiment you may want to try this yourself.
128+
without any import statement, provided that we always referred to it by
129+
its full name. As an experiment, you may want to try this yourself.
130130
Remove the import statement and change the string Scanner to
131131
``java.util.Scanner`` in the rest of the code. The program should still
132132
compile and run.
@@ -151,7 +151,7 @@ reference a Scanner object. This means that if we were to try an
151151
assignment like ``fahr = "xyz"`` the compiler would generate an error
152152
because ``"xyz"`` is a string and ``fahr`` is supposed to be a double.
153153

154-
For Python programmers the following error is likely to be even more
154+
For Python programmers, the following error is likely to be even more
155155
common. Suppose we forgot the declaration for ``cel`` and instead left
156156
line 6 blank. What would happen when we type ``javac TempConv.java`` on
157157
the command line?
@@ -171,7 +171,7 @@ the command line?
171171
2 errors
172172

173173
When you see the first kind of error, where the symbol is on the left
174-
side of the equals sign it usually means that you have not declared the
174+
side of the equals sign, it usually means that you have not declared the
175175
variable. If you have ever tried to use a Python variable that you have
176176
not initialized the second error message will be familiar to you. The
177177
difference here is that we see the message before we ever try to test
@@ -181,33 +181,33 @@ our program. More common error messages are discussed in the section
181181
The general rule in Java is that you must decide what kind of an object
182182
your variable is going to reference and then you must declare that
183183
variable before you use it. There is much more to say about the static
184-
typing of Java but for now this is enough.
184+
typing of Java, but for now this is enough.
185185

186186
Input / Output / Scanner
187187
~~~~~~~~~~~~~~~~~~~~~~~~
188188

189189
In the previous section you saw that we created a ``Scanner`` object. In
190-
Java Scanner objects make getting input from the user, a file, or even
190+
Java, Scanner objects make getting input from the user, a file, or even
191191
over the network relatively easy. In our case we simply want to ask the
192192
user to type in a number at the command line, so in line 9 we construct
193193
a Scanner by calling the constructor and passing it the ``System.in``
194194
object. Notice that this Scanner object is assigned to the name ``in``,
195195
which we declared to be a ``Scanner`` on line 7. ``System.in`` is
196-
similar to ``System.out`` except of course it is used for input. If you
196+
similar to ``System.out`` except, of course, it is used for input. If you
197197
are wondering why we must create a Scanner to read data from
198198
``System.in`` when we can write data directly to ``System.out`` using
199199
``println``, you are not alone. We will talk about the reasons why this
200-
is so later when we talk in depth about Java streams. You will also see
200+
is so later when we talk in-depth about Java streams. You will also see
201201
in other examples that we can create a Scanner by passing the Scanner a
202202
File object. You can think of a scanner as a kind of “adapter” that
203203
makes low level objects easier to use.
204204

205205
On line 11 we use the Scanner object to read in a number. Here again we
206206
see the implications of Java being a strongly typed language. Notice
207-
that we must call the method ``nextDouble``. Because the variable
207+
that we must call the method ``nextDouble`` because the variable
208208
``fahr`` was declared as a double. So, we must have a function that is
209209
guaranteed to return each kind of object we might want to read. In this
210-
case we need to read a Double so we call the function nextDouble. The
210+
case, we need to read a Double so we call the function ``nextDouble``. The
211211
compiler matches up these assignment statments and if you try to assign
212212
the results of a method call to the wrong kind of variable it will be
213213
flagged as an error.
@@ -229,9 +229,9 @@ to find them in the next chapter.
229229
String next() returns the next thing to read as a String
230230
==================== ================ ======================================================
231231

232-
Of course Java is more well known for producing applications that have
232+
Of course, Java is more well-known for producing applications that have
233233
more of a user interface to them than reading and writing from the
234-
command line. Lets look at a version of our temperature control
234+
command line. Lets look at a version of our temperature conversion
235235
application that uses dialog boxes for input and output.
236236

237237
.. activecode:: swing
@@ -268,14 +268,14 @@ The next dialog box is ``JOptionPane.showMessageDialog``. Notice that
268268
the first parameter is ``null`` In Java ``null`` serves the same purpose
269269
as ``None`` in Python. The first parameter is null because we do not
270270
have a ‘main window’ for this little application. When we look at
271-
creating full blown java programs with user interfaces, we will learn
271+
creating full-blown Java programs with user interfaces, we will learn
272272
more about this parameter.
273273

274274
The second parameter is ``"The temperature in C is, " + cel``. Now you
275275
may be thinking to yourself that this must surely be a violation of the
276-
strong typing I have been describing to you. After all you should not be
276+
strong typing I have been describing to you. After all, you should not be
277277
able to add together a string and a Double right? You are correct,
278-
however, all java objects have a method called ``tostring``. The
278+
however, all Java objects have a method called ``tostring``. The
279279
``tostring`` method acts much like the Python method ``__str__()`` and
280280
is called automatically by the compiler whenever it makes sense to
281281
convert a Java object to a string.
@@ -290,7 +290,7 @@ That is not to say that you can’t index into a Java string, you can. You
290290
can also pull out a substring just as you can with slicing. The
291291
difference is that Java uses method calls where Python uses Operators.
292292

293-
In fact this is the first example of another big difference between Java
293+
In fact, this is the first example of another big difference between Java
294294
and Python. Java does not support any operator overloading. Table 3 maps
295295
common Python string operations to their Java counterparts. For the
296296
examples shown in the table we will use a string variable called “str”
@@ -353,16 +353,16 @@ We will get output that looks like this:
353353

354354
Lets review what is happening in this little program. In the first line
355355
we create a list and initialize the first 10 positions in the list to be
356-
0. Next we open the data file called ‘test.dat’ Third, we have a loop
356+
0. Next we open the data file called ‘test.dat’. Third, we have a loop
357357
that reads each line of the file. As we read each line we convert it to
358358
an integer and increment the counter at the position in the list
359359
indicated by the number on the line we just read. Finally we iterate
360-
over each element in the list printing out both the position in the list
360+
over each element in the list, printing out both the position in the list
361361
and the total value stored in that position.
362362

363363
To write the Java version of this program we will have to introduce
364364
several new Java concepts. First, you will see the Java equivalent of a
365-
list, called an ``ArrayLlist.`` Next you will see three different kinds
365+
list, called an ``ArrayList.`` Next, you will see three different kinds
366366
of loops used in Java. Two of the loops we will use are going to be very
367367
familiar, the third one is different from what you are used to in Python
368368
but is easy when you understand the syntax:
@@ -407,7 +407,7 @@ Here is the Java code needed to write the exact same program:
407407
}
408408

409409
count = new ArrayList<Integer>(10);
410-
for (Integer i =0; i<10;i++) {
410+
for (Integer i = 0; i < 10; i++) {
411411
count.add(i,0);
412412
}
413413

@@ -439,7 +439,7 @@ Here is the Java code needed to write the exact same program:
439439
Before going any further, I suggest you try to compile the above program
440440
and run it on some test data that you create.
441441

442-
Now, lets look at what is happening in the Java source. As usual we
442+
Now, let's look at what is happening in the Java source. As usual, we
443443
declare the variables we are going to use at the beginning of the
444444
method. In this example we are declaring a Scanner variable called data,
445445
an integer called idx and an ``ArrayList`` called count. However, there
@@ -450,7 +450,7 @@ the ``ArrayList`` will contain Integers. The syntax we use to declare
450450
what kind of object the list will contain is the ``<Type>``
451451
syntax.
452452

453-
Technically, you don’t *have* to declare what is going to be on an array
453+
Technically, you don’t *have* to declare what is going to be in an array
454454
list. The compiler will allow you to leave the ``<``*Type*``>`` off the
455455
declaration. If you don’t tell Java what kind of object is going to be
456456
on the list Java will give you a warning message like this:
@@ -467,12 +467,12 @@ list like this! So, if you forget you will surely see more errors later
467467
in your code. (Try it and see what you get)
468468

469469
Lines 13—20 are required to open the file. Why so many lines to open a
470-
file in Java? The additional code mainly comes form the fact that Java
470+
file in Java? The additional code mainly comes from the fact that Java
471471
forces you to reckon with the possibility that the file you want to open
472472
is not going to be there. If you attempt to open a file that is not
473473
there you will get an error. A try/catch construct allows us to try
474474
things that are risky, and gracefully recover from an error if one
475-
occurs. The following example shows the general structure of a try catch
475+
occurs. The following example shows the general structure of a try/catch
476476
block.
477477

478478
::
@@ -485,30 +485,30 @@ block.
485485
We will catch that exception here!
486486
}
487487

488-
Notice that in line 16 we are catching an ``IOException``. In fact we
488+
Notice that in line 16 we are catching an ``IOException``. In fact, we
489489
will see later that we can have multiple catch blocks to catch different
490490
types of exceptions. If we want to be lazy and catch any old exception
491491
we can catch an ``Exception`` which is the parent of all exceptions.
492492

493-
On line 22 we create our array list and give it an initial size of 10.
494-
Strictly speaking it is not necessary to give the ``ArrayList`` any
495-
size. It will grow or shrink dynamically as needed just like a list in
493+
On line 22 we create our ``ArrayList`` and give it an initial size of 10.
494+
Strictly speaking, it is not necessary to give the ``ArrayList`` any
495+
size. It will grow or shrink dynamically as needed, just like a list in
496496
Python. On line 23 we start the first of three loops. The for loop on
497497
lines 23–25 serves the same purpose as the Python statement
498498
``count = [0]*10``, that is it initializes the first 10 positions in the
499499
``ArrayList`` to hold the value 0.
500500

501501
The syntax of this for loop probably looks very strange to you, but in
502502
fact it is not too different from what happens in Python using range. In
503-
fact ``for(Integer i = 0; i < 10; i++)`` is exactly equivalent to the
503+
fact ``for (Integer i = 0; i < 10; i++)`` is exactly equivalent to the
504504
Python ``for i in range(10)`` The first statement inside the parenthesis
505505
declares and initializes a loop variable i. The second statement is a
506506
Boolean expression that is our exit condition. In other words we will
507507
keep looping as long as this expression evaluates to true. The third
508508
clause is used to increment the value of the loop variable at the end of
509509
iteration through the loop. In fact ``i++`` is Java shorthand for
510-
``i = i +`` Java also supports the shorthand ``i--`` to decrement the
511-
value of i. Like Python you can also write ``i += 2`` as shorthand for
510+
``i = i + 1`` Java also supports the shorthand ``i--`` to decrement the
511+
value of i. Like Python, you can also write ``i += 2`` as shorthand for
512512
``i = i + 2`` Try to rewrite the following Python for loops as Java for
513513
loops:
514514

@@ -523,7 +523,7 @@ loops:
523523

524524
The next loop (lines 27–30) shows a typical Java pattern for reading
525525
data from a file. Java while loops and Python while loops are identical
526-
in their logic. In this case we will continue to process the body of the
526+
in their logic. In this case, we will continue to process the body of the
527527
loop as long as ``data.hasNextInt()`` returns true.
528528

529529
Line 29 illustrates another important difference between Python and
@@ -534,7 +534,7 @@ logical operations is done using methods. So, to set the value of an
534534
``ArrayList`` element we use the ``set`` method. The first parameter of
535535
``set`` indicates the index or position in the ``ArrayList`` we are
536536
going to change. The next parameter is the value we want to set. Notice
537-
that once again we cannot use the indexing square bracket operator to
537+
that, once again, we cannot use the indexing square bracket operator to
538538
retrieve a value from the list, but we must use the ``get`` method.
539539

540540
The last loop in this example is similar to the Python for loop where
@@ -551,7 +551,7 @@ that you had an error on this line. Why?
551551
Arrays
552552
------
553553

554-
As I said at the outset of this Section we are going to use Java
554+
As I said at the outset of this section, we are going to use Java
555555
``ArrayLists`` because they are easier to use and more closely match the
556556
way that Python lists behave. However, if you look at Java code on the
557557
internet or even in your Core Java books you are going to see examples
@@ -608,7 +608,7 @@ Dictionary
608608
----------
609609

610610
Just as Python provides the dictionary when we want to have easy access
611-
to key, value pairs, Java also provides us a similar mechanism. Rather
611+
to key-value pairs, Java also provides us a similar mechanism. Rather
612612
than the dictionary terminology, Java calls these objects Maps. Java
613613
provides two different implementations of a map, one is called the
614614
``TreeMap`` and the other is called a ``HashMap``. As you might guess

0 commit comments

Comments
 (0)