Loading subject…
true and false, Python capitalises them as True and False.double or float in Java and as float in Python.char grade = 'A';.| Data type | Java declaration | Python assignment | Value stored |
|---|---|---|---|
| Boolean | boolean passed = false; | passed = False | false |
| Integer | int count = 0; | count = 0 | 0 |
| Decimal | double price = 7.99; | price = 7.99 | 7.99 |
| Char | char grade = 'A'; | grade = 'A' | A (a one-character string in Python) |
| String | String name = "Ada"; | name = "Ada" | Ada |
float price = 7.99; in Java will not compile, because 7.99 is a double literal, so use double or write 7.99f.true or false in lower case in Python raises a NameError.char c = "a"; in Java fails, because double quotes make a String, not a char."5" + 1 concatenates instead of adding.global total first to change the global instead of shadowing it.static class variable, which any method of the class can assign to directly.average is calculated.| Pass | count at test | count <= 4 | total | count | Output |
|---|---|---|---|---|---|
| before loop | 0 | 1 | |||
| 1 | 1 | true | 1 | 2 | |
| 2 | 2 | true | 3 | 3 | |
| 3 | 3 | true | 6 | 4 | |
| 4 | 4 | true | 10 | 5 | |
| exit | 5 | false | 10 | 5 | 2.5 in Python, 2.0 in Java |
/ operator always produces a decimal, so 10 / 4 gives 2.5./ operator between two integers produces an integer, so 10 / 4 gives 2 and the remainder is discarded before it is ever stored in the double.(double) total / 4 gives 2.5 and the trace then matches Python.double does not undo integer division, because the division has already happened by the time the value is assigned.// operator is the one that discards the remainder, so it is the Python equivalent of Java's integer division.0 rather than to the first value gives the wrong answer on data that is all negative.global keyword before it can change a global variable?double average = total / 4; give 2.0 in Java when total holds 10?int score = 0; // type fixed at declarationscore = 12; // allowed, still an int// score = "twelve"; // will not compilescore = 0 # type inferred as intscore = 12 # allowed, still an intscore = "twelve" # allowed, score is now a strtotal = 0 # global
def add_rectangle(width, height): global total # without this line, total below is a new local area = width * height # local total = total + area return area
print(add_rectangle(4, 3)) # 12print(total) # 12# print(area) # NameError: area does not exist herepublic class Rectangles { static int total = 0; // global (class variable)
static int addRectangle(int width, int height) { int area = width * height; // local total = total + area; return area; }
public static void main(String[] args) { System.out.println(addRectangle(4, 3)); // 12 System.out.println(total); // 12 // System.out.println(area); // will not compile }}total = 0count = 1
while count <= 4: total = total + count count = count + 1
average = total / 4print(average)int total = 0;int count = 1;
while (count <= 4) { total = total + count; count = count + 1;}
double average = total / 4;System.out.println(average);