Skip to content

Commit a1d507f

Browse files
authored
Merge pull request #45 from Tristan-Raz/adding-cheat-sheet-issue42
Adds a cheatsheet for Java fixes issue #42
2 parents 8852b6b + 9dff49d commit a1d507f

File tree

2 files changed

+215
-1
lines changed

2 files changed

+215
-1
lines changed

source/ap-java-cheatsheet.ptx

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

source/meta_backmatter.ptx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<?xml version="1.0"?>
22

33
<!-- Generated by Docutils 0.19 -->
4-
<backmatter xml:id="meta_backmatter">
4+
<backmatter xml:id="meta_backmatter" xmlns:xi="http://www.w3.org/2001/XInclude">
55
<title> Appendices </title>
66

7+
<appendix>
8+
9+
<title>Java Cheat Sheet</title>
10+
<p>This is a quick reference guide for Java syntax and concepts.</p>
11+
<xi:include href="ap-java-cheatsheet.ptx" />
12+
</appendix>
13+
14+
15+
716
<appendix xml:id="backmatter_appendix">
817
<title>Shameless Plug</title>
918

0 commit comments

Comments
 (0)