diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 1ec6415..b230d3a 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -389,6 +389,7 @@ Gui stuff - [Field Initialization](./classes/field_initialization.md) - [Field Access](./classes/field_access.md) - [Field Default Values](./classes/field_default_values.md) + - [Multiple Instances](./classes/multiple_instances.md) - [Aliasing](./classes/aliasing.md) - [Return Multiple Values](./classes/return_multiple_values.md) - [Challenges](./classes/challenges.md) diff --git a/src/arrays/set_individual_elements.md b/src/arrays/set_individual_elements.md index 5209c80..e518484 100644 --- a/src/arrays/set_individual_elements.md +++ b/src/arrays/set_individual_elements.md @@ -50,7 +50,7 @@ IO.println( + response[4] ); -response[2] = "is"; +response[index] = "is"; IO.println( response[0] + " " diff --git a/src/classes/multiple_instances.md b/src/classes/multiple_instances.md new file mode 100644 index 0000000..174c783 --- /dev/null +++ b/src/classes/multiple_instances.md @@ -0,0 +1,26 @@ +# Multiple Instances + +If you make multiple instances of a class with `new` +those classes will each have their own values for +fields. + +```java +class Muppet { + String name; +} + +void main() { + Muppet kermit = new Muppet(); + kermit.name = "Kermit The Frog"; + + Muppet animal = new Muppet(); + animal.name = "animal"; + + // kermit and animal are distinct muppets + // and thus each have their own name. + IO.println(kermit.name); + IO.println(animal.name); +} +``` + +Each instance has what we would call its own "identity." \ No newline at end of file diff --git a/src/conclusion/what_now.md b/src/conclusion/what_now.md index 43447f7..8b17eb3 100644 --- a/src/conclusion/what_now.md +++ b/src/conclusion/what_now.md @@ -117,7 +117,7 @@ to in order to render websites. There are a lot of tools for this in Java. A lot a lot. I would recommend starting with the one that comes built-in: the `jdk.httpserver` module. -[Docs for `jdk.httpserver` here](https://docs.oracle.com/en/java/javase/25/docs/api/jdk.httpserver/module-summary.html) +[Docs for `jdk.httpserver` here](https://javadoc.mccue.dev/api/jdk.httpserver/module-summary.html) Then you will need to learn about SQL databases and how to query from/insert into them from Java. @@ -147,7 +147,7 @@ two paths. Path #1 is to learn Java Swing. This is an old crusty GUI framework that is kinda difficult to use but has the pro of coming with Java and being able to run on every potato in existence. -[Docs for `java.desktop` (Swing) here](https://docs.oracle.com/en/java/javase/25/docs/api/java.desktop/module-summary.html) +[Docs for `java.desktop` (Swing) here](https://javadoc.mccue.dev/api/java.desktop/module-summary.html) Path #2 is the learn JavaFX. By all accounts JavaFX is better software than Swing, but it was cursed by coming out at a point in history where desktop apps were no longer big business to develop. It diff --git a/src/documentation/challenges.md b/src/documentation/challenges.md index 900f296..f413b40 100644 --- a/src/documentation/challenges.md +++ b/src/documentation/challenges.md @@ -19,6 +19,6 @@ Fix these by documenting that code enough that `javadoc` no longer gives you war ## Challenge 3. -Read the [documentation for java.util.StringJoiner](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/StringJoiner.html). +Read the [documentation for java.util.StringJoiner](https://javadoc.mccue.dev/api/java.base/java/util/StringJoiner.html). If you can, alter one of your projects to use it. \ No newline at end of file diff --git a/src/documentation/javadoc.md b/src/documentation/javadoc.md index 53b511f..cf2c0f5 100644 --- a/src/documentation/javadoc.md +++ b/src/documentation/javadoc.md @@ -17,6 +17,6 @@ javadoc \ This will produce a directory full of HTML files that contain all the documentation from the specified modules. -This is what is used to make [the official Java documenation](https://docs.oracle.com/en/java/javase/25/docs/api/index.html) as well as at least part of the documentation for most Java libraries. +This is what is used to make [the official Java documenation](https://javadoc.mccue.dev/api/index.html) as well as at least part of the documentation for most Java libraries. [^theme]: As is a theme with command-line tools \ No newline at end of file diff --git a/src/first_steps.md b/src/first_steps.md index bbc9f8a..b5612c4 100644 --- a/src/first_steps.md +++ b/src/first_steps.md @@ -62,3 +62,6 @@ You should get in the habit of, whenever you see some bit of code, trying to phy tweak it, and play with it. So try playing around with this program. If you actively engage then you will retain information better. + +A lot of the code samples in the book are runnable in your browser. Hover +your mouse on the one above. You should see a run button in the top right. \ No newline at end of file diff --git a/src/hash_maps/challenges.md b/src/hash_maps/challenges.md index 684bba4..563c2fa 100644 --- a/src/hash_maps/challenges.md +++ b/src/hash_maps/challenges.md @@ -90,7 +90,7 @@ class Main { ``` If you feel like this is hard to do with just `.put` and `.get`, you can -check for [other methods on HashMap that can help you](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/HashMap.html). +check for [other methods on HashMap that can help you](https://javadoc.mccue.dev/api/java.base/java/util/HashMap.html). ## Challenge 3. diff --git a/src/interfaces/cat_dog.mp4 b/src/interfaces/cat_dog.mp4 new file mode 100644 index 0000000..d5f6c53 Binary files /dev/null and b/src/interfaces/cat_dog.mp4 differ diff --git a/src/interfaces/multiple_implementations.md b/src/interfaces/multiple_implementations.md index 87f2f2f..d6c0784 100644 --- a/src/interfaces/multiple_implementations.md +++ b/src/interfaces/multiple_implementations.md @@ -50,3 +50,8 @@ void main() { barkAndFetch(new Cat()); } ``` + +
+ Think a cat won't behave like a dog? + +
diff --git a/src/lambdas/method_references.md b/src/lambdas/method_references.md index b33db4c..ae472e6 100644 --- a/src/lambdas/method_references.md +++ b/src/lambdas/method_references.md @@ -34,6 +34,10 @@ interface Drummer { void drum(); } +interface Musician { + void play(); +} + class Main { void doDrum() { IO.println("ratatatatat"); @@ -46,7 +50,8 @@ class Main { Drummer drummer = this::doDrum; drummer.drum(); - Runnable run = drummer::drum; + Musician musician = drummer::drum; + musician.play(); } } ``` diff --git a/src/loops/continue.md b/src/loops/continue.md index 8bb7a2a..87de0c9 100644 --- a/src/loops/continue.md +++ b/src/loops/continue.md @@ -11,6 +11,10 @@ int x = 5; while (x > 0) { if (x == 4) { x--; // Make sure the loop continues to 3 + // When this line is reached it will skip + // all the lines after this in the function + // and immediately go back to the top of the + // loop continue; } IO.println(x + " is a good number"); diff --git a/src/loops/endless_loops.md b/src/loops/endless_loops.md index 75eb3f8..b2ef9a0 100644 --- a/src/loops/endless_loops.md +++ b/src/loops/endless_loops.md @@ -1,6 +1,6 @@ # Endless Loops -If a while loop will never end, we call that an endless loop. +If a while loop will never end, we call that an endless loop[^hot]. This can happen if the condition is a constant like `while (true)` @@ -26,3 +26,5 @@ while (x != 1) { Many games should never really "finish" so at the very start of that sort of program it is not uncommon to see a `while (true)`. + +[^hot]: If you run an endless loop that does nothing but loop your computer might start overheating. This is a natural part of the process and ultimately healthy for your CPU fans. diff --git a/src/loops_ii/drawing_right_triangles.md b/src/loops_ii/drawing_right_triangles.md index 2cc4c17..514e39d 100644 --- a/src/loops_ii/drawing_right_triangles.md +++ b/src/loops_ii/drawing_right_triangles.md @@ -1,6 +1,6 @@ # Drawing Right Triangles -One of the more fun things to do with `for` loops[^quiz] is to use them to print out shapes. +One of the more fun things to do with `for` loops is to use them to print out shapes. Say you wanted to draw this right triangle. diff --git a/src/loops_iii/iterable_and_iterator.md b/src/loops_iii/iterable_and_iterator.md index d7d66d0..ca44383 100644 --- a/src/loops_iii/iterable_and_iterator.md +++ b/src/loops_iii/iterable_and_iterator.md @@ -3,7 +3,7 @@ For things that are not arrays, a for-each loops are built on top of two interfaces: `java.lang.Iterable` and `java.lang.Iterator`. -The [`Iterator`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Iterator.html) interface defines two methods: `hasNext` and `next`[^remove]. Iterators let you box up the logic of +The [`Iterator`](https://javadoc.mccue.dev/api/java.base/java/util/Iterator.html) interface defines two methods: `hasNext` and `next`[^remove]. Iterators let you box up the logic of how to loop over something. ```java,no_run @@ -17,7 +17,7 @@ public interface Iterator { } ``` -[`Iterable`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Iterable.html) then +[`Iterable`](https://javadoc.mccue.dev/api/java.base/java/util/Iterable.html) then just has one method which gives you an `Iterator`. ```java,no_run diff --git a/src/methods/void.md b/src/methods/void.md index 3cfc088..9453c60 100644 --- a/src/methods/void.md +++ b/src/methods/void.md @@ -15,6 +15,8 @@ int views() { // Doesn't return any value. void talkAboutVideo() { + // Printing something to the screen is different + // than returning a value. IO.println(title() + " only has " + views() + " views."); } diff --git a/src/modules/exports.md b/src/modules/exports.md index 02cfbc8..4643322 100644 --- a/src/modules/exports.md +++ b/src/modules/exports.md @@ -3,7 +3,7 @@ Inside your `module-info.java` file you declare which packages are "exported" from that module. -```java +```java,no_run module reality { exports earth; exports sea; diff --git a/src/modules/java.base.md b/src/modules/java.base.md index bcfe2cf..57b439a 100644 --- a/src/modules/java.base.md +++ b/src/modules/java.base.md @@ -24,7 +24,7 @@ So if a file has the following ```java void main() { - var names = ArrayList(); + var names = new ArrayList(); names.add("Him"); names.add("Jim"); names.add("Bim"); @@ -39,7 +39,7 @@ import module java.base; class Main { void main() { - var names = ArrayList(); + var names = new ArrayList(); names.add("Him"); names.add("Jim"); names.add("Bim"); diff --git a/src/modules/purpose.md b/src/modules/purpose.md index 1b0081f..fa8faf8 100644 --- a/src/modules/purpose.md +++ b/src/modules/purpose.md @@ -9,14 +9,14 @@ want groupings of a grouping? The answer is that[^more] when you share code with other people you will often want to have that code contain multiple packages. If you couldn't "hide" packages of code from the people using -the code you are sharing it would be a bummer. +the code you[^andby] are sharing it would be a bummer. Just as private fields help you encapsulate state in a class and package-private classes help you encapsulate whole classes, unexported packages are your mechanism for encapsulating whole package. -[^andby]: And by "you" I mean you and whoever else is directly working with you. - [^more]: There are more answers, but they mostly involve tales of Java's past -and mistakes that were made there. \ No newline at end of file +and mistakes that were made there. + +[^andby]: And by "you" I mean you and whoever else is directly working with you. \ No newline at end of file diff --git a/src/packages/challenges.md b/src/packages/challenges.md index cd24633..058a51d 100644 --- a/src/packages/challenges.md +++ b/src/packages/challenges.md @@ -28,7 +28,7 @@ This will require marking the classes and many of the methods within `public`. ## Challenge 3. -Read through [the list of packages that come with Java](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/module-summary.html). You don't need to understand everything you are looking at; just +Read through [the list of packages that come with Java](https://javadoc.mccue.dev/api/java.base/module-summary.html). You don't need to understand everything you are looking at; just browse and try to find at least one class that interests you. Import that class and try to use it. diff --git a/src/reflection/challenges.md b/src/reflection/challenges.md index b6dc1bf..dca0a26 100644 --- a/src/reflection/challenges.md +++ b/src/reflection/challenges.md @@ -12,7 +12,7 @@ as input and returns if the name of the underlying class for that `Object` starts with a vowel. You might need to consult the documentation -for [Class](https://docs.oracle.com/en/java/javase/25/docs/api/index.html). +for [Class](https://javadoc.mccue.dev/api/index.html). ```java,editable class Apple {} diff --git a/src/return_values/void.md b/src/return_values/void.md index ba16473..d38e716 100644 --- a/src/return_values/void.md +++ b/src/return_values/void.md @@ -15,6 +15,8 @@ int views() { // Doesn't return any value. void talkAboutVideo() { + // Printing something to the screen is different + // than returning a value. IO.println(title() + " only has " + views() + " views."); } diff --git a/src/standard_input/other_types.md b/src/standard_input/other_types.md index cabdbd2..877d553 100644 --- a/src/standard_input/other_types.md +++ b/src/standard_input/other_types.md @@ -21,4 +21,16 @@ void main() { char grade = Character.parseCharacter(gradeString); IO.println("You have a " + grade + " in the class."); } +``` + +For characters specifically you can get the first character of a `String` with `.charAt(0)`, +but you might also want to check that said `String` is only one character long. Each type +will be special. + +```java,no_run +void main() { + String gradeString = IO.readln("What is your letter grade? "); + char grade = gradeString.charAt(0); + IO.println("You have a " + grade + " in the class."); +} ``` \ No newline at end of file diff --git a/src/static_methods/math.md b/src/static_methods/math.md index c0fab92..98f7f17 100644 --- a/src/static_methods/math.md +++ b/src/static_methods/math.md @@ -17,4 +17,4 @@ an instance of a class to call them would be silly. The `Math` class that comes with Java has methods that work in this way. `Math.max(a, b)` is `static` and therefore usable everywhere you want the maximum of two numbers.[^more] -[^more]: There are way more on there. [Take a look](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Math.html). \ No newline at end of file +[^more]: There are way more on there. [Take a look](https://javadoc.mccue.dev/api/java.base/java/lang/Math.html). \ No newline at end of file diff --git a/src/streams/challenges.md b/src/streams/challenges.md index bf33745..676abce 100644 --- a/src/streams/challenges.md +++ b/src/streams/challenges.md @@ -46,7 +46,7 @@ class Main { ## Challenge 3. -Read the documentation on [`Collector`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Collector.html) and [`Collectors`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/stream/Collector.html). +Read the documentation on [`Collector`](https://javadoc.mccue.dev/api/java.base/java/util/stream/Collector.html) and [`Collectors`](https://javadoc.mccue.dev/api/java.base/java/util/stream/Collector.html). Make an implementation of `Collector` that can collect elements into `MySpecialList`. diff --git a/src/streams/map.md b/src/streams/map.md index e9b58cd..b11d0c8 100644 --- a/src/streams/map.md +++ b/src/streams/map.md @@ -13,6 +13,16 @@ Stream numberStream = numbers.stream() .map(Integer::parseInt); // 1, 2, 3 ``` +You can also call `.map` multiple times to apply multiple transformations. + +```java,no_run +var numbers = List.of("1", "2", "3"); + +Stream numberStream = numbers.stream() + .map(Integer::parseInt) // 1, 2, 3 + .map(x -> x * 2); // 2, 4, 6 +``` + [^metaphor]: In the real life stream metaphor, this is akin to rocks getting polished by sand as they flow. diff --git a/src/time/date.md b/src/time/date.md index d04bc26..7eec026 100644 --- a/src/time/date.md +++ b/src/time/date.md @@ -56,4 +56,4 @@ class Main { To be clear though, `Date` has problems. We aren't ready to explain all of them yet. Just treat `Date` as haunted, as in by ghosts, and use the `java.time` alternatives when you can. -[^gmt]: You will notice that when we print out the date we get GMT. GMT is basically the same as UTC, though [the documentation for `Date`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Date.html) explains the difference. \ No newline at end of file +[^gmt]: You will notice that when we print out the date we get GMT. GMT is basically the same as UTC, though [the documentation for `Date`](https://javadoc.mccue.dev/api/java.base/java/util/Date.html) explains the difference. \ No newline at end of file