From b4e983a7372a19619a68fc886c967fd4889c79be Mon Sep 17 00:00:00 2001 From: FireIsGood Date: Thu, 16 May 2024 18:10:31 -0700 Subject: [PATCH] fix: java format Fixed modifier spacing and made some code into inline code wrapppers. Also replaced shell with bash --- source/_posts/java.md | 348 ++++++++++++++++++++++-------------------- 1 file changed, 183 insertions(+), 165 deletions(-) diff --git a/source/_posts/java.md b/source/_posts/java.md index bc750b7..af59723 100644 --- a/source/_posts/java.md +++ b/source/_posts/java.md @@ -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+ -- - -- * -- / -- % -- = -- ++ -- -- -- ! + +- + +- - +- \* +- / +- % +- = +- ++ +- -- +- ! + {.marker-none .cols-4} ----- +--- + +- == +- != +- > +- >= +- < +- <= -- == -- != -- > -- >= -- < -- <= {.marker-none .cols-4} ----- +--- -- && -- || +- && +- || - [?:](#ternary-operator){data-tooltip="Ternary (shorthand for if-then-else statement)"} + {.marker-none .cols-4} ----- +--- + +- instanceof -- instanceof {.marker-none} ----- +--- + +- ~ +- << +- >> +- >>> +- & +- ^ +- | -- ~ -- << -- >> -- >>> -- & -- ^ -- | {.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 nums = new ArrayList<>(); @@ -594,6 +606,7 @@ for (Integer value : nums) { ``` ### HashMap + ```java Map m = new HashMap<>(); m.put(5, "Five"); @@ -613,6 +626,7 @@ m.forEach((key, value) -> { ``` ### HashSet + ```java Set set = new HashSet<>(); if (set.isEmpty()) { @@ -636,6 +650,7 @@ for (String element : set) { ``` ### ArrayDeque + ```java Deque 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