Skip to content

Adds a cheatsheet for Java fixes issue #42 #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 205 additions & 0 deletions source/ap-java-cheatsheet.ptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?xml version="1.0" encoding="UTF-8"?>

<section xml:id="ap-java-cheatsheet" xmlns:xi="http://www.w3.org/2001/XInclude">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd to include a section AFTER the backmatter. This seems like an appendix to me and should be xi:included inside the backmatter. Therefore it should also have an appendix tag instead of a section

PreTeXt tries to help you semantically structure your book. Its worth following its lead!

Copy link
Contributor Author

@Tristan-Raz Tristan-Raz Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your help! I've now made the change, converting the section to an appendix and including it inside the backmatter tag. I will make sure to follow PreTeXt's lead on structure in the future.

<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 &lt; 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 &lt; 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></cell>
</row>
<row>
<cell> Arithmetic </cell>
<cell><c>/</c></cell>
<cell> Integer Division (truncates toward zero) </cell>
<cell><c>7 / 2</c> → 3 </cell>
</row>
<row>
<cell> Arithmetic </cell>
<cell><c>%</c></cell>
<cell> Modulus (remainder) </cell>
<cell><c>7 % 2</c> → 1 </cell>
</row>
<row>
<cell> Arithmetic </cell>
<cell><c>Math.pow()</c></cell>
<cell> Exponent </cell>
<cell><c>Math.pow(2, 3)</c> → 8.0 </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></cell>
</row>
<row>
<cell> Comparison </cell>
<cell><c>&gt;</c>, <c>&lt;</c>, <c>&gt;=</c>, <c>&lt;=</c></cell>
<cell> Greater/Less than, or equal to </cell>
<cell><c>x &gt; 5</c></cell>
</row>
<row>
<cell> Logical </cell>
<cell><c>&amp;&amp;</c>, <c>||</c>, <c>!</c></cell>
<cell> Logical AND, OR, NOT </cell>
<cell><c>x &gt; 1 &amp;&amp; y &lt; 10</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></cell>
</row>
</tabular>
</table>

<p>
<ul>
<li>
<p>
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.
</p>
</li>
<li>
<p>
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>.
</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>&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.
</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 example, to generate a list of squares, instead of a multi-line loop, you can write <c>List&lt;Integer&gt; squares = IntStream.range(0, 10).map(i -&gt; i * i).boxed().collect(Collectors.toList());</c>.
</p>
</li>
</ul>
</p>

</paragraphs>
</section>
11 changes: 10 additions & 1 deletion source/meta_backmatter.ptx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?xml version="1.0"?>

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

<appendix>

<title>Java Cheat Sheet</title>
<p>This is a quick reference guide for Java syntax and concepts.</p>
<xi:include href="ap-java-cheatsheet.ptx" />
</appendix>



<appendix xml:id="backmatter_appendix">
<title>Shameless Plug</title>

Expand Down