forked from RunestoneInteractive/java4python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathap-java-cheatsheet.ptx
More file actions
205 lines (202 loc) · 8.23 KB
/
Copy pathap-java-cheatsheet.ptx
File metadata and controls
205 lines (202 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?xml version="1.0" encoding="UTF-8"?>
<section xml:id="ap-java-cheatsheet" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Java Cheat Sheet</title>
<paragraphs>
<title>Purpose of this Cheat Sheet</title>
<p>
</p>
<p>
The following is intended to be useful in better understanding Java functions coming from a Python background.
</p>
</paragraphs>
<paragraphs>
<table>
<title>Function/Method Equivalents: Python to Java</title>
<tabular>
<row header="yes">
<cell> Python Function </cell>
<cell> Java Equivalent </cell>
<cell> Description </cell>
</row>
<row>
<cell><c>print()</c></cell>
<cell><c>System.out.println()</c></cell>
<cell> Prints output to the console. </cell>
</row>
<row>
<cell><c>len()</c></cell>
<cell><c>array.length</c> or <c>list.size()</c></cell>
<cell> Returns the length of an array or size of a list. </cell>
</row>
<row>
<cell><c>range()</c></cell>
<cell><c>for (int i = 0; i < n; i++)</c></cell>
<cell> Used in loops to iterate a specific number of times. </cell>
</row>
<row>
<cell><c>str()</c></cell>
<cell><c>String.valueOf()</c></cell>
<cell> Converts an object to a string. </cell>
</row>
<row>
<cell><c>int()</c></cell>
<cell><c>Integer.parseInt()</c></cell>
<cell> Converts a string to an integer. </cell>
</row>
<row>
<cell><c>float()</c></cell>
<cell><c>Float.parseFloat()</c></cell>
<cell> Converts a string to a float. </cell>
</row>
<row>
<cell><c>list.append()</c></cell>
<cell><c>ArrayList.add()</c></cell>
<cell> Adds an element to the end of a list. </cell>
</row>
<row>
<cell><c>list.pop()</c></cell>
<cell><c>ArrayList.remove(index)</c></cell>
<cell> Removes and assign the return value to use it.</cell>
</row>
<row>
<cell><c>list.sort()</c></cell>
<cell><c>Collections.sort(list)</c></cell>
<cell> Sorts a list in ascending order. </cell>
</row>
<row>
<cell><c>list.reverse()</c></cell>
<cell><c>Collections.reverse(list)</c></cell>
<cell> Reverses the order of elements in a list. </cell>
</row>
<row>
<cell><c>dict.get()</c></cell>
<cell><c>Map.get(key)</c></cell>
<cell> Retrieves the value associated with a key in a map. </cell>
</row>
<row>
<cell><c>dict.keys()</c></cell>
<cell><c>Map.keySet()</c></cell>
<cell> Returns a set of keys in a map. </cell>
</row>
<row>
<cell><c>dict.values()</c></cell>
<cell><c>Map.values()</c></cell>
<cell> Returns a collection of values in a map. </cell>
</row>
<row>
<cell><c>dict.items()</c></cell>
<cell><c>Map.entrySet()</c></cell>
<cell> Returns a set of key-value pairs in a map. </cell>
</row>
<row>
<cell><c>input()</c></cell>
<cell><c>Scanner.nextLine()</c></cell>
<cell> Reads a line of input from the console. </cell>
</row>
<row>
<cell><c>open()</c></cell>
<cell><c>FileReader</c>, <c>BufferedReader</c></cell>
<cell> Used to read from files. </cell>
</row>
<row>
<cell><c>enumerate()</c></cell>
<cell><c>for (int i = 0; i < list.size(); i++) { ... }</c></cell>
<cell> Used to iterate over a list with an index. </cell>
</row>
</tabular>
</table>
<table>
<title>Operator Equivalents and Usage</title>
<tabular>
<row header="yes">
<cell> Operator Type </cell>
<cell> Operator </cell>
<cell> Description </cell>
<cell> Example </cell>
</row>
<row>
<cell> Arithmetic </cell>
<cell><c>+</c>, <c>-</c>, <c>*</c>, <c>/</c></cell>
<cell> Addition, Subtraction, Multiplication, Division </cell>
<cell><c>5 + 2</c> → <c> 7 </c></cell>
</row>
<row>
<cell> Arithmetic </cell>
<cell><c>/</c></cell>
<cell> Integer Division (truncates toward zero) </cell>
<cell><c>7 / 2</c> → <c> 3 </c> </cell>
</row>
<row>
<cell> Arithmetic </cell>
<cell><c>%</c></cell>
<cell> Modulus (remainder) </cell>
<cell><c>7 % 2</c> → <c> 1 </c></cell>
</row>
<row>
<cell> Arithmetic </cell>
<cell><c>Math.pow()</c></cell>
<cell> Exponent </cell>
<cell><c>Math.pow(2, 3)</c> → <c> 8.0 </c></cell>
</row>
<row>
<cell> Assignment </cell>
<cell><c>+=</c>, <c>-=</c>, <c>*=</c>, <c>/=</c></cell>
<cell> Adds, subtracts, multiplies, or divides and assigns </cell>
<cell><c>x += 1</c> → <c>x = x + 1</c> </cell>
</row>
<row>
<cell> Comparison </cell>
<cell><c>==</c>, <c>!=</c></cell>
<cell> Equal to, Not equal to (use <c>.equals()</c> for objects) </cell>
<cell><c>x == y</c> → <c>True</c> or <c>False</c> </cell>
</row>
<row>
<cell> Comparison </cell>
<cell><c>></c>, <c><</c>, <c>>=</c>, <c><=</c></cell>
<cell> Greater/Less than, or equal to </cell>
<cell><c>x > 5</c> → <c>True</c> or <c>False</c> </cell>
</row>
<row>
<cell> Logical </cell>
<cell><c>&&</c>, <c>||</c>, <c>!</c></cell>
<cell> Logical AND, OR, NOT </cell>
<cell><c>x > 1 && y < 10</c> → <c>True</c> or <c>False</c> </cell>
</row>
</tabular>
</table>
<p>
<ul>
<li>
<p>
Ternary Operator: Provides a compact, one-line if-else statement. For instance, <c>String result = (score >= 60) ? "Pass" : "Fail";</c> is much shorter than a full if-else block.
</p>
</li>
<li>
<p>
No Chained Comparisons: Java does not support chained comparisons. Range checks must use logical operators, such as <c>if (age >= 18 && age < 65)</c>. In Python, this could be written as <c>if 18 <= age < 65:</c>.
</p>
</li>
<li>
<p>
String Formatting: Java uses methods like <c>String.format()</c> or <c>System.out.printf()</c> for embedding expressions in strings, similar to Python's F-Strings. For example, <c>String message = String.format("Hello, %s!", name);</c> is cleaner than traditional string concatenation.
</p>
</li>
<li>
<p>
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 <c>String name = "Alice"; int age = 30;</c>.
</p>
</li>
<li>
<p>
Short-Circuiting: The logical operators <c>&&</c> (AND) and <c>||</c> (OR) are efficient. They stop evaluating as soon as the outcome is known. For example, in <c>if (user != null && user.isAdmin())</c>, the code will not attempt to call <c>.isAdmin()</c> if <c>user</c> is null, preventing an error.
</p>
</li>
<li>
<p>
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 <c>List<Integer> numbers = IntStream.range(0, 5).boxed().toList(); </c> 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.
</p>
</li>
</ul>
</p>
</paragraphs>
</section>