Contents [0/11] |
Main [1/11] |
Import [2/11] |
Fully qualified class names [3/11] |
Variable declarations [4/11] |
Types [5/11] |
Syntax [6/11] |
Function declarations [7/11] |
Lists/Arrays [8/11] |
More about Arrays [9/11] |
Formatting Output [10/11] |
Extras: Scope [11/11] |
(Click here for one slide per page)
Main [1/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
Every java program requires a main
function. It must
be declared exactly as on the third line above.
Every java function resides in a class, in this case Hello
.
Every java class resides in a package, in this case algs11
.
Java includes visibility annotations, such as public
and private
. In python, everything is
public
.
Java functions are written using the keyword
static
. (More later.)
Java functions must include types for arguments and results, in
this case String[]
and void
.
Both java and python code is compiled before it is run.
The python interpreter performs both these steps, interactively.
For java, we will be using IntelliJ IDEA, which continuously compiles your code.
If the code successfully compiles (no red marks), then
you can run it by pressing the play
button.
Import [2/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
System.out
and System.in
refer to objects.
I don't want to talk about objects yet, so we will use the version
about, which replaces System.out
with StdOut
.
StdOut
is a class in the stdlib
package.
System
is a class in the java.lang
package.
All of the classes in java.lang
are imported
implicitly into every java program. All other classes must be
imported explicitly.
The statement import stdlib.*
makes the classes
declared in stdlib
visible in our program.
Instead of importing every class from a package, you can also import a single class.
import stdlib.*
with import stdlib.StdOut
.
Unnecessary imports will generate a warning.
import stdlib.StdIn
.
If your code has a compiler error, it will not run.
If your code has a warning, you can run it. But you should fix the warning.
Fully qualified class names [3/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
As an alternative to using import
, you can also use a
class's fully qualified name, which includes the package explicitly.
Fully qualified names make code rather verbose, so usually people
prefer to use import
.
Variable declarations [4/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
In python, values are typed, but variables are not.
In java, both values and variables are typed. Variable types must be explicitly declared.
The declaration and initialization can be combined into a single statement.
01 |
String name = "Bob"; StdOut.println ("Hello " + name); |
In both languages, +
is used to represent string concatenation.
Here's another version which does not use concatenation.
Python | Java | ||||
---|---|---|---|---|---|
|
|
Types [5/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
Python allows a single variable to be used at multiple types.
By typing variables, java catches more errors before runtime.
Python | Java | ||||
---|---|---|---|---|---|
|
|
Java does more implicit conversions than python.
Python | Java | ||||
---|---|---|---|---|---|
|
|
You can safely ignore the rest of this slide, which uses some more advanced java features that you don't need just yet.
The special class java.lang.Integer
is used to create
objects holding a single integer (more later).
All java objects can be referenced at type Object
.
Using these properties it is possible to mimic the original python code above, but java requires explicit casts in order to apply the concatenation and subtraction operators.
Python | Java | ||||
---|---|---|---|---|---|
|
|
Java will do implicit conversions in some places.
The difference between the two casts occurs because every java
object can be converted to a String, using
java.util.Objects.toString
. In some contexts, this
will be done implicitly.
Syntax [6/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
Java uses semicolons and curly-braces, where python uses newlines, colons and indentation.
When formatting java, the conventions for indentation and newlines mimic those of python. But in java, these are just conventions, not requirements.
If statements (aka conditionals)
Python | Java | ||||
---|---|---|---|---|---|
|
|
Comment: Python's **
operator returns an
int
when both arguments are int
s. (If
either argument is a float
, it returns a
float
.)
Java's Math.pow
, instead, always returns a
double
. So, to recover an int
, the Java
code requires a cast.
While statements (aka loops)
Python | Java | ||||
---|---|---|---|---|---|
|
|
For loops
Python | Java | ||||
---|---|---|---|---|---|
|
|
Function declarations [7/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
Java requires declaration of return type and parameter types.
Because of types, java compiler can catch more errors, as below. What's wrong?
Python | Java | ||||
---|---|---|---|---|---|
|
|
Lists/Arrays [8/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
Python | Java | ||||
---|---|---|---|---|---|
|
|
Python | Java | ||||
---|---|---|---|---|---|
|
|
More about Arrays [9/11] |
java.util.Arrays includes a useful function for printing arrays:
01 |
package algs11; import stdlib.*; import java.util.Arrays; public class Hello { public static void main (String[] args) { double[] lst = { 11, 21, 31 }; StdOut.println (lst); StdOut.println (Arrays.toString(lst)); } } |
The size of a Java array is fixed when it is created.
Python | Java | ||||
---|---|---|---|---|---|
|
|
Java arrays are similar to numpy arrays, in that their length is fixed when they are created.
import numpy as np a1 = np.array([11, 21, 31]) # fixed length a2 = np.resize(a1, 4) # returns a new array of the specified size, with copies of old elements a2[3] = 41 print (a2) # [11, 21, 31, 41]
In addition to numpy arrays, python has another type of arrays, which have variable length like python lists. These are only available for base types, like int
and double
.
import array as arr a3 = arr.array('i', [11, 21, 31]) # variable length array of ints a3.append (41) print (a3) # array('i', [11, 21, 31, 41]) a4 = arr.array('d', [11, 21, 31]) # variable length array of doubles a4.append (41) print (a4) # array('d', [11.0, 21.0, 31.0, 41.0])
Formatting Output [10/11] |
java.util.Formatter describes formatting conventions.
It's not necessary to use the Formatter
class directly.
The Formatter.format()
method is used by
StdOut.format()
, String.format()
, and similar methods.
01 |
package algs11; import stdlib.*; import java.util.Arrays; public class Hello { public static void main (String[] args) { double d = Math.PI; int i = Integer.MAX_VALUE; double[] lst = { 11, 21, 31 }; StdOut.format ("d=%f, i=%d\nlst=%s%n", d, i, Arrays.toString(lst)); } } |
StdOut.format
takes a variable number of arguments.
%
.
%f
is used for double
s (and float
s)
%d
is used for int
s (and long
s)
%s
is used for String
s
\n
and %n
represent the newline character
Extras: Scope [11/11] |
Python | Java | ||||
---|---|---|---|---|---|
|
|
Java compiler removes names for variables. Only the values are
stored at runtime. The variable names are replaced with numbers
(offsets in memory). This is one characteristic of static
languages.
Python keeps names for variables at runtime. It stores a
map (aka, a dictionary) from variable names to values. This
is characteristic of dynamic
languages.
Java's approach is more efficient. Python's is more flexible.
Scripting languages
, such as perl and javascript, use the
python approach. Most other languages, including C, C++, C#,
objective-C, swift and FORTRAN, use the java approach.