From 701f18f53476138996568bce8953e691ad29a190 Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Wed, 11 Jan 2023 07:43:43 -0500 Subject: [PATCH 1/2] Port files from old repo --- src/SUMMARY.md | 165 ++++++++++- src/boolean.md | 13 + src/boolean/and.md | 19 ++ src/boolean/challenges.md | 70 +++++ src/boolean/not.md | 15 + src/boolean/operator_precedance.md | 28 ++ src/boolean/or.md | 29 ++ src/chapter_1.md | 3 - src/first_steps.md | 69 +++++ src/first_steps/challenges.md | 54 ++++ src/first_steps/comments.md | 59 ++++ src/first_steps/formatting.md | 66 +++++ src/first_steps/semicolon.md | 50 ++++ src/floating_point_numbers.md | 4 + src/floating_point_numbers/limits.md | 1 + src/getting_started/hello_world.md | 14 + src/integers.md | 11 + src/integers/addition.md | 26 ++ src/integers/chained_comparisons.md | 16 + src/integers/challenges.md | 106 +++++++ src/integers/comparison.md | 44 +++ src/integers/division.md | 26 ++ src/integers/equality.md | 22 ++ src/integers/limits.md | 33 +++ src/integers/multiplication.md | 16 + src/integers/operator_precedance.md | 40 +++ src/integers/reassignment.md | 23 ++ src/integers/remainder.md | 55 ++++ src/integers/shorthands_for_reassignment.md | 61 ++++ src/integers/subtraction.md | 26 ++ src/limits.md | 0 src/modern/bchallenges.md | 1 + src/numbers.md | 3 + src/numbers/float_and_double.md | 1 + src/numbers/int_and_long.md | 1 + src/paths.md | 87 ++++++ src/prelude.md | 39 +++ src/rant.md | 0 src/scratch.md | 80 +++++ src/strings.md | 1 + src/strings/escaped_characters.md | 1 + src/strings/multiline.md | 25 ++ src/strings/string_theory.md | 1 + src/tour_of_java.md | 4 + src/types.md | 1 + src/types/numbers.md | 19 ++ src/types/strings.md | 1 + src/variables.md | 31 ++ src/variables/challenges.md | 111 +++++++ src/variables/delayed_assignment.md | 45 +++ src/variables/final_variables.md | 71 +++++ src/variables/inferred_types.md | 40 +++ src/variables/naming.md | 20 ++ src/variables/reassignment.md | 37 +++ src/variables/types.md | 33 +++ theme/index.hbs | 313 ++++++++++++++++++++ 56 files changed, 2125 insertions(+), 5 deletions(-) create mode 100644 src/boolean.md create mode 100644 src/boolean/and.md create mode 100644 src/boolean/challenges.md create mode 100644 src/boolean/not.md create mode 100644 src/boolean/operator_precedance.md create mode 100644 src/boolean/or.md delete mode 100644 src/chapter_1.md create mode 100644 src/first_steps.md create mode 100644 src/first_steps/challenges.md create mode 100644 src/first_steps/comments.md create mode 100644 src/first_steps/formatting.md create mode 100644 src/first_steps/semicolon.md create mode 100644 src/floating_point_numbers.md create mode 100644 src/floating_point_numbers/limits.md create mode 100644 src/getting_started/hello_world.md create mode 100644 src/integers.md create mode 100644 src/integers/addition.md create mode 100644 src/integers/chained_comparisons.md create mode 100644 src/integers/challenges.md create mode 100644 src/integers/comparison.md create mode 100644 src/integers/division.md create mode 100644 src/integers/equality.md create mode 100644 src/integers/limits.md create mode 100644 src/integers/multiplication.md create mode 100644 src/integers/operator_precedance.md create mode 100644 src/integers/reassignment.md create mode 100644 src/integers/remainder.md create mode 100644 src/integers/shorthands_for_reassignment.md create mode 100644 src/integers/subtraction.md create mode 100644 src/limits.md create mode 100644 src/modern/bchallenges.md create mode 100644 src/numbers.md create mode 100644 src/numbers/float_and_double.md create mode 100644 src/numbers/int_and_long.md create mode 100644 src/paths.md create mode 100644 src/prelude.md create mode 100644 src/rant.md create mode 100644 src/scratch.md create mode 100644 src/strings.md create mode 100644 src/strings/escaped_characters.md create mode 100644 src/strings/multiline.md create mode 100644 src/strings/string_theory.md create mode 100644 src/tour_of_java.md create mode 100644 src/types.md create mode 100644 src/types/numbers.md create mode 100644 src/types/strings.md create mode 100644 src/variables.md create mode 100644 src/variables/challenges.md create mode 100644 src/variables/delayed_assignment.md create mode 100644 src/variables/final_variables.md create mode 100644 src/variables/inferred_types.md create mode 100644 src/variables/naming.md create mode 100644 src/variables/reassignment.md create mode 100644 src/variables/types.md create mode 100644 theme/index.hbs diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7390c82..cb50eeb 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -1,3 +1,164 @@ -# Summary + +# Getting Started +- [Hello, World](./getting_started/hello_world.md) +# Modern Java +- [Prelude](./prelude.md) +- [First Steps](./first_steps.md) + - [Comments](./first_steps/comments.md) + - [Semicolons](./first_steps/semicolon.md) + - [Formatting](./first_steps/formatting.md) + - [Challenges](./first_steps/challenges.md) +- [Local Variables](./variables.md) + - [Naming](./variables/naming.md) + - [Reassignment](./variables/reassignment.md) + - [Delayed Assignment](./variables/delayed_assignment.md) + - [Types](./variables/types.md) + - [Final Variables](./variables/final_variables.md) + - [Inferred Types](./variables/inferred_types.md) + - [Challenges](./variables/challenges.md) +- [Booleans](./boolean.md) + - [And](./boolean/and.md) + - [Or](./boolean/or.md) + - [Not](./boolean/not.md) + - [Operator Precedance](./boolean/operator_precedance.md) + - [Challenges](./modern/bchallenges.md) +- [Integers](./integers.md) + - [Addition](./integers/addition.md) + - [Subtraction](./integers/subtraction.md) + - [Multiplication](./integers/multiplication.md) + - [Division](./integers/division.md) + - [Remainder](./integers/remainder.md) + - [Equality](./integers/equality.md) + - [Comparison](./integers/comparison.md) + - [Chained Comparisons](./integers/chained_comparisons.md) + - [Operator Precedance](./integers/operator_precedance.md) + - [Reassignment](./integers/reassignment.md) + - [Shorthands for Reassignment](./integers/shorthands_for_reassignment.md) + - [Limits](./integers/limits.md) + - [Challenges](./integers/challenges.md) +- [Floating Point Numbers](./floating_point_numbers.md) + - [Limits](./floating_point_numbers/limits.md) + - [float and double](./numbers/float_and_double.md) + - [Precision]() + - [The "epsilon" Pattern]() -- [Chapter 1](./chapter_1.md) + +- [Strings](./strings.md) + - [String Theory](./strings/string_theory.md) + - [Escaped Characters](./strings/escaped_characters.md) + - [Multiline Strings](./strings/multiline.md) + +- [If and Else]() + - [If Statement]() + - [Relation to Delayed Assignment]() + - [Integer Comparisons]() + - [Floating Point Number Comparisons]() + - [String Comparisons]() + - [Ternary Expression]() + +- [While Loop]() +- [Arrays]() + - ["String\[\] args"]() +- [For Each Loop]() +- [Counted For Loop]() +- [Methods]() + - [Arguments]() + - [Return Value]() + - [void]() +- [String Methods]() + - [length]() +- [User Defined Classes]() + - [Fields]() + - [Constructors]() + - [Methods]() + + +- [Control Flow]() + + + + + + + + + \ No newline at end of file diff --git a/src/boolean.md b/src/boolean.md new file mode 100644 index 0000000..7321af1 --- /dev/null +++ b/src/boolean.md @@ -0,0 +1,13 @@ +# Booleans + + +A `boolean` is either `true` or `false`. + + +```java +boolean onFleek = true; +boolean badVibes = false; +``` + +This is used to represent situations where there are exactly two possible states. + diff --git a/src/boolean/and.md b/src/boolean/and.md new file mode 100644 index 0000000..7361c41 --- /dev/null +++ b/src/boolean/and.md @@ -0,0 +1,19 @@ +# And + +One way multiple booleans can be combined is by using the "and" operator - `&&`. + +```java +boolean funToBeAround = true; +boolean believesInFundamentalHumanRights = true; +boolean willAskOnDate = funToBeAround && believesInFundamentalHumanRights; +``` + +So in this case, I will ask someone on a date if they are fun to be around _and_ +they wholeheartedly believe in the assertions made in the [Universal Declaration of Human Rights](https://www.un.org/en/about-us/universal-declaration-of-human-rights). + +| funToBeAround | believesInFundamentalHumanRights | willAskOnDate | +|---------------|----------------------------------|---------------| +| true | true | true | +| true | false | false | +| false | true | false | +| false | false | false | \ No newline at end of file diff --git a/src/boolean/challenges.md b/src/boolean/challenges.md new file mode 100644 index 0000000..0608775 --- /dev/null +++ b/src/boolean/challenges.md @@ -0,0 +1,70 @@ +# Challenges + +Remember the rules for this are +* Try to use only the information given up to this point in this book. +* Try not to give up until you've given it a solid attempt + +## Challenge 1. + +What will this program output when run? Write down your guess and then try running it. + +```java +public class Main { + public static void main(String[] args) { + boolean a = true; + boolean b = false; + boolean c = true; + boolean d = false; + + boolean result = a || b && c || !d; + + System.out.println(result); + } +} +``` + +## Challenge 2. + +What will this program output when run? Write down your guess and then try running it. + +```java +public class Main { + public static void main(String[] args) { + boolean a = true; + boolean b = false; + boolean c = true; + boolean d = false; + + boolean result = !(a || b && c || !d) || (a && b || c); + + System.out.println(result); + } +} +``` + +## Challenge 3. + +Say you have two boolean variables, how could you use the operators we've covered to get the "exclusive or" of the two. + +```java +public class Main { + public static void main(String[] args) { + // Change these two variables to test your solution + boolean hasIceCream = true; + boolean hasCookie = false; + + boolean validChoice = < YOUR CODE HERE >; + + System.out.println(validChoice); + } +} +``` + +Make sure to test all the possibilities. + +| hasIceCream | hasCookie | validChoice | +|-------------|-----------|-------------| +| true | true | false | +| true | false | true | +| false | true | true | +| false | false | false | \ No newline at end of file diff --git a/src/boolean/not.md b/src/boolean/not.md new file mode 100644 index 0000000..7bc6df3 --- /dev/null +++ b/src/boolean/not.md @@ -0,0 +1,15 @@ +# Not + +Booleans can alsobe "negated" using the "not" operator - `!`. + +```java +boolean haveOreosInHouse = true; +boolean stuckToCalorieLimit = !haveOreos; +``` + +So in this case, I have stuck to my calorie limit if there are _not_ Oreos in the house. + +| haveOreosInHouse | stuckToCalorieLimit | +|------------------|---------------------| +| false | true | +| true | false | \ No newline at end of file diff --git a/src/boolean/operator_precedance.md b/src/boolean/operator_precedance.md new file mode 100644 index 0000000..5ee21c8 --- /dev/null +++ b/src/boolean/operator_precedance.md @@ -0,0 +1,28 @@ +# Operator Precedance + +The operators that work on booleans have a "precedance order." + +This is defines an order of operations similar to mathematics, where multiplication and division happen before +addition and subtraction. + +For booleans `!` always happens first. This is followed by `&&` and then by `||`. + +```java +boolean a = true; +boolean b = false +boolean c = false; + +// just as 2 + 5 * 3 "evaluates" 5 * 3 before adding 2 +// first, !b is true +// second, a && true is true +// third true || c is true. +boolean result = a && !b || c; +``` + +Also like mathematics, parentheses can be used to control this order. + +```java +// Even though || has a lower precedance than &&, we evaluate +// !b || c first because of the parentheses. +boolean result = a && (!b || c); +``` \ No newline at end of file diff --git a/src/boolean/or.md b/src/boolean/or.md new file mode 100644 index 0000000..24cac5c --- /dev/null +++ b/src/boolean/or.md @@ -0,0 +1,29 @@ +# Or + +Another way booleans can be combined is by using the "or" operator - `||`. + +```java +boolean dogLooksNice = true; +boolean personLooksNice = false; +boolean willAskToPetDog = dogLooksNice || personLooksNice; +``` + +So in this case, I will ask to pet someone's dog if either the the dog looks nice _or_ the person +walking the dog looks nice. + +| dogLooksNice | personLooksNice | willAskToPetDog | +|--------------|-----------------|-----------------| +| true | true | true | +| true | false | true | +| false | true | true | +| false | false | false | + +## Exclusive vs. Inclusive + +It is important too note that this is not an "exclusive" OR. + +An exclusive OR would be something like +a child being allowed to have ice cream _or_ a cookie, but not both. + +The `||` operator is an "inclusive" OR, meaning the child is allowed ice cream, a cookie, or both ice cream and the cookie. + diff --git a/src/chapter_1.md b/src/chapter_1.md deleted file mode 100644 index 6771576..0000000 --- a/src/chapter_1.md +++ /dev/null @@ -1,3 +0,0 @@ -# Chapter 1 - -Hello World! \ No newline at end of file diff --git a/src/first_steps.md b/src/first_steps.md new file mode 100644 index 0000000..48d9c7f --- /dev/null +++ b/src/first_steps.md @@ -0,0 +1,69 @@ +# First Steps + +If you made it through the [Getting Started](../getting_started/hello_world.md) you've successfully run this program. + +```java +public class Main { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } +} +``` + +This "prints" - not in the sense of a physical printer, but like "displays on the screen" - +the text `"Hello, World!"`. + +Its a tradition for this to be your first program in +any language. Unfortunately, for reasons that are impossible to explain with the context you have at this point, +half of this probably reads as cryptic nonsense. + +```java +public class Main { + public static void main(String[] args) { +``` + +I don't _want_ it to stay cryptic nonsense, but until we get there all you truly need to know +is that Java uses all of that to know where to start the program. + +```java +public class Main { + public static void main(String[] args) { + < WRITE YOUR CODE HERE > + } +} +``` + +So for all intents and purposes, this is the whole program. + +```java +System.out.println("Hello, World!"); +``` + +This bit of magic here - `System.out.println` - is a "statement" that "prints" the text inside the `(` and `)` as well as a "new line" to the screen. + +`print` with new `l`i`n`e. + +If you were to replace it with `System.out.print`, then the output would lack that new line. This makes the following program be functionally identical to the first. + +```java +System.out.print("Hello, "); +System.out.print("World"); +System.out.println("!"); +``` + +Which, when we add back the part you are squinting past, looks like this. + +```java +public class Main { + public static void main(String[] args) { + System.out.print("Hello, "); + System.out.print("World"); + System.out.println("!"); + } +} +``` + +You should get in the habit of, whenever you see some bit of code, trying to physically type it out, run it, +tweak it, and play with it. + +So try playing around with this program. If you are not actively engaging then the whole thing is a bit of a wash. diff --git a/src/first_steps/challenges.md b/src/first_steps/challenges.md new file mode 100644 index 0000000..00d3679 --- /dev/null +++ b/src/first_steps/challenges.md @@ -0,0 +1,54 @@ +# Challenges + + +At the end of each larger section, I am going to write down some things you can do +to make sure you get what was just gone over. + +The rules for this are +* Try to use only the information given up to this point in this book. +* Try not to give up until you've given it a solid attempt + +## Challenge 1. + +Write a program that prints your name twice. So if your name is "Jasmine", the output of the program should be this. + +``` +Jasmine +Jasmine +``` + +## Challenge 2. + +What will this program output when run? Write down your guess and then try actually running it. + +``` +public class Main { + public static void main(String[] args) { + System.out.println("A"); + //System.out.println("B"); + System.out.println("C");// + System.out.println("D"); + /* + System.out.println("E"); + System.out.println("F");*/ + System.out.println("G"); + } +} +``` + +## Challenge 3. + +There are four semicolons in this perfectly functional program. Delete one of them at random and see what the errors you get look like. + +How could you use that error to figure out where you might have forgotten to put a semicolon? + +```java +public class Main { + public static void main(String[] args) { + System.out.println("Apple"); + System.out.println("Banana"); + System.out.println("Clementine"); + System.out.println("Durian"); + } +} +``` \ No newline at end of file diff --git a/src/first_steps/comments.md b/src/first_steps/comments.md new file mode 100644 index 0000000..a2c564c --- /dev/null +++ b/src/first_steps/comments.md @@ -0,0 +1,59 @@ +# Comments +At various points, I am going to leave "comments" in the code. Comments are parts of the code that +are solely there for a human to be able to read as an explanation and can be written in regular +words. + +```java +public class Main { + public static void main(String[] args) { + // This prints hello world! + System.out.println("Hello, World!"); + } +} +``` + +The rules for this are that if you see a `//`, everything after that in the same line +is ignored. + +If you put `//` in front of something that is "code" and not an English explanation we colloquially call that "commenting out" the line. + +```java +public class Main { + public static void main(String[] args) { + System.out.println("Hello, World!"); + // The line that prints out goodbye is "commented out" + // System.out.println("Goodbye!"); + } +} +``` + +You might want to do that at various points where you want to see what happens if you "turn off" parts of +the code. + +If you put `/*` in the code then everything up until the next `*/` will be treated as a comment. The distinction +here is that this style of comment can span multiple lines. + +```java +public class Main { + public static void main(String[] args) { + /* + I have eaten + the plums + that were in + the icebox + and which + you were probably + saving + for breakfast + Forgive me + they were delicious + so sweet + and so cold + */ + System.out.println("Hello, World!"); + } +} +``` + +So that's a mechanism you will see me use and you can use yourself however you see fit. + diff --git a/src/first_steps/formatting.md b/src/first_steps/formatting.md new file mode 100644 index 0000000..0e56140 --- /dev/null +++ b/src/first_steps/formatting.md @@ -0,0 +1,66 @@ +# Formatting + + +You may have noticed that after each `{` all the code that comes after it is "indented" in one "level." + +```java +public class Main { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } +} +``` + +Then, when there is a `}` everything is "de-dented" one level. + +I will kindly ask that you try to stick to this rule when writing your own code as well. +If you try to find help online and you haven't, it will be dyslexically hard for people +to read your code. + +So don't do this. + +```java +public class Main { +public static void main(String[] args) { +System.out.println("Hello, World!"); +}} +``` + +Don't do this. + +```java +public class Main + { + public static void main(String[] args) + { + System.out.println("Hello, World!"); + } + } +``` + +Don't do this. + +```java +public class Main { + public static void main(String[] args) + { System.out.println("Hello, World!"); + }} +``` + +And this is just insane. + +```java +public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } +``` + +Do this. + +```java +public class Main { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } +} +``` + +And keep in mind that this rule of thumb applies to every language constrict that requires a `{` and `}` many of which I will introduce later. diff --git a/src/first_steps/semicolon.md b/src/first_steps/semicolon.md new file mode 100644 index 0000000..176a74b --- /dev/null +++ b/src/first_steps/semicolon.md @@ -0,0 +1,50 @@ +# Semicolons + +The `;` at the end of each of those lines is a "semicolon". + +```java +System.out.print("Hello, "); // <-- this thing + // ^ +``` + +It indicates that the statement has finished. A "statement" is a line of code that "does something." +The reason we call it a statement and not just a "line of code" is that it can technically span multiple lines and be +more complicated than these examples. + +```java +System.out.print( + "Hello, " +); +``` + +The `;` at the end is needed so that Java knows that the statement is over. + You need to put a `;` +at the end of every statement. If you do not, Java will get confused and your code will not work. + +If you happen to have an extra semi-colon or two that is technically okay. It just reads it as an "empty statement." Its pointless, but it is allowed. + +```java +System.out.print( + "Hello, " +);; +``` + +Or even + +```java +System.out.print( + "Hello, " +); + + // Technically legal, but kinda sus + + ;;;;;;;;;;;;; + ;;; ;; + ;;; ;; + ;;;;;;;;;;;;; + ;; ;;; ;; + ;;;;;;;;;;;;; + ; ; ; ; + ; ; ; ; + ;;; ;;; +``` \ No newline at end of file diff --git a/src/floating_point_numbers.md b/src/floating_point_numbers.md new file mode 100644 index 0000000..7ff6ff3 --- /dev/null +++ b/src/floating_point_numbers.md @@ -0,0 +1,4 @@ +# Floating Point Numbers + +In order to represent numbers like `2.5` - which `int` cannot - +Java gives diff --git a/src/floating_point_numbers/limits.md b/src/floating_point_numbers/limits.md new file mode 100644 index 0000000..7947c80 --- /dev/null +++ b/src/floating_point_numbers/limits.md @@ -0,0 +1 @@ +# Limits diff --git a/src/getting_started/hello_world.md b/src/getting_started/hello_world.md new file mode 100644 index 0000000..2e5d81a --- /dev/null +++ b/src/getting_started/hello_world.md @@ -0,0 +1,14 @@ +# Hello, World + +There are a lot of ways to "get set up" to run Java code. +Some are closer to how you program in "the real world" than others + + + +```java +public class Main { + public static void main(String[] args) { + System.out.println("Hello, World"); + } +} +``` diff --git a/src/integers.md b/src/integers.md new file mode 100644 index 0000000..293f2bb --- /dev/null +++ b/src/integers.md @@ -0,0 +1,11 @@ +# Integers + +[An integer is any number in the set `{ ..., -2, -1, 0, 1, 2, ... }.`](https://www.khanacademy.org/math/cc-sixth-grade-math/cc-6th-expressions-and-variables/whole-numbers-integers/a/whole-numbers-integers) + +```java +int x = 1; +int y = 8; +int z = -4; +``` + + diff --git a/src/integers/addition.md b/src/integers/addition.md new file mode 100644 index 0000000..007006f --- /dev/null +++ b/src/integers/addition.md @@ -0,0 +1,26 @@ +# Addition + +You can add any two `int`s using the `+` operator. + +```java +int x = 5; +// y will be 6 +int y = x + 1; +// z will be 11 +int z = x + y; + +System.out.println(x); +System.out.println(y); +System.out.println(z); +``` + +Adding a negative number does the same thing as subtraction. + +```java +int x = 5; +// y will be 1 +int y = x + -4; + +System.out.println(x); +System.out.println(y); +``` diff --git a/src/integers/chained_comparisons.md b/src/integers/chained_comparisons.md new file mode 100644 index 0000000..a9eebc0 --- /dev/null +++ b/src/integers/chained_comparisons.md @@ -0,0 +1,16 @@ +# Chained Comparisons + +When writing an expression in math to say something along the lines of +"`x` is greater than zero and less than 5," it is natural to put that `x` +in the middle of both operators like so. + +``` +0 < x < 5 +``` + +This does not work in Java. In order to "chain" comparisons like this, you should combine +the results of comparisons using the `&&` operator. + +```java +boolean xInRange = 0 < x && x < 5; +``` diff --git a/src/integers/challenges.md b/src/integers/challenges.md new file mode 100644 index 0000000..75fb15f --- /dev/null +++ b/src/integers/challenges.md @@ -0,0 +1,106 @@ +# Challenges + +Remember the rules for this are +* Try to use only the information given up to this point in this book. +* Try not to give up until you've given it a solid attempt + +## Challenge 1. + +What will this program output when run? Write down your guess and then try running it. + +```java +public class Main { + public static void main(String[] args) { + int x = 5; + int y = 8; + System.out.println(x + y); + } +} +``` + +## Challenge 2. + +What will this program output when run? Write down your guess and then try running it. + +```java +public class Main { + public static void main(String[] args) { + int x = 5; + x--; + x--; + x = x + x; + System.out.println(x + y); + } +} +``` + +## Challenge 3 +Make it so that this program correctly determines if the numbers are even or not. + +Assume that the values of `x`, `y`, and `z` could be changed. Don't just write out +literally `true` and `false` for their current values. + +```java +public class Main { + public static void main(String[] args) { + int x = 5; + int y = 4; + int z = 98; + + boolean xIsEven = < CODE HERE >; + System.out.println(xIsEven); + + boolean yIsEven = < CODE HERE >; + System.out.println(yIsEven); + + boolean zIsEven = < CODE HERE >; + System.out.println(zIsEven); + } +} +``` + + +## Challenge 4 + +Try dividing a number by zero. What happens? + +Write down your guess and then try running the program below to see. + +```java +public class Main { + public static void main(String[] args) { + System.out.println(5 / 0); + } +} +``` + +## Challenge 5 + +What can you write in the spot marked that will make the program output 2? + +```java +public class Main { + public static void main(String[] args) { + int x = 5; + int y = ; + System.out.println(x + y); + } +} +``` + +## Challenge 6 + +What is the output of this code.[^fbarticle] + +```java +public class Main { + public static void main(String[] args) { + System.out.println( + 6 / 2 * (1 + 2) + ); + } +} +``` + + +[^fbarticle]: [Now get in a fight with your relatives about it](https://slate.com/technology/2013/03/facebook-math-problem-why-pemdas-doesnt-always-give-a-clear-answer.html) \ No newline at end of file diff --git a/src/integers/comparison.md b/src/integers/comparison.md new file mode 100644 index 0000000..24b6f68 --- /dev/null +++ b/src/integers/comparison.md @@ -0,0 +1,44 @@ +# Comparison + +In addition to comparing for equality with `==`, `int`s can be compared to see if one is bigger than another using +`>`, `<`, `>=`, and `<=`. + +`>` will evaluate to true if the number on the left is greater than the one on the right. + +```java +boolean willBeTrue = 5 > 2; +boolean willBeFalse = 2 > 5; +``` + +`<` will evaluate to true if the number on the right is greater than the one on the left. + +```java +boolean willBeFalse = 5 < 2; +boolean willBeTrue = 2 < 5; +``` + +If you went to public school like I did, you should be used to imagining that the `>` was the jaw of a shark. +Whatever direction the Jaws are facing, thats the one that would need to be bigger for the statement to be true.[^sharks] + +```java +// true if the shark is facing the bigger number +// false otherwise. +boolean result = 9 🦈 5; +``` + +`>=` behaves the same as `>` except `>=` will evaluate to `true` if the numbers are identical, `>` will not. + +```java +boolean willBeTrue = 5 >= 5; +boolean willBeFalse = 5 > 5; +``` + +`<=` has the same relationship to `<` as `>=` does to `>`. + +```java +boolean willBeTrue = 5 <= 5; +boolean willBeFalse = 5 < 5; +``` + +[^sharks]: Shark attacks are far more rare than people think they are. You are not a seal. + diff --git a/src/integers/division.md b/src/integers/division.md new file mode 100644 index 0000000..32fcd5b --- /dev/null +++ b/src/integers/division.md @@ -0,0 +1,26 @@ +# Division + +You can divide any two `int`s using the `/` operator. + +```java +int x = 8; +// y will be 4 +int y = x / 2; + +System.out.println(x); +System.out.println(y); +``` + +Division with integers gives results in only the [quotient](https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-quotient-remainder-theorem) of the result, not the remainder. + +So `5 / 2` does not result in `2.5`, but instead just `2`. + +```java +// 5 / 2 is not 2.5, but instead 2. +int x = 5 / 2; +// 13 / 3 is not 4.3333, but instead 4. +int y = 13 / 2; + +System.out.println(x); +System.out.println(y); +``` diff --git a/src/integers/equality.md b/src/integers/equality.md new file mode 100644 index 0000000..3f5d36e --- /dev/null +++ b/src/integers/equality.md @@ -0,0 +1,22 @@ +# Equality + +Any two `int`s can be inspected to see if their value is equal by using the `==` operator. + +Unlike the previous operators, which all take `int`s and produce `int`s as their result, `==` takes two `int`s +and produces a `boolean` as its result. + +```java +// 1 is never equal to 2 +// this will be false +boolean universeBroken = 1 == 2; +System.out.println(universeBroken); + +boolean loneliestNumber = 1; +boolean otherLonelyNumber = 2; + +// this will be true +boolean bothLonely = loneliestNumber == (otherLonelyNumber - 1); +System.out.println(bothLonely); +``` + +It is very important to remember that a single `=` does an assignment. Two equals signs `==` checks for equality. diff --git a/src/integers/limits.md b/src/integers/limits.md new file mode 100644 index 0000000..7e88ceb --- /dev/null +++ b/src/integers/limits.md @@ -0,0 +1,33 @@ +# Limits + +Unlike in math, where numbers can be arbitrarily big or small, a Java `int` +is "fixed width." + +Say you had a piece of paper that was only big enough to write two numbers on. + +The only numbers you could write in a [base ten system](https://www.khanacademy.org/math/algebra-home/alg-intro-to-algebra/algebra-alternate-number-bases/v/number-systems-introduction) would be those from 0 to 99. You could not write 100 or anything larger. + +A Java `int` is similar except instead of only being able to write 0 to 99 on a piece of paper, a variable that has +the type `int` can represent numbers from -231 to 231 - 1. + + +If you try to directly write out a number that is outside of that range, Java will not let you. + +```java +// This will not run +int tooBig = 999999999999; +``` + +If you do math that should produce a larger number than is representable, the value will "loop around." + +```java +// This is the value of 2^31 - 1 +int atLimit = 2147483647; +// The value will "loop around" to -2^31 +int beyondLimit = atLimit + 1; +// This will output -2147483648 +System.out.println(beyondLimit); +``` + +There are other types which can represent a larger range of integers, as well as types +which do not have any limits, but for now `int` is the only one you will need. diff --git a/src/integers/multiplication.md b/src/integers/multiplication.md new file mode 100644 index 0000000..a92a890 --- /dev/null +++ b/src/integers/multiplication.md @@ -0,0 +1,16 @@ +# Multiplication + +You can multiply any two `int`s using the `*` operator. + +```java +// x will be 15 +int x = 3 * 5; +// y will be 75 +int y = x * 5; +// z will be 1125 +int z = x * y; + +System.out.println(x); +System.out.println(y); +System.out.println(z); +``` diff --git a/src/integers/operator_precedance.md b/src/integers/operator_precedance.md new file mode 100644 index 0000000..ff374e8 --- /dev/null +++ b/src/integers/operator_precedance.md @@ -0,0 +1,40 @@ +# Operator Precedance + +Just like boolean operators, `+`, `-`, `*`, `/`, and `%` have a defined precedence order. + +The order of operations is the same as mathematics. Multiplication and division happen before +addition and subtraction, with the modulo operator being a sort of "funky division." +Parentheses can be used to control this order. + +None of this should be a surprise if you learned [PEMDAS](https://www.khanacademy.org/math/cc-seventh-grade-math/cc-7th-negative-numbers-multiply-and-divide/cc-7th-order-of-operations/v/introduction-to-order-of-operations) in school. + +```java +// Following the order of operations: + +// 2 * 3 happens first +// 6 + 3 * 9 / 2 - 2 + +// 3 * 9 happens next +// 6 + 27 / 2 - 2 + +// 27 / 2 happens next +// because of int division, that gives 13 +// 6 + 13 - 2 + +// 6 + 13 comes next +// 19 - 7 + +// and the final result is 17; +int result = 2 * 3 + 3 * 9 / 2 - 2; +System.out.println(result); +``` + +The `==`, `>`, `<`, `>=`, and `<=` operators play a part here too[^theyalldo]. They all have a lower precedance order than all the math operators, so you can +put them in the middle of any two math expressions. + +```java +// The == check happens last. +boolean areThingsSame = 3 * (4 - 1 + 3) * 4 == 5 * 3 + 1 * 3 * 9; +``` + +[^theyalldo]: Every operator in the language has a defined order of operations with respect to all of the others. I am just showing them to you as they become relevant. \ No newline at end of file diff --git a/src/integers/reassignment.md b/src/integers/reassignment.md new file mode 100644 index 0000000..422f0e1 --- /dev/null +++ b/src/integers/reassignment.md @@ -0,0 +1,23 @@ +# Reassignment + +When the value of a variable is reassigned, the value stored in the variable +before the reassignment can be used to compute the new value. + +This is true for all data types, but it is easiest to demonstrate with numbers. + +```java +int x = 1; +System.out.println(x); + +// x starts as 1, 1 + 1 is 2. +// 2 is the new value of x. +x = x + 1; +System.out.println(x); + +// x is now 2, 2 * 2 * 3 is 12 +// 12 is the new value of x. +x = x * x * 3; +System.out.println(x); +``` + +This property was used in the previous example for the `%` operator, but I think it worth calling attention to even if it is "intuitive". \ No newline at end of file diff --git a/src/integers/remainder.md b/src/integers/remainder.md new file mode 100644 index 0000000..075045a --- /dev/null +++ b/src/integers/remainder.md @@ -0,0 +1,55 @@ +# Remainder + +To get the remainder of the division between two integers you can use the `%` operator. +This is called the "modulo operator." + +```java +int x = 5; +// The remainder of 5 / 2 is 1 +// y will be 1 +int y = x % 2; +// The remainder of 5 / 3 is 2 +// z will be 2 +int z = x % 3; + +System.out.println(x); +System.out.println(y); +System.out.println(z); +``` + +A common use for this is to make numbers "go in a circle." + +For instance, say you wanted to count from 0 up to 3 and then go back to 3. + +```java +int value = 0; +System.out.println(value); + +// the remainder of (0 + 1) divided by 3 is 1 +// value will be 1 +value = (value + 1) % 3; +System.out.println(value); + + +// the remainder of (1 + 1) divided by 3 is 2 +// value will be 2 +value = (value + 1) % 3; +System.out.println(value); + + +// the remainder of (2 + 1) divided by 3 is 0 +// value will again be 0! +value = (value + 1) % 3; +System.out.println(value); + +// the remainder of (0 + 1) divided by 3 is 1 +// value will be 1 +value = (value + 1) % 3; +System.out.println(value); + +// and so on. +``` + +The fact that all the reassignments of value look identical is something that will be useful in tandem +with loops. + diff --git a/src/integers/shorthands_for_reassignment.md b/src/integers/shorthands_for_reassignment.md new file mode 100644 index 0000000..28c0b18 --- /dev/null +++ b/src/integers/shorthands_for_reassignment.md @@ -0,0 +1,61 @@ +# Shorthands for Reassignment + +A very common thing to do is to take the current value of a variable, perform some simple operation like addition on it, and reassign the newly +computed value back into the variable. + +```java +int x = 2; +System.out.println(x); + +x = x * 5 // 10 +System.out.println(x); +``` + +As such, there is a dedicated way to do just that. + +```java +int x = 1; + +// This is the same as +// x = x + 2; +x += 2; + +// This is the same as +// x = x * 4 +x *= 4; + +// This is the same as +// x = x - (x * 5) +x -= (x * 5) + +// This is the same as +// x = x / 6 +x /= 6; + +// This is the same as +// x = x % 3 +x %= 3; + +// Pop quiz! +System.out.println(x); +``` + +Of note is that adding or subtracting exactly 1 is common enough that it +has its own special shorthand. + +```java +int x = 0; +System.out.println(x); + +// Same as +// x = x + 1; +// x += 1; +x++; +System.out.println(x); + +// Same as +// x = x - 1; +// x -= 1; +x--; +System.out.println(x); +``` \ No newline at end of file diff --git a/src/integers/subtraction.md b/src/integers/subtraction.md new file mode 100644 index 0000000..7422a2e --- /dev/null +++ b/src/integers/subtraction.md @@ -0,0 +1,26 @@ +# Subtraction + +You can subtract any two `int`s using the `-` operator. + +```java +int x = 5; +// y will be 4 +int y = x - 1; +// z will be 1 +int z = x - y; + +System.out.println(x); +System.out.println(y); +System.out.println(z); +``` + +Subtracting a negative number does the same thing as addition. + +```java +int x = 5; +// y will be 9 +int y = x - -4; + +System.out.println(x); +System.out.println(y); +``` diff --git a/src/limits.md b/src/limits.md new file mode 100644 index 0000000..e69de29 diff --git a/src/modern/bchallenges.md b/src/modern/bchallenges.md new file mode 100644 index 0000000..9358534 --- /dev/null +++ b/src/modern/bchallenges.md @@ -0,0 +1 @@ +# Challenges diff --git a/src/numbers.md b/src/numbers.md new file mode 100644 index 0000000..b88e3fc --- /dev/null +++ b/src/numbers.md @@ -0,0 +1,3 @@ +# Floating Point Numbers + +While perfectly capable of doing so, most computers wou diff --git a/src/numbers/float_and_double.md b/src/numbers/float_and_double.md new file mode 100644 index 0000000..b7d0d43 --- /dev/null +++ b/src/numbers/float_and_double.md @@ -0,0 +1 @@ +# float and double diff --git a/src/numbers/int_and_long.md b/src/numbers/int_and_long.md new file mode 100644 index 0000000..0d53658 --- /dev/null +++ b/src/numbers/int_and_long.md @@ -0,0 +1 @@ +# int and long diff --git a/src/paths.md b/src/paths.md new file mode 100644 index 0000000..d10dfb0 --- /dev/null +++ b/src/paths.md @@ -0,0 +1,87 @@ +# Welcome Contributors + +Hey folks, + + +This is early days so I am going to use this first page as a message board of sorts to catch everyone up to speed. + +## If you don't want to write + +If you want to get involved the (non-writing) tasks that provide the most value are +- Provide feedback and suggestions +- Offer any domain knowledge on things that the writers don't have (Has anyone used JavaFX for *real*?) +- Start to investigate what it would take to have interactive code snippets or inline quizzes +- Make art for pages that are "content complete." The wackier the better. +- Share reference materials / tutorials you like or don't like. + + +## Examples + +In this section you will find some "indexes" of +books and online tutorials that have been copied down rote for reference. + + + +## Getting Started +The section labeled "Getting Started" is where folks should work on making a guide for getting +set up with the ability to run Java and develop on their machine. + +There is a wide variety of possible setups, so this is strangely tricky! + +In every chapter and guide we can assume that folks have gone through whatever setup instructions are there, but no more. + +This should end with a successful "hello, world" and not have to be "redone" at any point for a learner to follow along. + + +## Modern Java +In this section we are focusing on plotting the "learning path" for someone who wants to learn the "best version" +of Java. + +By "someone" I mean a "student" (loosely defined high school, college, or self learning) who is lacking +solid fundamentals. (due to bad curriculum, bad teacher, bad resources). Thats my current working hypothesis +for a target audience. + +I don't imagine we will be able to make "101" type material that competes with a full curriculum. (Teaching for loops is way too hard!) but we should be able to make something that gives the proper foundation for + + +Generally speaking, we are working on a "tutorial" in the [diataxis](https://diataxis.fr/) sense. As a rule of thumb, if someone has something that is +an "Article" like you would see on Baeldung or geeksforgeeks or Medium, that is not the thing we want. (They are free to publish on those sites or self publish). + +At this point, we think the content should be written as if it is an interaction between a single author - "I" - and +a hypothetical reader. + +In this we should use the latest Java uncompromisingly. There is no "as of Java 8" in this town. It is just Java, whatever the latest release is. Not latest LTS, latest release. I am not going to wait 2 years to talk about pattern matching. + +We should also introduce every topic and concept that we bring up. It should be readable from start to finish with minimal +external context. + +## Legacy Java + +Here is where I am dumping all the content that is "legacy." Stuff like `Vector` and `Hashtable` or `Date`. +This is where we would explain where bad patterns came from and what folks should do instead and why. + +If anyone has a good get/set rant in them, here is the place. (I think). + +I am mostly using it as a place to put things to remind me to go over them, though it could +spiral beyond that mandate. + +## Tour of Java + +If the "modern java" section is where someone would go to learn how to program from near zero, +this is where someone would go if they already knew another language like Python, JavaScript, C#, etc. +and just wanted to get up to speed. + +The order of topics in this section should roughly follow the order they are introduced in the learning +section, though as they are for a different audience they can go more in depth and assume more common +knowledge. + +Of everything this is the strongest canidate for the kind of content that would be a "seperate book." +Seeing as the initial use of this is to link to strugglers, its fine to be colocated atm. + +Think [tour of scala](https://docs.scala-lang.org/tour/tour-of-scala.html) +or [rust by example](https://doc.rust-lang.org/rust-by-example/). + +Once a topic has been covered and fleshed out enough in the tutorial, someone can write a corresponding reference +page in the tour. + + diff --git a/src/prelude.md b/src/prelude.md new file mode 100644 index 0000000..94fd5c9 --- /dev/null +++ b/src/prelude.md @@ -0,0 +1,39 @@ +# Prelude + +First of all, thank you for your time and for your patience. + +I know that when I was in school I never wanted to read a book +and, except for [The Cay](https://en.wikipedia.org/wiki/The_Cay) and [Great Expectations](https://en.wikipedia.org/wiki/Great_Expectations), I +got away without having to. + +So while it is hypocritical for me to ask this of you, +this book is written to be read linearly like a book. You can skip the rest +of this prelude if you aren't interested and just dive in once you've [got your machine set up to code](../getting_started/hello_world.md), but keep that in mind. + +## Background + +Computers are ubiquitous. From the app that just notified me that a raccoon was outside of the basement door +to the software that [DRM locks the equipment that produces our food](https://www.techdirt.com/2021/02/23/john-deere-promised-to-back-off-monopolizing-repair-it-then-ignored-that-promise-completely/), there is no +escaping it. + +Just as there is power in being able to produce software, there is a vulnerability in being unable to. + + +This book is written specifically for those folks that feel like giving up, like they are too stupid to get it, +or like they didn't understand a damn thing their professor has said for the last three months. + +I see you. You are not stupid. This is not your fault, it is theirs. + + +While some of you are truly morons + +While the fair gods of economics will it and while + +Even if your destiny is to do radder shit than type out code all day, + +If you have a teacher that jus + + +This book came out of two things + +1. The observation tha \ No newline at end of file diff --git a/src/rant.md b/src/rant.md new file mode 100644 index 0000000..e69de29 diff --git a/src/scratch.md b/src/scratch.md new file mode 100644 index 0000000..9f4ce90 --- /dev/null +++ b/src/scratch.md @@ -0,0 +1,80 @@ +- Virtual Threads +- Foreign Memory API +- jextract +- Cleaner api + + +- StructuredTaskScope +- Executors + + - [Build Tool] + - [Maven] + - Dependencies + +```xml + + + 4.0.0 + + org.example + project + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + +``` + +- Getting Started +- [Hello, World](./hello_world.md) +- Data Types + - int and long + - Integer Division + - float and double + - Floating Point Numbers + - booleans + - Arrays + - String + - String Literals + - Multiline String Literals +- Expressions +- Conditionals +- Loops +- Exceptions + - Checked Exceptions + - Unchecked Exceptions +- Interfaces + - Sealed Interfaces +- Visibility Modifiers +- Types of Objects +- Records +- Enums +- Inheritance + - Abstract Classes + - Final Classes + - Sealed Classes +- Inversion of Control +- JDBC + - ResultSet + - SQLException +- Service Provider +- jcmd +- jfr +- Custom JFR Events +- Logging +- XML +- UncheckedIOException +- Pattern Matching +- instanceof + +- Maven before hikaricp + - HikariCP for connection pool + + +System.console() over Scanner? \ No newline at end of file diff --git a/src/strings.md b/src/strings.md new file mode 100644 index 0000000..ed218ce --- /dev/null +++ b/src/strings.md @@ -0,0 +1 @@ +# Strings diff --git a/src/strings/escaped_characters.md b/src/strings/escaped_characters.md new file mode 100644 index 0000000..c0d07d2 --- /dev/null +++ b/src/strings/escaped_characters.md @@ -0,0 +1 @@ +# Escaped Characters diff --git a/src/strings/multiline.md b/src/strings/multiline.md new file mode 100644 index 0000000..6ef36c6 --- /dev/null +++ b/src/strings/multiline.md @@ -0,0 +1,25 @@ +# Multiline Strings + +If the text you want to store in a `String` has multiple lines, you can use +three quotation marks to represent it in code. + +```java +String poem = """ + I met a traveller from an antique land, + Who said—“Two vast and trunkless legs of stone + Stand in the desert. . . . Near them, on the sand, + Half sunk a shattered visage lies, whose frown, + And wrinkled lip, and sneer of cold command, + Tell that its sculptor well those passions read + Which yet survive, stamped on these lifeless things, + The hand that mocked them, and the heart that fed; + And on the pedestal, these words appear: + My name is Ozymandias, King of Kings; + Look on my Works, ye Mighty, and despair! + Nothing beside remains. Round the decay + Of that colossal Wreck, boundless and bare + The lone and level sands stretch far away. + """; +``` + +Inside of the this "Multi Line String Literal" you don't need to escape things like quotation marks `"` \ No newline at end of file diff --git a/src/strings/string_theory.md b/src/strings/string_theory.md new file mode 100644 index 0000000..17bef67 --- /dev/null +++ b/src/strings/string_theory.md @@ -0,0 +1 @@ +# String Theory diff --git a/src/tour_of_java.md b/src/tour_of_java.md new file mode 100644 index 0000000..6713844 --- /dev/null +++ b/src/tour_of_java.md @@ -0,0 +1,4 @@ +# Tour of Java + +This section is intended for people who already know how to program in another language +like Python or Javascript. \ No newline at end of file diff --git a/src/types.md b/src/types.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/types.md @@ -0,0 +1 @@ + diff --git a/src/types/numbers.md b/src/types/numbers.md new file mode 100644 index 0000000..69d95aa --- /dev/null +++ b/src/types/numbers.md @@ -0,0 +1,19 @@ +# Numbers + +Strings + +In the code so far, I've referred to a type called a "`String`". +## Types + +## Values + +## boolean + + +## int and long + +Twos compliment + +## float and double + +## Reference Types \ No newline at end of file diff --git a/src/types/strings.md b/src/types/strings.md new file mode 100644 index 0000000..ed218ce --- /dev/null +++ b/src/types/strings.md @@ -0,0 +1 @@ +# Strings diff --git a/src/variables.md b/src/variables.md new file mode 100644 index 0000000..6bad094 --- /dev/null +++ b/src/variables.md @@ -0,0 +1,31 @@ +# Local Variables + +Mechanically, the next thing to cover is "variables". + +```java +public class Main { + public static void main(String[] args) { + String boss = "Jaqueline"; + System.out.println(boss); + } +} +``` + +A variable declaration has three components - the "type", the "name", and the "initial value". + +```java + String boss = "Jaqueline"; +// type name initial value +``` + +In this case, we are declaring a variable called "boss" and assigning it the initial value +of `"Jaqueline"`. Its "type" is "String", which I'll explain in more detail a bit ahead. + +After you declare a variable and assign it a value, the "name" refers to the value on the right +hand side and you can use that name instead of the value. + +```java +// Does the same thing as System.out.println("Jaqueline"); +String boss = "Jaqueline"; +System.out.println(boss); +``` diff --git a/src/variables/challenges.md b/src/variables/challenges.md new file mode 100644 index 0000000..83a717c --- /dev/null +++ b/src/variables/challenges.md @@ -0,0 +1,111 @@ +# Challenges + +Remember the rules for this are +* Try to use only the information given up to this point in this book. +* Try not to give up until you've given it a solid attempt + +## Challenge 1. + +What will this program output when run? Write down your guess and then try running it. + +```java +public class Main { + public static void main(String[] args) { + String mascot = "The Noid"; + System.out.println(mascot); + mascot = "Pizza the Hut"; + System.out.println(mascot); + mascot = "Little Caesar"; + System.out.println(mascot); + } +} +``` + +## Challenge 2. + +Why won't this code run? Make it run by only changing one line. + +```java +public class Main { + public static void main(String[] args) { + String fruit; + fruit = "apple" + + System.out.println(fruit); + + final String vegtable = "carrot"; + + System.out.println(fruit); + System.out.println(vegtable); + + fruit = "orange"; + vegetable = "celery"; + + System.out.println(fruit); + System.out.println(vegtable); + } +} +``` + +## Challenge 3 + +What is the output of this code? + +```java +public class Main { + public static void main(String[] args) { + String a = "A"; + String b = "B"; + + b = a; + a = b; + b = a; + a = b; + + System.out.println(a); + System.out.println(b); + } +} +``` + +## Challenge 4 + +Only adding lines in the middle, make it so that the output of the program is + +``` +B +A +``` + +```java +public class Main { + public static void main(String[] args) { + String a = "A"; + String b = "B"; + // Don't touch above this + + // You can add code here + + // Don't touch below this + System.out.println(a); + System.out.println(b); + } +} +``` + + +## Challenge 5 + +Some of the variables in this program are named "wrong." Fix them. + +```java +public class Main { + public static void main(String[] args) { + String apple = "red"; + String clown_car = "polka dot"; + String SeriousCar = "black"; + String FASTRunner = "bolt"; + String slowRunner = "tortoise"; + } +} +``` \ No newline at end of file diff --git a/src/variables/delayed_assignment.md b/src/variables/delayed_assignment.md new file mode 100644 index 0000000..9da82e2 --- /dev/null +++ b/src/variables/delayed_assignment.md @@ -0,0 +1,45 @@ +# Delayed Assignment + +The declaration of a variable and the assignment of its initial value can +be done separately. + +```java +public class Main { + public static void main(String[] args) { + String contestWinner; + contestWinner = "Boaty McBoatface"; + + System.out.println(contestWinner); + } +} +``` + +In which case the "variable declaration" will only have the "type" and "name" components. + +```java + String contestWinner; +// type name +``` + +And the "initial assignment" will look identical to a "re-assignment". + +```java + contestWinner = "Boaty McBoatface"; +// name initial value +``` + +Before an initial value is assigned to a variable, it is not allowed to be used.[^whydelay] + +```java +public class Main { + public static void main(String[] args) { + String contestWinner; + // This will not run, since Java knows that + // you never gave contestWinner an initial value. + System.out.println(contestWinner); + } +} +``` + +[^whydelay]: There is no direct use for separating declaration and initial assignment at this point, +but [it's a surprise tool that will help us later](https://knowyourmeme.com/memes/its-a-surprise-tool-that-will-help-us-later). diff --git a/src/variables/final_variables.md b/src/variables/final_variables.md new file mode 100644 index 0000000..576bf7a --- /dev/null +++ b/src/variables/final_variables.md @@ -0,0 +1,71 @@ +# Final Variables + + +There is an optional extra part to a variable declaration where you can +mark a variable as "final", meaning its value can never be reassigned. + +```java +public class Main { + public static void main(String[] args) { + final String coolestChef = "Anthony Bourdain"; + System.out.println(coolestChef); + } +} +``` + +If you try to reassign a final variable, Java will not accept your program. + +```java +public class Main { + public static void main(String[] args) { + final String coolestChef = "Anthony Bourdain"; + System.out.println(coolestChef); + + // I'm sorry, but no. Cool guy, but no. + coolestChef = "Gordan Ramsey"; + System.out.println(coolestChef); + } +} +``` + +This is useful if you have a lot of lines of code and want the mental +comfort of knowing you couldn't have reassigned that variable at any +point between its declaration and its use. + +```java +final String genie = "Robin Williams"; +// imagine +// 100s of lines +// of code +// and it is +// hard to +// read all of it +// at a glance +// ...... +// ...... +// You can still be sure "genie" +// has the value of "Robin Williams" +System.out.println(genie); +``` + +Variables whose assignment is delayed can also be marked final. + +```java +final String mario; +mario = "Charles Martinet"; +``` + +The restriction is the same - after the initial assignment, the variable +cannot be reassigned. + +```java +final String mario; +// An initial assignment is fine +mario = "Charles Martinet"; +// But you cannot reassign it afterwards +mario = "Chris Pratt"; +``` + +The downside to this, of course, is more visual noise. If a variable is only +"alive" for a small part of the code, then adding `final` might make it harder +to read the code, not easier. \ No newline at end of file diff --git a/src/variables/inferred_types.md b/src/variables/inferred_types.md new file mode 100644 index 0000000..f85d15c --- /dev/null +++ b/src/variables/inferred_types.md @@ -0,0 +1,40 @@ +# Inferred Types + +In many cases, Java is smart enough to know what the type of a variable should be +based on what it is initially assigned to. +In these cases, you can write `var` in place of the type and let java "infer" what it should +be. + + +```java +// Since what is to the right hand side +// of the = is in quotes, Java knows that +// it is a String. +var theDude = "Lebowski"; +System.out.println(theDude); +``` + +You cannot use `var` with variables whose assignment is delayed. + +```java +// With just the line "var theDude;", +// Java doesn't know enough to infer the type +var theDude; +theDude = "Lebowski"; +System.out.println(theDude); +``` + +You can use `var` with `final` to make a variable whose type is inferred +and cannot be reassigned. + +```java +final var theDude = "Lebowski"; +System.out.println(theDude); +``` + +Important to note that even if Java is smart enough to automatically know the type, +you might not be yet. There is no shame in writing out the type explicitly. + +```java +String theDude = "lebowski"; +``` \ No newline at end of file diff --git a/src/variables/naming.md b/src/variables/naming.md new file mode 100644 index 0000000..0af2ef9 --- /dev/null +++ b/src/variables/naming.md @@ -0,0 +1,20 @@ +# Naming + +It is a social convention[^perry] that local variables be named `likeThis`. + +That is if their name is one word, that word should be in lowercase. + +```java +String apple = "Red Delicious"; +``` + +If it is multiple words, the first word should be lowercase and the others should start with a capital letter. + +This convention is called `camelCase` because the capitals looks like the humps on a Camels back[^camel]. + +Just like proper formatting, sticking to this style will increase your chances of someone online being able to +help you with your code. + +[^perry]: [Trapped! By societal convention!](https://youtu.be/CnOWLuN37D8?t=64) + +[^camel]: And because doing this will increase your body's ability to retain water by ~34% \ No newline at end of file diff --git a/src/variables/reassignment.md b/src/variables/reassignment.md new file mode 100644 index 0000000..b59ae96 --- /dev/null +++ b/src/variables/reassignment.md @@ -0,0 +1,37 @@ +# Reassignment + +After a variable is declared and assigned an initial value, that value can be later reassigned. + +```java +public class Main { + public static void main(String[] args) { + String boss = "Jaqueline"; + System.out.println(boss); + boss = "Chelsea" + System.out.println(boss); + } +} +``` + +Reassignments just involve the name and the new value. The type should not be redeclared. + +```java + boss = "Chelsea"; +// name new value +``` + +After a variable is reassigned, the value associated with the name will reflect the new value from that point in the program onwards . + +```java +public class Main { + public static void main(String[] args) { + String boss = "Jaqueline"; + // This will output "Jaqueline" + System.out.println(boss); + boss = "Chelsea" + // But this will output "Chelsea" + System.out.println(boss); + } +} +``` + diff --git a/src/variables/types.md b/src/variables/types.md new file mode 100644 index 0000000..3002e12 --- /dev/null +++ b/src/variables/types.md @@ -0,0 +1,33 @@ +# Types + +When a variable is declared, it is given a type. + +```java +String color = "green"; +``` + +In this case, the variable `color` is declared to have the type `String`. +After this declaration, `color` cannot be assigned to a value that is not a `String`. + +```java +// A number is not a String! +String color = 8; +``` + +This applies to all situations where a variable might be given a value, +including delayed assignment and reassignment. + +One mental model is that types are like shapes. If the type of something is a circle, you can only put circles into it. + +```java +◯ thing = ◯; +``` + +You cannot put square pegs in that round hole. + +```java +// If Java actually functioned in terms of shapes, this +// would not work since a Square is not the same "type" +// of thing as a Circle. +◯ thing = ▢; +``` \ No newline at end of file diff --git a/theme/index.hbs b/theme/index.hbs new file mode 100644 index 0000000..147eb9a --- /dev/null +++ b/theme/index.hbs @@ -0,0 +1,313 @@ + + + + + + {{ title }} + {{#if is_print }} + + {{/if}} + {{#if base_url}} + + {{/if}} + + + + {{> head}} + + + + + + {{#if favicon_svg}} + + {{/if}} + {{#if favicon_png}} + + {{/if}} + + + + {{#if print_enable}} + + {{/if}} + + + + {{#if copy_fonts}} + + {{/if}} + + + + + + + + {{#each additional_css}} + + {{/each}} + + {{#if mathjax_support}} + + + {{/if}} + + + + + + + + + + + + + + + + +
+ +
+ {{> header}} + + + + {{#if search_enabled}} + + {{/if}} + + + + +
+
+ {{{ content }}} +
+ + +
+
+ + + +
+ + {{#if live_reload_endpoint}} + + + {{/if}} + + {{#if google_analytics}} + + + {{/if}} + + {{#if playground_line_numbers}} + + {{/if}} + + {{#if playground_copyable}} + + {{/if}} + + {{#if playground_js}} + + + + + + {{/if}} + + {{#if search_js}} + + + + {{/if}} + + + + + + + {{#each additional_js}} + + {{/each}} + + {{#if is_print}} + {{#if mathjax_support}} + + {{else}} + + {{/if}} + {{/if}} + + + From c2cd8356869cf4f398de0b357d0cd29ac3b0a189 Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Wed, 11 Jan 2023 07:48:47 -0500 Subject: [PATCH 2/2] Remove theme.rbs --- theme/index.hbs | 313 ------------------------------------------------ 1 file changed, 313 deletions(-) delete mode 100644 theme/index.hbs diff --git a/theme/index.hbs b/theme/index.hbs deleted file mode 100644 index 147eb9a..0000000 --- a/theme/index.hbs +++ /dev/null @@ -1,313 +0,0 @@ - - - - - - {{ title }} - {{#if is_print }} - - {{/if}} - {{#if base_url}} - - {{/if}} - - - - {{> head}} - - - - - - {{#if favicon_svg}} - - {{/if}} - {{#if favicon_png}} - - {{/if}} - - - - {{#if print_enable}} - - {{/if}} - - - - {{#if copy_fonts}} - - {{/if}} - - - - - - - - {{#each additional_css}} - - {{/each}} - - {{#if mathjax_support}} - - - {{/if}} - - - - - - - - - - - - - - - - -
- -
- {{> header}} - - - - {{#if search_enabled}} - - {{/if}} - - - - -
-
- {{{ content }}} -
- - -
-
- - - -
- - {{#if live_reload_endpoint}} - - - {{/if}} - - {{#if google_analytics}} - - - {{/if}} - - {{#if playground_line_numbers}} - - {{/if}} - - {{#if playground_copyable}} - - {{/if}} - - {{#if playground_js}} - - - - - - {{/if}} - - {{#if search_js}} - - - - {{/if}} - - - - - - - {{#each additional_js}} - - {{/each}} - - {{#if is_print}} - {{#if mathjax_support}} - - {{else}} - - {{/if}} - {{/if}} - - -