fix: java format

Fixed modifier spacing and made some code into inline code wrapppers.
Also replaced shell with bash
This commit is contained in:
FireIsGood committed 2024-05-16 18:10:31 -07:00
1 parent d1e93bc51d
commit b4e983a737
1 file changed
+183 -165
+183 -165
View File
@@ -3,22 +3,21 @@ title: Java
date: 2021-03-10 19:50:01
background: bg-[#d33731]
tags:
- object-oriented
- class
- object-oriented
- class
categories:
- Programming
- Programming
intro: |
This cheat sheet is a crash course for Java beginners and help review the basic syntax of the Java language.
This cheat sheet is a crash course for Java beginners and help review the basic syntax of the Java language.
plugins:
- tooltip
- copyCode
- tooltip
- copyCode
---
Getting Started
--------
## Getting Started
### Hello.java {.row-span-2}
```java
public class Hello {
// main method
@@ -29,16 +28,17 @@ public class Hello {
}
}
```
Compiling and running
```shell script
```bash
$ javac Hello.java
$ java Hello
Hello, world!
```
### Variables
```java
int num = 5;
float floatNum = 5.99f;
@@ -48,8 +48,9 @@ String site = "cheatsheets.zip";
```
### Primitive Data Types {.row-span-2}
| Data Type | Size | Default | Range |
|-----------|--------|---------|---------------------|
| --------- | ------ | ------- | :------------------ |
| `byte` | 1 byte | 0 | -128 ^to^ 127 |
| `short` | 2 byte | 0 | -2^15^ ^to^ 2^15^-1 |
| `int` | 4 byte | 0 | -2^31^ ^to^ 2^31^-1 |
@@ -58,24 +59,22 @@ String site = "cheatsheets.zip";
| `double` | 8 byte | 0.0d | _N/A_ |
| `char` | 2 byte | \\u0000 | 0 ^to^ 65535 |
| `boolean` | _N/A_ | false | true / false |
{.show-header}
### Strings
```java
String first = "John";
String last = "Doe";
String name = first + " " + last;
System.out.println(name);
```
See: [Strings](#java-strings)
### Loops
```java
String word = "CheatSheets";
for (char c: word.toCharArray()) {
@@ -83,10 +82,11 @@ for (char c: word.toCharArray()) {
}
// Outputs: C-h-e-a-t-S-h-e-e-t-s-
```
See: [Loops](#java-loops)
### Arrays
```java
char[] chars = new char[10];
chars[0] = 'a'
@@ -96,10 +96,11 @@ String[] letters = {"A", "B", "C"};
int[] mylist = {100, 200};
boolean[] answers = {true, false};
```
See: [Arrays](#java-arrays)
### Swap
```java
int a = 1;
int b = 2;
@@ -112,13 +113,14 @@ System.out.println(a + " " + b); // 2 1
```
### Type Casting
```java
// Widening
// byte<short<int<long<float<double
int i = 10;
long l = i; // 10
// Narrowing
// Narrowing
double d = 10.02;
long l = (long)d; // 10
@@ -128,6 +130,7 @@ Double.parseDouble("10"); // 10.0
```
### Conditionals
```java
int j = 10;
@@ -139,11 +142,11 @@ if (j == 10) {
System.out.println("I also don't");
}
```
See: [Conditionals](#java-conditionals)
### User Input
```java
Scanner in = new Scanner(System.in);
String str = in.nextLine();
@@ -153,19 +156,18 @@ int num = in.nextInt();
System.out.println(num);
```
Java Strings
-------
## Java Strings
### Basic
```java
String str1 = "value";
String str1 = "value";
String str2 = new String("value");
String str3 = String.valueOf(123);
```
### Concatenation
```java
String s = 3 + "str" + 3; // 3str3
String s = 3 + 3 + "str"; // 6str
@@ -175,37 +177,46 @@ String s = "" + 3 + 3 + "23"; // 3323
String s = 3 + 3 + 23; // Incompatible types
```
### StringBuilder {.row-span-3}
StringBuilder sb = new StringBuilder(10);
`StringBuilder sb = new StringBuilder(10);`
```java
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| | | | | | | | | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
```
sb.append("QuickRef");
`sb.append("QuickRef");`
```java
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| Q | u | i | c | k | R | e | f | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
```
sb.delete(5, 9);
`sb.delete(5, 9);`
```java
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| Q | u | i | c | k | | | | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
```
sb.insert(0, "My ");
`sb.insert(0, "My ");`
```java
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| M | y | | Q | u | i | c | k | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
```
sb.append("!");
`sb.append("!");`
```java
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| M | y | | Q | u | i | c | k | ! |
@@ -213,11 +224,11 @@ sb.append("!");
0 1 2 3 4 5 6 7 8 9
```
### Comparison
```java
String s1 = new String("QuickRef");
String s2 = new String("QuickRef");
String s1 = new String("QuickRef");
String s2 = new String("QuickRef");
s1 == s2 // false
s1.equals(s2) // true
@@ -225,8 +236,8 @@ s1.equals(s2) // true
"AB".equalsIgnoreCase("ab") // true
```
### Manipulation
```java
String str = "Abcd";
@@ -239,8 +250,8 @@ str.replace("b", "-"); // A-cd
"ab".toCharArray(); // {'a', 'b'}
```
### Information
```java
String str = "abcd";
@@ -257,9 +268,8 @@ str.startsWith("a"); // true
str.isEmpty(); // false
```
### Immutable
```java
String str = "hello";
str.concat("world");
@@ -280,14 +290,10 @@ System.out.println(concat);
Once created cannot be modified, any modification creates a new String
Java Arrays
-------
## Java Arrays
### Declare
```java
int[] a1;
int[] a2 = {1, 2, 3};
@@ -299,8 +305,8 @@ a4[2] = 2;
a4[3] = 3;
```
### Modify
```java
int[] a = {1, 2, 3};
System.out.println(a[0]); // 1
@@ -311,8 +317,8 @@ System.out.println(a[0]); // 9
System.out.println(a.length); // 3
```
### Loop (Read & Modify)
```java
int[] arr = {1, 2, 3};
for (int i=0; i < arr.length; i++) {
@@ -322,18 +328,18 @@ for (int i=0; i < arr.length; i++) {
// Outputs: 2 4 6
```
### Loop (Read)
```java
String[] arr = {"a", "b", "c"};
for (String a: arr) {
System.out.print(a + " ");
}
// Outputs: a b c
// Outputs: a b c
```
### Multidimensional Arrays
```java
int[][] matrix = { {1, 2, 3}, {4, 5} };
@@ -347,11 +353,11 @@ for (int i = 0; i < a.length; ++i) {
System.out.println(a[i][j]);
}
}
// Outputs: 1 2 3 4 5 6 7
// Outputs: 1 2 3 4 5 6 7
```
### Sort
```java
char[] chars = {'b', 'a', 'c'};
Arrays.sort(chars);
@@ -360,60 +366,61 @@ Arrays.sort(chars);
Arrays.toString(chars);
```
Java Conditionals
-----------
## Java Conditionals
### Operators {.row-span-2}
- <a href="javascript:void(0);" data-tooltip="Additive operator (also used for String concatenation)">+</a>
- <a href="javascript:void(0);" data-tooltip="Subtraction operator">-</a>
- <a href="javascript:void(0);" data-tooltip="Multiplication operator">*</a>
- <a href="javascript:void(0);" data-tooltip="Division operator">/</a>
- <a href="javascript:void(0);" data-tooltip="Remainder operator">%</a>
- <a href="javascript:void(0);" data-tooltip="Simple assignment operator">=</a>
- <a href="javascript:void(0);" data-tooltip="Increment operator; increments a value by 1">++</a>
- <a href="javascript:void(0);" data-tooltip="Decrement operator; decrements a value by 1">--</a>
- <a href="javascript:void(0);" data-tooltip="Logical complement operator; inverts the value of a boolean">!</a>
- <a data-tooltip="Additive operator (also used for String concatenation)">+</a>
- <a data-tooltip="Subtraction operator">-</a>
- <a data-tooltip="Multiplication operator">\*</a>
- <a data-tooltip="Division operator">/</a>
- <a data-tooltip="Remainder operator">%</a>
- <a data-tooltip="Simple assignment operator">=</a>
- <a data-tooltip="Increment operator; increments a value by 1">++</a>
- <a data-tooltip="Decrement operator; decrements a value by 1">--</a>
- <a data-tooltip="Logical complement operator; inverts the value of a boolean">!</a>
{.marker-none .cols-4}
----
---
- <a data-tooltip="Equal to">==</a>
- <a data-tooltip="Not equal to">!=</a>
- <a data-tooltip="Greater than">></a>
- <a data-tooltip="Greater than or equal to">>=</a>
- <a data-tooltip="Less than"><</a>
- <a data-tooltip="Less than or equal to"><=</a>
- <a href="javascript:void(0);" data-tooltip="Equal to">==</a>
- <a href="javascript:void(0);" data-tooltip="Not equal to">!=</a>
- <a href="javascript:void(0);" data-tooltip="Greater than">></a>
- <a href="javascript:void(0);" data-tooltip="Greater than or equal to">>=</a>
- <a href="javascript:void(0);" data-tooltip="Less than"><</a>
- <a href="javascript:void(0);" data-tooltip="Less than or equal to"><=</a>
{.marker-none .cols-4}
----
---
- <a href="javascript:void(0);" data-tooltip="Conditional-AND">&&</a>
- <a href="javascript:void(0);" data-tooltip="Conditional-OR">||</a>
- <a data-tooltip="Conditional-AND">&&</a>
- <a data-tooltip="Conditional-OR">||</a>
- [?:](#ternary-operator){data-tooltip="Ternary (shorthand for if-then-else statement)"}
{.marker-none .cols-4}
----
---
- <a data-tooltip="Compares an object to a specified type">instanceof</a>
- <a href="javascript:void(0);" data-tooltip="Compares an object to a specified type">instanceof</a>
{.marker-none}
----
---
- <a data-tooltip="Unary bitwise complement">~</a>
- <a data-tooltip="Signed left shift"><<</a>
- <a data-tooltip="Signed right shift">>></a>
- <a data-tooltip="Unsigned right shift">>>></a>
- <a data-tooltip="Bitwise AND">&</a>
- <a data-tooltip="Bitwise exclusive OR">^</a>
- <a data-tooltip="Bitwise inclusive OR">|</a>
- <a href="javascript:void(0);" data-tooltip="Unary bitwise complement">~</a>
- <a href="javascript:void(0);" data-tooltip="Signed left shift"><<</a>
- <a href="javascript:void(0);" data-tooltip="Signed right shift">>></a>
- <a href="javascript:void(0);" data-tooltip="Unsigned right shift">>>></a>
- <a href="javascript:void(0);" data-tooltip="Bitwise AND">&</a>
- <a href="javascript:void(0);" data-tooltip="Bitwise exclusive OR">^</a>
- <a href="javascript:void(0);" data-tooltip="Bitwise inclusive OR">|</a>
{.marker-none .cols-4}
### If else
```java
int k = 15;
if (k > 20) {
@@ -426,6 +433,7 @@ if (k > 20) {
```
### Switch {.row-span-2}
```java
int month = 3;
String str;
@@ -448,8 +456,8 @@ switch (month) {
System.out.println("Result " + str);
```
### Ternary operator
```java
int a = 10;
int b = 20;
@@ -459,11 +467,10 @@ int max = (a > b) ? a : b;
System.out.println(max);
```
Java Loops
----
## Java Loops
### For Loop
```java
for (int i = 0; i < 10; i++) {
System.out.print(i);
@@ -471,7 +478,7 @@ for (int i = 0; i < 10; i++) {
// Outputs: 0123456789
```
------
---
```java
for (int i = 0,j = 0; i < 3; i++,j--) {
@@ -481,6 +488,7 @@ for (int i = 0,j = 0; i < 3; i++,j--) {
```
### Enhanced For Loop
```java
int[] numbers = {1,2,3,4,5};
@@ -489,10 +497,11 @@ for (int number: numbers) {
}
// Outputs: 12345
```
Used to loop around array's or List's
### While Loop
```java
int count = 0;
@@ -504,6 +513,7 @@ while (count < 5) {
```
### Do While Loop
```java
int count = 0;
@@ -515,6 +525,7 @@ do {
```
### Continue Statement
```java
for (int i = 0; i < 5; i++) {
if (i == 3) {
@@ -526,6 +537,7 @@ for (int i = 0; i < 5; i++) {
```
### Break Statement
```java
for (int i = 0; i < 5; i++) {
System.out.print(i);
@@ -536,13 +548,12 @@ for (int i = 0; i < 5; i++) {
// Outputs: 0123
```
Java Collections Framework
--------------------
## Java Collections Framework
### Java Collections {.col-span-2}
| Collection | Interface | Ordered | Sorted | Thread safe | Duplicate | Nullable |
|--------------------------------------------------------------------------------------------------------------------|-------------|---------|--------|-------------|-----------|--------------------|
| ------------------------------------------------------------------------------------------------------------------ | ----------- | ------- | ------ | ----------- | --------- | ------------------ |
| [ArrayList](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) | List | Y | _N_ | _N_ | Y | Y |
| [Vector](https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html) | List | Y | _N_ | Y | Y | Y |
| [LinkedList](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html) | List, Deque | Y | _N_ | _N_ | Y | Y |
@@ -565,10 +576,11 @@ Java Collections Framework
| [ArrayBlockingQueue](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html) | Queue | Y | _N_ | Y | Y | _N_ |
| [LinkedBlockingDeque](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html) | Deque | Y | _N_ | Y | Y | _N_ |
| [PriorityBlockingQueue](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html) | Queue | Y | _N_ | Y | Y | _N_ |
{.show-header .left-text}
### ArrayList
```java
List<Integer> nums = new ArrayList<>();
@@ -594,6 +606,7 @@ for (Integer value : nums) {
```
### HashMap
```java
Map<Integer, String> m = new HashMap<>();
m.put(5, "Five");
@@ -613,6 +626,7 @@ m.forEach((key, value) -> {
```
### HashSet
```java
Set<String> set = new HashSet<>();
if (set.isEmpty()) {
@@ -636,6 +650,7 @@ for (String element : set) {
```
### ArrayDeque
```java
Deque<String> a = new ArrayDeque<>();
@@ -658,21 +673,21 @@ System.out.println(a.peek());
System.out.println(a.pop());
```
Misc
----
## Misc
### Access Modifiers {.col-span-2}
| Modifier | Class | Package | Subclass | World |
|-------------|-------|---------|----------|-------|
| ----------- | ----- | ------- | -------- | ----- |
| public | Y | Y | Y | Y |
| protected | Y | Y | Y | _N_ |
| no modifier | Y | Y | _N_ | _N_ |
| private | Y | _N_ | _N_ | _N_ |
{.show-header .left-text}
### Regular expressions
```java
String text = "I am learning Java";
// Removing All Whitespace
@@ -682,84 +697,86 @@ text.replaceAll("\\s+", "");
text.split("\\|");
text.split(Pattern.quote("|"));
```
See: [Regex in java](/regex#regex-in-java)
### Comment
```java
// I am a single line comment!
/*
And I am a
And I am a
multi-line comment!
*/
/**
* This
* is
* documentation
* comment
* This
* is
* documentation
* comment
*/
```
### Keywords {.col-span-2}
- abstract
- continue
- for
- new
- switch
- assert
- default
- goto
- package
- synchronized
- boolean
- do
- if
- private
- this
- break
- double
- implements
- protected
- throw
- byte
- else
- import
- public
- throws
- case
- enum
- instanceof
- return
- transient
- catch
- extends
- int
- short
- try
- char
- final
- interface
- static
- void
- class
- finally
- long
- strictfp
- volatile
- const
- float
- native
- super
- while
- `abstract`
- `continue`
- `for`
- `new`
- `switch`
- `assert`
- `default`
- `goto`
- `package`
- `synchronized`
- `boolean`
- `do`
- `if`
- `private`
- `this`
- `break`
- `double`
- `implements`
- `protected`
- `throw`
- `byte`
- `else`
- `import`
- `public`
- `throws`
- `case`
- `enum`
- `instanceof`
- `return`
- `transient`
- `catch`
- `extends`
- `int`
- `short`
- `try`
- `char`
- `final`
- `interface`
- `static`
- `void`
- `class`
- `finally`
- `long`
- `strictfp`
- `volatile`
- `const`
- `float`
- `native`
- `super`
- `while`
{.marker-none .cols-6}
### Math methods
### Math methods
| Method | Description |
|-----------------------|------------------------|
| --------------------- | :--------------------- |
| `Math.max(a,b)` | Maximum of a and b |
| `Math.min(a,b)` | Minimum of a and b |
| `Math.abs(a)` | Absolute value a |
@@ -775,6 +792,7 @@ multi-line comment!
| `Math.toRadians(deg)` | Angle deg in radians |
### Try/Catch/Finally
```java
try {
// something