From Python to Java
CISC124
1
double is the usual choice for routine calculations
involving floating-point values
double hi = Double.MAX_VALUE; // a very big value
double lo = -hi;
// the smallest positive value
double tiny = Double.MIN_VALUE;
2
A double literal is any number written using a decimal
and not ending in F or f
it is a compile-time error if the numeric value is
outside the range that an double can represent
double x = 1.0;
double y = 2.;
// underscores allowed between digits
// (not allowed at beginning or end)
double z = 1_000_000.99;
// scientific notation
double alsoZ = 1.00000099e6;
3
An arithmetic operation involving two double values
always produces a double value
double x = 1.0;
double y = 3.0;
double z = 13.0;
double result = x + y; // 4.0
result = x – y; // -2.0
result = x * y; // 3.0
result = x / y; // 0.33...
result = z / y; // 4.33...
result = (x + y + z) / 3; // 5.66...
4
Unlike the integer types, floating-point computations do
not wrap when values exceed the range of double
values saturate at Double.POSITIVE_INFINITY and
Double.NEGATIVE_INFINITY
double x = 1e200 * 1e200; // Double.POSITIVE_INFINITY
double y = -1e200 * 1e200; // Double.NEGATIVE_INFINITY
5
Python has user-defined functions
procedures that can be called to perform a specific
task, e.g., find the max of two integer values
def max2(a, b):
twice_max = a + b + abs(a – b)
return twice_max / 2
# in another module after importing the module
# containing max2
x = max2(5, -3)
6
Java has no functions
the closest analog is a public static method
a method is similar to a function but it must be
defined inside of a class (or interface)
public class Lecture2 {
public static int max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;
}
}
// continued on next slide
7
To call the max2 method, a method in another class
must import the Lecture2 class and then call the max2
method using the syntax
Lecture2.max2(arg1, arg2)
import Lecture2;
public class TestLecture2 {
public static void main(String[] args) {
int x = Lecture2.max2(5, -3);
}
}
8
In Python, indentation is fundamental to the language
required to denote the body of a function, if
statement, for loop, etc.
def max2(a, b):
twice_max = a + b + abs(a – b)
return twice_max / 2
9
In Java, indentation is used only for readability
the compiler does not care if your code is indented
but use the examples in the slides and course notes for
guidance on good Java programming style
// this compiles cleanly, but don't format your code
// like this
public class Lecture2 { public static int
max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;}
}
10
Java uses braces to delimit the bodies of classes,
methods, if statements, and loops
the braces are required
public class Lecture2 {
public static int max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;
} // end of method body
} // end of class body
11
For the time being, all of our classes will be public
the public access modifier on a top-level class means
that the class is visible to all other classes
public class Lecture2 {
public static int max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;
}
}
12
For the time being, all of our methods will be public and
static
the public access modifier on a method means that
the method is callable from within any class
public class Lecture2 {
public static int max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;
}
}
13
For the time being, all of our methods will be public and
static
the static modifier on a method means that the
method is associated with its class
which is why you use the class name in addition to the
method name when calling a static method
public class Lecture2 {
public static int max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;
}
}
14
The parameters of a Java method must have types
e.g., max2 has two parameters of type int
public class Lecture2 {
public static int max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;
}
}
15
A Java method is allowed to return zero or one value
if a method returns a value, then it must declare the
type of the value that it returns
the type should appear immediately before the method
name
e.g., max2 returns an int value
public class Lecture2 {
public static int max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;
}
}
16
A Java method is allowed to return zero or one value
if a method does not return a value, then it must
declare that it returns the type void
e.g., a main always returns void in Java
import Lecture2;
public class TestLecture2 {
public static void main(String[] args) {
int x = Lecture2.max2(5, -3);
}
}
17
There must be a return statement in a method that
returns a value
the method stops running immediately after the
return statement finishes running
public class Lecture2 {
public static int max2(int a, int b) {
int twiceMax = a + b + Math.abs(a – b);
return twiceMax / 2;
}
type must be compatible with
} the declared return type of the method
18
A void method has no return statement
import Lecture2;
public class TestLecture2 {
public static void main(String[] args) {
int x = Lecture2.max2(5, -3);
// no return statement
}
}
19
A void method has no return statement
but an empty return statement is legal
import Lecture2;
public class TestLecture2 {
public static void main(String[] args) {
int x = Lecture2.max2(5, -3);
return;
}
}
20
A Python if statement changes program flow based on
one or more conditions:
def max3(a, b, c):
if a >= b and a >= c:
result = a
elif b >= a and b >= c:
result = b
else:
result = c
return result
21
The corresponding method in Java:
public static double max3(double a, double b, double c) {
double result;
if (a >= b && a >= c) {
result = a;
}
else if (b >= a && b >= c) {
result = b;
}
else {
result = c;
}
return result;
}
22
We can remove the else clause by assuming the
maximum value is c:
def max3(a, b, c):
result = c
if a >= b and a >= c:
result = a
elif b >= a and b >= c:
result = b
return result
23
The corresponding method in Java
public static double max3(double a, double b, double c) {
double result = c;
if (a >= b && a >= c) {
result = a;
}
else if (b >= a && b >= c) {
result = b;
}
return result;
}
24
The conditions must appear inside parentheses
public static double max3(double a, double b, double c) {
double result = c;
if (a >= b && a >= c) {
result = a;
}
else if (b >= a && b >= c) {
result = b;
}
return result;
}
25
The scope of a variable is the region of code where a
declared variable name is usable
in Java, the scope starts where the variable was declared and
goes to the end of the block that the variable was declared in
parameters: scope = method body
public static double max3(double a, double b, double c) {
double result = c;
if (a >= b && a >= c) {
result = a;
}
else if (b >= a && b >= c) {
result = b;
}
return result;
}
26
The scope of a variable is the region of code where a
declared variable name is usable
in Java, the scope starts where the variable was declared and
goes to the end of the block that the variable was declared in
public static double max3(double a, double b, double c) {
double result = c; result: scope = from here to end of method body
if (a >= b && a >= c) {
result = a;
}
else if (b >= a && b >= c) {
result = b;
}
return result;
}
27
The scope of a variable is the region of code where a
declared variable name is usable
in Java, the scope starts where the variable was declared and
goes to the end of the block that the variable was declared in
public static double max3(double a, double b, double c) {
if (a >= b && a >= c) {
double result = a; result: scope = just this line!
}
else if (b >= a && b >= c) {
double result = b; result: scope = just this line!
}
return result; compile-time error:
no declared variable named result
}
28
A method may have more than one return statement
but there cannot be reachable code after a return statement
some consider multiple return statements to be bad form
public static double max3(double a, double b, double c) {
if (a >= b && a >= c) {
return a;
// no code allowed here
}
else if (b >= a && b >= c) {
return b;
// no code allowed here
}
return c;
// no code allowed here
}
29
A method that returns a value must always return a value
most compilers do not attempt logical deduction
public static double max3(double a, double b, double c) {
if (a >= b && a >= c) {
return a; in this example, the compiler
}
sees three if statements and
assumes that it is possible that
else if (b >= a && b >= c) {
none of the conditions are true
return b;
which leads to program flow
} reaching the end of the method
else if (c >= a && c >= b) { without executing a return
return c; statement
}
// compile-time error: compiler does not recognize that the method
// always returns a value
}
30
In Java, && is the logical AND operator
public static double max3(double a, double b, double c) {
double result = c;
if (a >= b && a >= c) {
result = a;
}
else if (b >= a && b >= c) {
result = b;
}
return result;
}
31
In Java, || is the logical OR operator
&& has higher precedence than ||
public static boolean isLeapYear (int year) {
if (year % 400 == 0 || year % 4 == 0 &&
year % 100 != 0) {
return true;
}
return false;
}
32
Use parentheses if you want to be explicit about
precedence
public static boolean isLeapYear (int year) {
if (year % 400 == 0 || (year % 4 == 0 &&
year % 100 != 0)) {
return true;
}
return false;
}
33
Start training yourself to avoid writing an if statement
when one is not required:
public static boolean isLeapYear (int year) {
return year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0);
}
34
In Java, ! is the logical NOT operator
assume isLeapYear is defined in the class Lecture3
public static int daysInFeb(int year) {
if (!Lecture3.isLeapYear(year)) {
return 28;
}
return 29;
}
35
Python allows the programmer to chain comparison
operators:
def print_letter(grade):
if grade >= 80:
result = 'A'
elif 70 <= grade < 80:
result = 'B'
# and so on
print(result)
36
Java does not allow chained comparisons. The
programmer must use && and two separate comparisons.
public static void printGrade(int grade) {
String result = "";
if (grade >= 80) {
result = "A";
}
else if (grade >= 70 && grade < 80) {
result = "B";
}
// and so on
System.out.println(result);
}
37