Section A.1 Java Cheat Sheet
Purpose of this Cheat Sheet.
The following is intended to be useful in better understanding Java functions coming from a Python background.
| Python Function | Java Equivalent | Description |
|---|---|---|
print() |
System.out.println() |
Prints output to the console. |
len() |
array.length or list.size()
|
Returns the length of an array or size of a list. |
range() |
for (int i = 0; i < n; i++) |
Used in loops to iterate a specific number of times. |
str() |
String.valueOf() |
Converts an object to a string. |
int() |
Integer.parseInt() |
Converts a string to an integer. |
float() |
Float.parseFloat() |
Converts a string to a float. |
list.append() |
ArrayList.add() |
Adds an element to the end of a list. |
list.pop() |
ArrayList.remove(index) |
Removes and assign the return value to use it. |
list.sort() |
Collections.sort(list) |
Sorts a list in ascending order. |
list.reverse() |
Collections.reverse(list) |
Reverses the order of elements in a list. |
dict.get() |
Map.get(key) |
Retrieves the value associated with a key in a map. |
dict.keys() |
Map.keySet() |
Returns a set of keys in a map. |
dict.values() |
Map.values() |
Returns a collection of values in a map. |
dict.items() |
Map.entrySet() |
Returns a set of key-value pairs in a map. |
input() |
Scanner.nextLine() |
Reads a line of input from the console. |
open() |
FileReader, BufferedReader
|
Used to read from files. |
enumerate() |
for (int i = 0; i < list.size(); i++) { ... } |
Used to iterate over a list with an index. |
| Operator Type | Operator | Description | Example |
|---|---|---|---|
| Arithmetic |
+, -, *, /
|
Addition, Subtraction, Multiplication, Division |
5 + 2 β 7
|
| Arithmetic | / |
Integer Division (truncates toward zero) |
7 / 2 β 3
|
| Arithmetic | % |
Modulus (remainder) |
7 % 2 β 1
|
| Arithmetic | Math.pow() |
Exponent |
Math.pow(2, 3) β 8.0
|
| Assignment |
+=, -=, *=, /=
|
Adds, subtracts, multiplies, or divides and assigns |
x += 1 β x = x + 1
|
| Comparison |
==, !=
|
Equal to, Not equal to (use .equals() for objects) |
x == y β True or False
|
| Comparison |
>, <, >=, <=
|
Greater/Less than, or equal to |
x > 5 β True or False
|
| Logical |
&&, ||, !
|
Logical AND, OR, NOT |
x > 1 && y < 10 β True or False
|
-
Ternary Operator: Provides a compact, one-line if-else statement. For instance,
String result = (score >= 60) ? "Pass" : "Fail";is much shorter than a full if-else block. -
No Chained Comparisons: Java does not support chained comparisons. Range checks must use logical operators, such as
if (age >= 18 && age < 65). In Python, this could be written asif 18 <= age < 65:. -
String Formatting: Java uses methods like
String.format()orSystem.out.printf()for embedding expressions in strings, similar to Pythonβs F-Strings. For example,String message = String.format("Hello, %s!", name);is cleaner than traditional string concatenation. -
No Tuple or List Unpacking: Java does not have a direct equivalent to Pythonβs tuple and list unpacking. Assignment must be done one variable at a time, such as
String name = "Alice"; int age = 30;. -
Short-Circuiting: The logical operators
&&(AND) and||(OR) are efficient. They stop evaluating as soon as the outcome is known. For example, inif (user != null && user.isAdmin()), the code will not attempt to call.isAdmin()ifuseris null, preventing an error. -
Streams API: Javaβs Stream API is the idiomatic alternative to Pythonβs List Comprehensions. It can be used to filter, map, and reduce data in a sequence of steps. For a simpler example, to generate a basic list of numbers, instead of a multi-line loop, you can write
List<Integer> numbers = IntStream.range(0, 5).boxed().toList();This single line creates a stream of numbers from 0 to 4, prepares them for the list with the `.boxed()` method, and collects them into the final result.
You have attempted of activities on this page.
