-
-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathBoxDemoApp.java
More file actions
21 lines (19 loc) · 901 Bytes
/
BoxDemoApp.java
File metadata and controls
21 lines (19 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.bobocode.basics;
/**
* This demo demonstrates why using Object is not safe. It's not safe because runtime casting can cause runtime
* exceptions. We should always fail as soon as possible. So in this code we should not allow setting String
* value at compile time, if we expect to work with integers.
* <p>
* todo: refactor class {@link Box} to make type parameterization safe and make this demo fail at compile time
*/
public class BoxDemoApp {
public static void main(String[] args) {
var intBox = new Box<>(123);
var intBox2 = new Box<>(321);
System.out.println(intBox.getValue() + intBox2.getValue());
intBox.setValue(222);
// intBox.setValue("abc"); // this should not be allowed
// the following code will compile, but will throw runtime exception
System.out.println(intBox.getValue() + intBox2.getValue());
}
}