-
Notifications
You must be signed in to change notification settings - Fork 68
/
chapter00-genesis.jsh
86 lines (70 loc) · 3.84 KB
/
chapter00-genesis.jsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// To starts, run jshell --enable-preview which is a program able to interpret Java syntax
// then cut and paste the following lines to see how it works
// To exit jshell type /exit
// # Genesis
// In Java, there is strong division between primitive types like double that are written in lower case and
// objects like String that have a name that starts with an uppercase letter.
// ## Types
// A primitive type is stored as value while an object is stored as
// a reference (the address of the object in memory).
// In Java, `var` creates a new variable
var maxIntensity = 1.0; // it's a value
var colorName = "black"; // it's a reference to String somewhere in memory
// you can also indicate the type instead of `var`
// if you are using var, you are asking the compiler to find the type for you.
String colorName = "black";
// ### System.out.println()
// To print a value in Java we have a weird incantation `System.out.println()` that we will detail later.
System.out.println(maxIntensity);
// Primitive types and objects can be printed using the same incantation.
System.out.println(colorName);
// ### Concatenation with +
// If we want to print a text followed by a value, we use the operator `+`.
System.out.println("the value of colorName is " + colorName);
// ## A record is a user defined type
// here Light is defined as containing two components: a color (typed as a String) and
// an intensity (typed as a 64-bit floating number double).
record Light(String color, double intensity) {}
// ### Object creation with `new`
// To create an object in memory, we use the operator `new` followed by the value of each record component.
// The following instruction creates a Light with "blue" as color and 1.0 as intensity.
var blueLight = new Light("blue", 1.0);
System.out.println(blueLight);
// ### Record methods
// To interact with an object in Java, we use methods, that are functions attached to an object.
// To call a method, we use the operator `.` followed by the name of the method and its arguments.
// A record automatically declares methods to access its components so Light declares two methods
// color() and intensity().
// By example to get the intensity of the object blueLight
System.out.println(blueLight.intensity());
// ### toString()
// By default a record knows how to transform itself into a String
// in Java, the method to transform an object to a String is named toString().
// In fact, println() calls toString() if the argument is an object
// so when using println(), calling explicitly toString() is not necessary.
System.out.println(blueLight.toString());
System.out.println(blueLight);
// ### equals()
// In Java, you can ask if two objects are equal, using the method equals(Object).
// The return value is a boolean (a primitive type that is either true or false).
var redLight = new Light("red", 0.5);
var redLight2 = new Light("red", 0.5);
System.out.println(blueLight.equals(redLight));
System.out.println(redLight.equals(redLight2));
// ### hashCode()
// You can also ask to get an integer summary (a hash) of any objects.
// This is used to speed up data structures (hash tables).
// Two objects that are equals() must have the same hashCode().
var greenLight = new Light("green", 0.2);
var greenLight2 = new Light("green", 0.2);
System.out.println(greenLight.hashCode());
System.out.println(greenLight2.hashCode());
// ## Summary
// A `record` has components that are the parameters used to create an object
// To create an object we use the operator `new` followed by the arguments of the
// record components in the same order.
// To interact with an object, we are using methods that are functions that you
// call on an object using the operator `.`.
// A Record defines methods to access the value of a component, and also
// `toString()` to get the textual representation of an object and
// `equals()` and `hashCode()` to test if two objects are equal.