Cheatography
https://cheatography.com
Complete Cheat Sheet on Java 17, WIP
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Primitive Data types
Name |
Size |
Values |
|
1 B |
-128 → 127 |
|
2 B |
-32768 → 32767 |
|
4 B |
-2∙10⁹ → 2∙10⁹ |
|
8 B |
-9∙10¹⁵ → 9∙10¹⁵ |
|
4 B |
6-7 decimal digits |
|
8 B |
15 decimal digits |
|
1 b |
|
|
2 B |
Characters or ASCII |
Comparison Operators
Equal |
|
Not equal |
|
Less |
|
Less or equal |
|
Greater |
|
Greater or equal |
|
String Methods
Usage |
Description |
|
Length of a string |
|
Extract the nth character |
|
Returns a copy of a string in ALL CAPS |
|
Returns a copy of a string in lowecase |
|
Returns the first occurring index of c |
|
Search and Replace |
|
Splits string into tokens |
|
Compares strings (true if equal) |
|
Returns "0" if equal, "+" if s > s2, "-" if s < s2 |
IF Statement
// conditional handler
if (condition) {
// pose a condition ...
} else if (other condition) {
// if first condition not met pose another one ...
} else {
// if second condition not met finally ...
}
|
FOR EACH Statement
// self-iterating loop for iterable objects
for (var: collection) {
statements
}
|
FOR Statement
// structured iterator for non-iterable objects
for (int i = 0; i < max; i++) {}
|
Continue
for (int i = 0; i < max; i++) {
// jump to the next iteration
continue;
}
|
Break
while (true) {
// exit early a loop
break;
}
|
TRY-CATCH Statement
// exception handler
try {
// try to do something
} catch (exception e) {
// when you fail do...
} finally {
// when you are done do...
}
|
Enumerals
// "class" containing a group of constants
enum Name {
NAME1,
NAME2,
NAME3
}
// constant are accessible per dot notation
// they are typed as the declared enum
Name var = Name.NAME1;
|
Object Example
public class Object {
private <type> attr1;
public Object (<type> attr1) {
this.attr1 = attr1;
}
public void method() {
// ...
}
}
|
Interfaces
// An interface manages accessibility of object
// it is an abstract object
public interface Intrf {
String const = "CIAO";
public void method1();
}
public class Obj implements Intrf {
public void Obj() { }
@override
public void method1() {}
}
|
Parent Class
// parent class
public class Parent implements Intrf {}
// child class, inherits Parents characteristics
public class Child extends Parent {}
|
ArrayList Methods
Usage |
Description |
|
Add item to the list |
|
Return the nth item |
|
Return number of item |
|
Remove the nth item |
|
Put at the nth position value |
|
|
Non-Primitive Data Types
Name |
Declaration |
String |
|
Array |
|
Class |
|
Interface |
public interface ObjInterface { ... }
|
Arithmetic Operators
Addition |
|
Subtraction |
|
Multiplication |
|
Division |
|
Increment |
|
Decrement |
|
Modulus |
|
WHILE Statement
// pre-conditioned iterator ...
while (condition) {}
|
Scanner
import java.utils.Scanner;
public class Obj{
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
int i = input.nextInt();
input.close();
}
}
|
DO-WHILE Statement
// post-conditional iterator
do { } while (condition);
|
SWITCH Statement
// case-oriented condition handler
switch (condition) {
case value0:
statements
break;
case value1:
statements
break;
default:
statements
}
|
Ternary operator
// same as an if, but inline!
condition ? iftrue : iffalse;
|
Type conversion
|
int i = Integer.parseInt(str);
|
|
String s = String.valueOf(value);
|
Between numeric types |
|
Array Utilities
Usage |
Description |
Arrays.copyOf(og, og.length);` |
Copy array onto another one |
Recursion
// method that sums all the number 1..10
// attention! recursion can incur in an infinite loop
// handle it well
public static int sum(int start, int end) {
if (end > start) {
return end + sum(start, end - 1);
} else {
return end;
}
}
|
Access Definitions
|
|
|
|
|
Class |
• |
• |
• |
• |
Package |
• |
• |
• |
|
Subclasses |
• |
• |
|
|
Classes |
• |
|
|
|
Usage of an Object
// to use an object
public static void main (String[] args) {
// declaration meets constructor method
Object obj = new Object(attr1);
obj.method();
}
|
Dynamic Data Types
import java.util.*;
List<type> names = new ArrayList<type>();
Vector<type> vec = new Vector<type>();
Stack<type> stk= new Stack<>();
|
Dynamic Data Types Methods
Vector |
|
Add an element to the end |
|
Add an element to a specific index |
|
Returns the current capacity |
|
Empty vector |
|
Clone the vector |
Stack Inherits from Vector |
|
Returns true if head is empty |
|
Returns head of stack |
|
Delete head of stack |
|
Add an element at head |
|
Returns index of element |
|