Skip to content

Commit 28725cd

Browse files
committed
Time and interfaces
1 parent 470f103 commit 28725cd

23 files changed

+761
-38
lines changed

src/SUMMARY.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@
458458
- [Integer to a Base 16 String](./integers_ii/integer_to_a_base_16_string.md)
459459
- [Underscores in Integer Literals](./integers_ii/underscores_in_integer_literals.md)
460460

461-
- [🚧 Construction Zone 🚧]()
462461

463462
# Interactive Programs III
464463

@@ -471,6 +470,8 @@
471470
- [Create a Folder](./files/creating_a_folder.md)
472471
- [Challenges](./files/challenges.md)
473472

473+
474+
<!--
474475
# Data Structures & Algorithms II
475476
476477
- [Hash Maps](./hash_maps.md)
@@ -483,7 +484,7 @@
483484
- [Growable Buckets](./hash_maps/growable_buckets.md)
484485
- [Keys and Values](./hash_maps/keys_and_values.md)
485486
- [Put Items](./hash_maps/put_items.md)
486-
- [Get Items](./hash_maps/get_items.md)
487+
- [Get Items](./hash_maps/get_items.md)-->
487488

488489

489490
# Code Structure IV
@@ -496,18 +497,24 @@
496497
- [@Override](./objects/override.md)
497498
- [equals and hashCode](./objects/equals_and_hashCode.md)
498499
- [Override equals and hashCode](./objects/override_equals_and_hashCode.md)
499-
- [Variance and Casting](./objects/variance_and_casting.md)
500-
- [Subclasses](./objects/subclasses.md)
500+
<!--- [Variance and Casting](./objects/variance_and_casting.md)
501+
- [Subclasses](./objects/subclasses.md)-->
502+
503+
- [Generics](./generics.md)
504+
- [Type Variables](./generics/type_variables.md)
505+
- [Naming](./generics/naming.md)
506+
- [Instantiation](./generics/instantiation.md)
507+
- [Inference](./generics/inference.md)
508+
- [Soundness](./generics/soundness.md)
509+
- [Raw Types](./generics/raw_types.md)
501510
- [Interfaces](./interfaces.md)
502511
- [Interface Declaration](./interfaces/interface_declaration.md)
512+
- [Implementation](./interfaces/implementation.md)
513+
- [@Override](./interfaces/override.md)
503514
- [Naming](./interfaces/naming.md)
504-
- [Instances](./interfaces/instances.md)
505-
- [Fields](./interfaces/fields.md)
506-
- [Methods](./interfaces/methods.md)
507515
- [Subtypes](./interfaces/subtypes.md)
508-
- [Generics](./generics.md)
509-
- [Generic Parameters](./generics/generic_parameters.md)
510-
- [Wildcards]()
516+
- [Multiple Implementations](./interfaces/multiple_implementations.md)
517+
511518
# Data Types V
512519

513520
<!-- Note: Put a joke about the "A train leaves chicago at ..." problems.
@@ -529,7 +536,7 @@ Make them do one. -->
529536
- [Loop over items](./array_list/loop_over_items.md)
530537
- [Set an item](./array_list/set_an_item.md)
531538
- [Remove an item](./array_list/remove_an_item.md)
532-
- [HashMap](./hash_map.md)
539+
<!--- [HashMap](./hash_map.md)
533540
- [Multi-Dimensional Arrays](./multi_dimensional_arrays.md)
534541
- [Iterable and Iterator](./iterable_and_iterator.md)
535542
# Concepts II
@@ -541,7 +548,7 @@ Make them do one. -->
541548
- [Encapsulation](./encapsulation.md)
542549
- [Implementation Details](./encapsulation/implementation_details.md)
543550
- [Methods](./encapsulation/methods.md)
544-
- [Classes](./encapsulation/classes.md)
551+
- [Classes]()(./encapsulation/classes.md)
545552
- [Implicit Interfaces](./encapsulation/implicit_interfaces.md)
546553
- [Abstractions](./encapsulation/abstractions.md)
547554
- [Leaky Abstractions](./encapsulation/leaky_abstractions.md)
@@ -554,7 +561,7 @@ Make them do one. -->
554561
- [Iterable]()
555562
- [ArrayList]()
556563
- [Concurrent Modifications](./loops_iii/concurrent_modifications.md)
557-
- [Reasons to go back]() <!-- -->
564+
- [Reasons to go back]() <!--
558565
- [Switch III](./switch_iii.md)
559566
- [Fallthrough](./switch_iii/fallthrough.md)
560567
- [break](./switch_iii/break.md)
@@ -596,7 +603,7 @@ Make them do one. -->
596603
- [Pattern]()
597604
598605
# Communication
599-
<!-- Should cover InputStream and OutputStream - maybe in Files part deux? -->
606+
<!-- Should cover InputStream and OutputStream - maybe in Files part deux?
600607
- [TCP over IP](tcp_over_ip.md)
601608
- [Sockets](./tcp_over_ip/sockets.md)
602609
- [Low Level versus High Level](./tcp_over_ip/low_level_versus_high_level.md)
@@ -638,7 +645,7 @@ Make them do one. -->
638645
- [Pattern Matching]()
639646
- [Primitive Patterns]()
640647
- [Record Patterns]()
641-
648+
-->
642649
<!--
643650
644651
@@ -865,3 +872,7 @@ multi dimensional arrays
865872
866873
System.console() over Scanner?
867874
-->
875+
876+
877+
878+
- [🚧 (More Chapters Planned) 🚧]()

src/generics.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Generics
22

3-
Certain types of classes, like growable arrays
4-
and hash maps, are simply holders of data.
3+
Certain types of classes, like growable arrays, are simply holders of data.
54

65
That is, almost none of how they work has to change to
76
store different kinds of data.

src/generics/generic_parameters.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/generics/inference.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Inference
2+
3+
Sometimes Java has enough information to know
4+
what the right values for type variables would be without you
5+
specifying them.
6+
7+
In these cases you can rely on the compiler to infer them by writing `<>` with no type variables
8+
in between.
9+
10+
```java
11+
class Box<T> {
12+
T data;
13+
}
14+
15+
void main() {
16+
// It is being assigned to a Box<String> on the left
17+
// so Java can figure out what should be on the right.
18+
Box<String> boxOfString = new Box<>();
19+
20+
boxOfString.data = "abc";
21+
22+
String s = boxOfString.data;
23+
24+
System.out.println(s);
25+
}
26+
```
27+
28+
This inference does not work on the left-hand side of an `=`.
29+
30+
```java,does_not_compile
31+
class Box<T> {
32+
T data;
33+
}
34+
35+
void main() {
36+
// Use var if you want inference for local variables.
37+
Box<> boxOfString = new Box<String>();
38+
39+
boxOfString.data = "abc";
40+
41+
String s = boxOfString.data;
42+
43+
System.out.println(s);
44+
}
45+
```

src/generics/instantiation.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Instantiation
2+
3+
When you make an instance of a class that takes generic parameters
4+
you are expected to provide the actual concrete types you want to be used
5+
in place of any type variables.
6+
7+
You do this by writing the name of the types inside a `<>` alongside the call to `new`.
8+
9+
```java
10+
class Box<T> {
11+
T data;
12+
}
13+
14+
void main() {
15+
var boxOfString = new Box<String>();
16+
17+
boxOfString.data = "abc";
18+
19+
String s = boxOfString.data;
20+
21+
System.out.println(s);
22+
}
23+
```

src/generics/naming.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Naming
2+
3+
Type variables don't have to be only a single letter, though that is common. If you pick a longer name
4+
you are generally expected to name them as if they were classes.
5+
6+
```java
7+
class Box<Data> {
8+
Data data;
9+
}
10+
```

src/generics/raw_types.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Raw Types
2+
3+
If generics are cramping your style, you are
4+
technically allowed to turn them off.
5+
6+
If you make an instance of a generic class without
7+
any `<>` we call that a "raw type."
8+
9+
```java
10+
class Box<T> {
11+
T data;
12+
}
13+
14+
void main() {
15+
Box b = new Box();
16+
}
17+
```
18+
19+
When you have a raw type you will see `Object` in any place
20+
you put[^unbounded] a type variable.
21+
22+
This lets you do whatever you want without the burden of having to make sense to Java.
23+
24+
```java
25+
class Box<T> {
26+
T data;
27+
}
28+
29+
void main() {
30+
Box b = new Box();
31+
b.data = 123;
32+
b.data = "abc";
33+
34+
if (b.data instanceof String s) {
35+
System.out.println(s);
36+
}
37+
}
38+
```
39+
40+
Raw types exist for two basic reasons
41+
42+
1. Every now and then Java isn't smart enough. Trust that there are valid reasons to turn off generics, even
43+
I haven't shown you any yet. Avoid doing so yourself - at least for awhile.
44+
2. Generics weren't always in Java! Classes that later were made generic had to stay compatible with old "raw"
45+
usages somehow.
46+
47+
All that is to say: Be aware raw types exist. Make sure you are always putting `<>` otherwise you are falling
48+
into that mode. Avoid that mode.
49+
50+
51+
[^unbounded]: An "unbounded" type variable to be exact. We'll visit generic bounds later.

src/generics/soundness.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Soundness
2+
3+
Even though every `String` is assignable to `Object`,
4+
a `Box<String>` is not assignable to `Box<Object>`.
5+
6+
If that was allowed then you could do something like the following.
7+
8+
```java,no_run,does_not_compile
9+
Box<String> stringBox = new Box<>();
10+
// This might feel innocent
11+
Box<Object> objectBox = stringBox;
12+
// But now we can put anything,
13+
// like an Integer, in the Box
14+
objectBox.data = 123;
15+
// Which is a problem since that affects
16+
// stringBox as well
17+
String s = stringBox.data; // But its actually an Integer! Crash!
18+
```
19+
20+
We call this property - that the types don't let you accidentally make
21+
silly situations - soundness. Java isn't _fully_ sound, but its sound enough
22+
for most people.

src/generics/type_variables.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Type Variables
2+
3+
After the name of a class in the class definition you can put one or more "type variables."
4+
These look like `<` followed by a comma separated list of "type names" and ended by a `>`.
5+
6+
```java,no_run
7+
class Box<T> {
8+
9+
}
10+
```
11+
12+
Inside a class definition you can use these type variables on fields, method arguments,
13+
and method return types.
14+
15+
```java,no_run
16+
class Box<T> {
17+
T data;
18+
19+
void setData(T newData) {
20+
this.data = newData;
21+
}
22+
}
23+
```
24+
25+
This indicates that you would be okay with any type being used in place of the type variable (in the above code `T`).

src/hash_maps/get_items.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,41 @@ class CarsCharacterHashMap {
8686
return carsCharacter.lastName().charAt(0);
8787
}
8888

89-
void put(CarsCharacter character) {
89+
void put(String lastName, CarsCharacter character) {
9090
// 1. Compute the hash code
91-
char hash = hashFunction(character);
91+
char hash = hashFunction(lastName);
9292
// 2. Find the bucket
9393
Bucket bucket = buckets[indexFor(hash)];
9494
// 3. Add to the bucket
9595
bucket.add(character);
9696
}
97+
}
9798

98-
` get(CarsCharacter character) {
99+
CarsCharacter get(String lastName) {
99100
// 1. Compute the hash code
100101
char hash = hashFunction(character);
101102
// 2. Find the bucket
102103
Bucket bucket = buckets[indexFor(hash)];
103-
// 3. Add to the bucket
104-
bucket.add(character);
104+
// 3. Go through everything in the bucket
105+
for (int i = 0; i < bucket.size(); i++) {
106+
CarsCharacter inBucket = bucket.get(i);
107+
if (inBucket.equals(character)) {
108+
return inBucket; // Return any matches
109+
}
110+
}
111+
112+
// Null if no matches
113+
return null;
105114
}
106115
}
107116

108117
void main() {
109118
var map = new CarsCharacterHashMap();
110119

111-
map.put(new CarsCharacter("Sally", "Carrera"));
112-
map.put(new CarsCharacter("Doc", "Hudson"));
113-
map.put(new CarsCharacter("Lightning", "McQueen"));
120+
map.put("Carrera", new CarsCharacter("Sally", "Carrera"));
121+
map.put("Hudson", new CarsCharacter("Doc", "Hudson"));
122+
map.put("McQueen", new CarsCharacter("Lightning", "McQueen"));
123+
114124

115125
System.out.println(map.buckets[0].size());
116126
System.out.println(map.buckets[1].size());

0 commit comments

Comments
 (0)