Skip to content

Commit 9055d3b

Browse files
committed
regex start
1 parent b18750c commit 9055d3b

File tree

10 files changed

+291
-4
lines changed

10 files changed

+291
-4
lines changed

book.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
[book]
22
title = "Modern Java"
33
description = "Book teaching how to write modern and effective Java."
4-
authors = ["Ethan McCue", "Together Java", "Contributions from the Java Community"]
4+
authors = [
5+
"Ethan McCue",
6+
"Together Java",
7+
"Contributions from the Java Community",
8+
]
59
language = "en"
610

711
[output.html]
@@ -10,10 +14,11 @@ edit-url-template = "https://github.com/Together-Java/ModernJava/edit/develop/{p
1014
mathjax-support = true
1115
additional-css = ["ferris.css"]
1216
additional-js = ["ferris.js"]
17+
heading-split-level = 2
1318

1419
[output.html.fold]
15-
enable = true # whether or not to enable section folding
16-
level = 0 # the depth to start folding
20+
enable = false # whether or not to enable section folding
21+
level = 0 # the depth to start folding
1722

1823
[output.html.playground]
1924
editable = true

src/SUMMARY.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,10 +953,34 @@ TODO: Wait for hermetic java
953953
- [Collectors](./streams/collectors.md)
954954
- [Purpose](./streams/purpose.md)
955955
- [Challenges](./streams/challenges.md)
956-
<!-- - [Regular Expressions 🚧]()
956+
- [Regular Expressions 🚧](./regular_expressions.md)
957+
- [Theoretical Basis](./regular_expressions/theoretical_basis.md)
958+
- [Pattern](./regular_expressions/pattern.md)
959+
- [Exact Matches](./regular_expressions/exact_matches.md)
960+
- [Character Classes](./regular_expressions/character_classes.md)
961+
- [Cardinality Modifiers]()
962+
- [Groups]()
963+
- [Matcher]()
964+
- [Pathological Cases]()
965+
- [Power]()
966+
967+
# Conclusion
968+
969+
- [What Now?](./conclusion/what_now.md)
970+
971+
972+
<!--
957973
- [Strings III 🚧]()
958974
975+
Streams II
976+
- Infinite Streams
977+
- takeWhile
978+
- limit
979+
- dropWhile
959980
981+
Streams III
982+
- Gatherers
983+
960984
# Code Structure IX
961985
962986
- [Sealed Interfaces 🚧]()

src/conclusion/what_now.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# What Now?
2+
3+
If you have made it this far:
4+
5+
1. Congratulations, you have now learned Java!
6+
2. You are probably wondering what you should do next.
7+
8+
## Go Deeper
9+
10+
No matter how much I write there is no chance I will have covered all of the Java
11+
language nor all of what you might want to know to write software in Java.
12+
13+
With what you've learned so far you should have a solid enough foundation
14+
to go off and learn from other sources. I'll try and paint a picture of the landscape for you
15+
before you run off though.
16+
17+
### Build Tools
18+
19+
First you probably are going to want to learn a build tool.
20+
I haven't covered how to get dependencies yet and that is on purpose.
21+
22+
In the Java world - due to the ability to launch a program with `java src/Main.java` being
23+
a pretty recent development - all the tools that help you automatically download
24+
libraries written by other people are married with tools that "build" - i.e. run `javac`, `jar`, etc.
25+
for you - your code.
26+
27+
There are two major build tools (meaning widely used) in the Java world (Maven and Gradle)
28+
as well as many more niche ones (bld, mill, etc.).
29+
30+
If you want a gentle introduction to this world you can start with bld, though be aware this
31+
will be a road less travelled.
32+
33+
([bld tutorial here](https://github.com/rife2/bld/wiki))
34+
35+
If you want to learn the one that will probably be the most useful to you
36+
in a professional setting you should learn Maven first.
37+
38+
(["Maven By Example" book here](https://books.sonatype.com/mvnex-book/reference/index.html))
39+
40+
If you are angling to get into Android development you should learn Gradle.
41+
Hop ahead and check out the resources for Kotlin too because Kotlin is the
42+
language you will use for Gradle build scripts.
43+
44+
([Gradle Documentation Here](https://docs.gradle.org/current/userguide/userguide.html))
45+
46+
### Minecraft
47+
48+
If your age begins with the number `1` you are either near death or statistically
49+
very interested in Minecraft.
50+
51+
A lot of people who learn Java do so in order to be able to write Minecraft
52+
mods or plugins for Minecraft servers, it is normal.
53+
54+
Just a few words of caution:
55+
56+
* The world of Minecraft development can be deeply exploitative. If you are not
57+
a full adult please do not try to "work" for anyone. Be careful. Talk to a
58+
parent or an adult you trust when people online seem to want something from you.
59+
* The kinds of code you write to make mods work will be pretty different than the
60+
kind of code you would write for most other kinds of software. This is partially
61+
because of what modding is (adjusting software whose evolution you do not control)
62+
and partially because of peculiarities around Minecraft in particular.
63+
64+
Now: there are two fa
65+
66+
### Websites
67+
68+
Making websites is a profitable career path. At least it is at the time of writing.
69+
There are a few essential things you will need to learn about to get started with that
70+
path.
71+
72+
The first is how to make an HTTP Server. HTTP Servers are what web browsers talk
73+
to in order to render websites.
74+
75+
There are a lot of tools for this in Java. A lot a lot. I would recommend
76+
starting with the one that comes built-in: the `jdk.httpserver` module.
77+
78+
[Docs for `jdk.httpserver` here](https://docs.oracle.com/en/java/javase/24/docs/api/jdk.httpserver/module-summary.html)
79+
80+
Then you will need to learn about SQL databases and how to query from/insert into them from Java.
81+
82+
A good start for that is SQLite - it is a database that runs self-contained in a single file.
83+
84+
[SQLite tutorial here](https://www.sqlitetutorial.net/)
85+
86+
After that - or as part of that - you should learn about JDBC. This is the way you interact with
87+
a database from Java.
88+
89+
[I have a primer for that here](https://mccue.dev/pages/1-17-24-java-sql)
90+
91+
Then from there you should learn about Dependency Injection
92+
93+
[Good video on that here](https://www.youtube.com/watch?v=J1f5b4vcxCQ)
94+
95+
And finally you can dive into the world of Spring - which is likely the
96+
most employable one of the many HTTP server options out there.
97+
98+
[Spring Academy Courses here - the basic ones are free](https://spring.academy/)
99+
100+
### Desktop Applications
101+
102+
If you want to learn how to make desktop applications in Java you have basically
103+
three paths.
104+
105+
Path #1 is to learn Java Swing. This is an old crusty GUI framework that is kinda difficult to use
106+
but has the pro of coming with Java and being able to run on every potato in existence.
107+
108+
[Docs for `java.desktop` here](https://docs.oracle.com/en/java/javase/24/docs/api/jdk.httpserver/module-summary.html)
109+
110+
Path #2 is the learn JavaFX. By all accounts JavaFX is better software than Swing, but it was cursed
111+
by coming out at a point in history where desktop apps were no longer big business to develop. It
112+
was eventually removed from the JDK and you will need to procure it like any other dependency.
113+
114+
Path #3 is to branch out to a different language. Kotlin and C# are decent choices for this from what I can
115+
tell.
116+
117+
118+
## Learn a New Language
119+
120+
An unfortunate truth is that you cannot use Java for everything.
121+
122+
Sometimes this is intrinsic - Java would not be a good choice for code
123+
driving a pacemaker - and some of it is just how the ecosystems around languages
124+
are right now.
125+
126+
Even if you could get away with only using Java, there is significant value in knowing multiple
127+
languages. Particuarly languages that are different than Java.
128+
129+
### JavaScript
130+
131+
To make highly interactive programs that run inside a browser you
132+
will need JavaScript. JavaScript and languages that compile to JavaScript
133+
are basically the only language it is practical
134+
to use for the frontend of a website[^wasm].
135+
136+
It is probably worth your time to learn how to write JavaScript.
137+
138+
[Mozilla Developer Network's JavaScript Guide here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)
139+
140+
The most popular language that compiles to JavaScript is TypeScript. After you've
141+
learned JavaScript that is a good one to touch next.
142+
143+
[TypeScript tutorial here](https://www.typescripttutorial.net/)
144+
145+
146+
There are languages out there like TypeScript that compile to JavaScript -
147+
and you can find some projects out there that do much the same for Java -
148+
but just practically speaking learning JavaScript
149+
150+
### C#
151+
152+
C# is a language broadly similar to Java. It has a lot of features Java doesn't - for better or worse -
153+
but the basics are comparable.
154+
155+
C# is very prevalent in the game development world. The Unity game engine,
156+
[corporate blunders notwithstanding](https://www.theverge.com/2024/9/12/24242937/unity-runtime-fee-cancelled-subscription-pricing), is still very big and still has you use C# for game scripts. Competitors like Godot [have C# Scripting as well](https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/index.html).
157+
158+
You will also find C# being used to make websites and desktop apps but it doesn't have
159+
as much unique pull there as it does in game development.
160+
161+
[Microsoft's C# tutorial here](https://learn.microsoft.com/en-us/training/paths/get-started-c-sharp-part-1/)
162+
163+
### Kotlin
164+
165+
Kotlin is one of a family of languages that try to be a "better Java."
166+
Better is relative, but you are likely to learn _something_ when diving into it.
167+
168+
Regardless, Kotlin is what you nowadays use to write Gradle build scripts in and
169+
Kotlin is the de-facto language for writing Android apps.
170+
171+
This means that, while Java in some form is still technically an option,
172+
all the documentation for Android will be using Kotlin for their examples
173+
and most new frameworks will assume you are using Kotlin in preference to Java.
174+
175+
And Gradle is a build tool that many Java projects choose to use. You probably don't need
176+
as deep of an understanding of Kotlin to work with Gradle build scripts as
177+
you do to make a full app in it, but it can't hurt.
178+
179+
[Getting Started with Kotlin tutorial here](https://kotlinlang.org/docs/getting-started.html)
180+
181+
[Kotlin Android Tutorial here](https://kotlinlang.org/docs/android-overview.html)
182+
183+
### Others
184+
185+
Other languages you might want to learn that I haven't written up
186+
context for quite yet:
187+
188+
* C
189+
* C++
190+
* Clojure
191+
* Elm
192+
* Haskell
193+
* Python
194+
* Ruby
195+
196+
[^wasm]: Fight me, WebAssembly fans.

src/regular_expressions.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Regular Expressions 🚧
2+
3+
<img src="/regular_expressions/header.png" height="200px"/>
4+
5+
Phone numbers in the US mostly look like `123-456-7890`. Three numbers,
6+
a dash, three numbers, a dash, then four numbers.[^simple]
7+
8+
Writing the code to check if a `String` matches this pattern is tricky.
9+
10+
```java,no_run
11+
boolean matchesPhoneNumberPattern(String s) {
12+
return s.length() == 12
13+
&& Character.isDigit(s.charAt(0))
14+
&& Character.isDigit(s.charAt(1))
15+
&& Character.isDigit(s.charAt(2))
16+
&& s.charAt(3) == '-'
17+
&& Character.isDigit(s.charAt(4))
18+
&& Character.isDigit(s.charAt(5))
19+
&& Character.isDigit(s.charAt(6))
20+
&& s.charAt(7) == '-'
21+
&& Character.isDigit(s.charAt(8))
22+
&& Character.isDigit(s.charAt(9))
23+
&& Character.isDigit(s.charAt(10))
24+
&& Character.isDigit(s.charAt(11))
25+
}
26+
```
27+
28+
For situations like this a useful tool is something called "regular expressions"
29+
and the `Pattern` class.
30+
31+
```java,no_run
32+
boolean matchesPhoneNumberPattern(String s) {
33+
return Pattern.compile("(\\d\\d\\d)-(\\d\\d\\d)-(\\d\\d\\d\\d)")
34+
.asMatchPredicate()
35+
.test(s);
36+
}
37+
```
38+
39+
40+
[^simple]: A common, but understandable, mistake is assuming that formats
41+
like phone numbers are actually this simple. Phone numbers and emails are always a little
42+
bit of a nightmare to validate.
375 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Character Classes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Exact Matches

src/regular_expressions/header.png

35.1 KB
Loading

src/regular_expressions/pattern.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Pattern
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Theoretical Basis
2+
3+
There are basically two levels you can engage the concept of regular expressions
4+
at.
5+
6+
The first is as a practical tool for matching patterns in text. You will learn
7+
how to construct special strings that act as a shorthand for code that can
8+
recognize if text meets a certain shape.
9+
10+
The second is around their place in the theory of computation. A regular expression
11+
is one step above a "deterministic finite automata" and one step below a "turing machine."
12+
13+
I tend to find that diving into the theoretical makes the practical part a lot less intimidating.
14+
It will be a giant tangent, but i suggest [diving in to a theory of computation course](https://www.youtube.com/watch?v=rJhfQp1jgGI&list=PLRgsEjJNLnh7yx0AAQMrdJssDySazeJzZ)[^random]. When you come out
15+
of it regular expressions will be much easier to understand.
16+
17+
[^random]: I just searched online and this one popped up. It looks fine, but maybe its not or maybe there is something better.

0 commit comments

Comments
 (0)