-
-
Notifications
You must be signed in to change notification settings - Fork 549
Expand file tree
/
Copy pathBoxDemoApp.java
More file actions
23 lines (20 loc) · 952 Bytes
/
BoxDemoApp.java
File metadata and controls
23 lines (20 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.bobocode.basics;
import com.bobocode.basics.Box;
/**
* 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) {
Box intBox = new Box(123);
Box intBox2 = new Box(321);
System.out.println((int) intBox.getValue() + (int) 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((int) intBox.getValue() + (int) intBox2.getValue());
}
}