Skip to content

Commit dd0b125

Browse files
committed
Add 5 predict/howto tasks
1 parent 624a754 commit dd0b125

File tree

6 files changed

+194
-1
lines changed

6 files changed

+194
-1
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ jacocoTestReport {
7171
afterEvaluate {
7272
classDirectories.setFrom(files(classDirectories.files.collect {
7373
fileTree(dir: it, exclude: [
74-
'**/config/**',
7574
'**/domain/**',
7675
'**/dto/**',
7776
'**/refactoring*/initial/**',
77+
'**/core/howto/**',
78+
'**/core/predict/**',
7879
])
7980
}))
8081
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package by.andd3dfx.core.howto;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* <pre>
7+
* How to convert an array of objects to an array of their primitive types?
8+
*
9+
* <a href="https://stackoverflow.com/questions/564392/converting-an-array-of-objects-to-an-array-of-their-primitive-types">See Q&A</a>
10+
* </pre>
11+
*/
12+
public class ConvertObjectsArrayToPrimitivesArray {
13+
14+
public static void main(String[] args) {
15+
Integer[] array = {1, 2, 3, 4, 5};
16+
17+
int[] converted = Arrays.stream(array).mapToInt(value -> value).toArray();
18+
19+
System.out.println(Arrays.toString(converted));
20+
}
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package by.andd3dfx.core.predict;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* <pre>
8+
* Рассказать свои мысли о данном коде:
9+
*
10+
* List<Employee> list = new ArrayList<Employee>();
11+
* list.add(new Manager(100));
12+
* list.add(new Manager(200));
13+
* ((List<Manager>)list).get(1).getBonus();
14+
* </pre>
15+
*/
16+
public class DangerousCasting {
17+
18+
public static void main(String[] args) {
19+
List<Employee> list = new ArrayList<Employee>();
20+
list.add(new Manager(100));
21+
list.add(new Manager(200));
22+
//((List<Manager>)list).get(1).getBonus();
23+
var bonus = ((Manager)list.get(0)).getBonus();
24+
System.out.println(bonus);
25+
26+
/**
27+
* Во время выполнения в 4й строке вывалится ClassCastException.
28+
* Но такой каст бы прошел: ((Manager)list.get(0)).getBonus()
29+
*/
30+
}
31+
32+
public static class Employee {
33+
}
34+
35+
public static class Manager extends Employee{
36+
private int bonus;
37+
38+
public Manager(int bonus) {
39+
this.bonus = bonus;
40+
}
41+
42+
public int getBonus() {
43+
return bonus;
44+
}
45+
}
46+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package by.andd3dfx.core.predict;
2+
3+
/**
4+
* <pre>
5+
* Что будет выведено в консоль?
6+
*
7+
* String string = "Hello";
8+
* string.concat(" World");
9+
* System.out.println(string);
10+
* </pre>
11+
*/
12+
public class ImmutableString {
13+
14+
public static void main(String[] args) {
15+
String string = "Hello";
16+
string.concat(" World");
17+
System.out.println(string);
18+
19+
/**
20+
* Будет выведено только 'Hello', т.к. String - immutable,
21+
* и результат вызова concat() попадет в другую строку,
22+
* которую никакой переменной не присвоили
23+
*/
24+
}
25+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package by.andd3dfx.core.predict;
2+
3+
/**
4+
* <pre>
5+
* Что выведется в консоль при создании объекта класса B?
6+
*
7+
* public static class A {
8+
* private int a = 1;
9+
* public A() {
10+
* System.out.println(getA());
11+
* }
12+
*
13+
* public int getA() {
14+
* return a;
15+
* }
16+
* }
17+
*
18+
* public static class B extends A {
19+
* private int a = 2;
20+
* public B() {
21+
* System.out.println(getA());
22+
* }
23+
*
24+
* public int getA() {
25+
* return a;
26+
* }
27+
* }
28+
* </pre>
29+
*/
30+
public class Inheritance {
31+
32+
public static void main(String[] args) {
33+
var b = new B();
34+
35+
/**
36+
* Выведется: 0, потом 2, т.к. при вызове конструктора класса A вызовется (из-за полиморфизма)
37+
* метод getA() класса B, который выведет значение переменной a, к тому моменту еще неинициализированной.
38+
*/
39+
}
40+
41+
public static class A {
42+
private int a = 1;
43+
public A() {
44+
System.out.println(getA());
45+
}
46+
47+
public int getA() {
48+
return a;
49+
}
50+
}
51+
52+
public static class B extends A {
53+
private int a = 2;
54+
public B() {
55+
System.out.println(getA());
56+
}
57+
58+
public int getA() {
59+
return a;
60+
}
61+
}
62+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package by.andd3dfx.core.predict;
2+
3+
import java.util.stream.Stream;
4+
5+
/**
6+
* <pre>
7+
* Что будет выведено в консоль?
8+
*
9+
* public static Stream<String> bar(Stream<String> stream) {
10+
* System.out.println("start bar");
11+
* return stream.filter(el -> !el.equals("bar"))
12+
* .peek(System.out::print);
13+
* }
14+
*
15+
* public static void main(String[] args) {
16+
* Stream<String> stream = Stream.of("foo", "bar", "foo", "bar");
17+
* Stream<String> result = bar(stream);
18+
* }
19+
* </pre>
20+
*/
21+
public class NoTerminalOpOnStream {
22+
23+
public static Stream<String> bar(Stream<String> stream) {
24+
System.out.println("start bar");
25+
return stream.filter(el -> !el.equals("bar"))
26+
.peek(System.out::print);
27+
}
28+
29+
public static void main(String[] args) {
30+
Stream<String> stream = Stream.of("foo", "bar", "foo", "bar");
31+
Stream<String> result = bar(stream);
32+
33+
/**
34+
* Кажется, что будет выведено 'foofoo', но на стриме не вызвана терминальная операция,
35+
* поэтому будет выведена только надпись `start bar`.
36+
*/
37+
}
38+
}

0 commit comments

Comments
 (0)