forked from forax/java-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter01-basic_types.jsh
166 lines (129 loc) · 5.27 KB
/
chapter01-basic_types.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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
// # Basic Types
// Java has two kinds of type,
// - primitive types that are directly mapped to CPU basic types
// - reference types that address of the object in memory
// ## Primitive types
// primitive types, written in lower case, have no method
// ### boolean (true|false)
var result = true;
var anotherResult = false;
// ### char (character)
var firstLetter = 'j';
// ### int (signed 32 bits integer)
var numberOfLegs = 2;
// ### double (64 bits floating point)
var cost = 3.78;
// ### long and float
// some more exotic types that requires a suffix (`L` or `f`)
// long (64 bits integers) and float (32 bits floating point numbers)
var longValue = 123L;
var floatValue = 123.5f;
// ### byte and short
// you also have byte (a signed 8 bits integer) and short (a signed 16 bits short integer)
// that are only useful to take less memory when defining an object
record CompactHeader(byte tag, short version) {}
// when used in variables, they are promoted to 32 bits integer
// in the following code result is a 32 bits integer (so an int)
short value = 12;
var result = value + value;
// ### primitive conversions
// You have automatic conversions if there is no loose of precision
// and converting to double or float is always allowed
int intValue = 13;
long longValue = intValue;
// you can force conversion in the opposite direction using a cast
// supplementary bits will be shaved (use with reluctance)
long longValue = 1_000_000_000_000L;
int intValue = (int) longValue;
System.out.println(intValue);
// ## Objects
// All other types are objects, there are two special types, String and arrays
// that are object but considered as built-in by the compiler
// ### String
// A String that stores a text (a sequence of characters) is delimited
// by two doublequotes
var text = "hello";
System.out.println(text);
// a String can also span several lines, it's called a __text block__
// and starts and ends with 3 double quotes
var multilineText = """
This is
a multilines string
""";
System.out.println(multilineText);
// The indentation is determined by the alignment compared to position of the last """
// By example, to have an indentation of two spaces
var multilineText = """
This is
a multilines string
indented by two spaces
""";
System.out.println(multilineText);
// Strings have a lot of methods, here is some of them
// length of a String
System.out.println("hello".length());
// to upper/lower case
System.out.println("hello".toUpperCase());
System.out.println("hello".toLowerCase());
// repeat the same pattern
System.out.println("|*|".repeat(3));
// char at an index (starting with index 0)
System.out.println("hello".charAt(0));
// index of a character
System.out.println("hello".indexOf('l'));
System.out.println("hello".indexOf('o'));
// primitive to String
// The fastest and easy way to convert a primitive value to a String is
// to use the string concatenation (see chapter 'string formatting' for more)
System.out.println("" + 3);
System.out.println("" + 7.6);
// String to primitive
// There are a bunch of static methods in Boolean, Integer or Double
// (see chapter 'wrapper' for more info)
System.out.println(Integer.parseInt("3"));
System.out.println(Double.parseDouble("7.6"));
// ### Array
// an array initialized with zeros (false, 0, 0.0, etc)
var intArray = new int[2];
// An array initialized with some default values
// Because a value like `2` or `3` can be an numeric type
// (an `int`, a `long`, a `short`, etc)
// you have to specify the type of the array when you create it
var intArray = new int[] {2, 3 };
var longArray = new long[] { 2, 3 };
// you can use the operator [] to access or change the value
// of an array at a specific index
System.out.println(intArray[0]);
intArray[0] = 42;
System.out.println(intArray[0]);
// trying to access an array out of its bound raised an exception
intArray[-1] = 42; // throws IndexOutOfBoundsException
// and a special syntax to get the length of an array
// Notice that there is no parenthesis when calling length,
// we will see later why.
var arrayLength = intArray.length;
System.out.println(arrayLength);
// arrays have methods like \toString()` or `equals()` but
// they are not implemented correctly, we will see later why
System.out.println(intArray);
System.out.println(new int[] {42}.equals(new int[] {42}));
// ### On arrays
// We don't use array much in Java, we have more
// powerful object like List, that we will see later
var intList = List.of(2, 3);
// ## Static methods
// Because primitive types and arrays have (almost) no method,
// if you want to play with them you have to use static methods.
// A static method is a function that is declared on a type somewhere
// that you can call using the syntax `SomeWhere.methodName(arg0, arg1, arg2)`
// by example to transform a String to an int, we call the method
// parseInt stored in the type `java.lang.Integer`
var resultAsInt = java.lang.Integer.parseInt("42");
System.out.println(resultAsInt);
// To transform an array to a text, there is the static method toString
// on the type `java.util.Arrays`
var text = java.util.Arrays.toString(intArray);
System.out.println(text);