-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeSafety.java
46 lines (37 loc) · 1.65 KB
/
TypeSafety.java
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
package com.stech.generics;
import com.stech.example.Employee;
import java.util.ArrayList;
/**
* Created by sandeeplulla.
*/
public class TypeSafety {
public static void main(String args[]) {
Employee[] employees;
employees = new Employee[100];
employees[0] = new Employee(1, "Sandeep", "IT");
employees[1] = new Employee(2, "Ravi", "CE");
employees[2] = new Employee(3, "Alan", "IT");
//employees[3] = "Sandeep"; //This gives a compilation error since arrays are typesafe. Uncomment and compile to see the error.
for(int i=0; i<100;i++) {
System.out.println(employees[i]);
}
ArrayList employeeList = new ArrayList();
employeeList.add(new Employee(1, "Sandeep", "IT"));
employeeList.add(new Employee(2, "Ravi", "CE"));
employeeList.add(new Employee(3, "Alan", "IT"));
employeeList.add("Sandeep");
for(int i=0; i<employeeList.size();i++) {
if(employeeList.get(i) instanceof Employee) //Comment this to get ClassCastException
System.out.println(((Employee) employeeList.get(i)).getId());
}
//Using Generics
ArrayList<Employee> employeeListGenerics = new ArrayList();
employeeListGenerics.add(new Employee(1, "Sandeep", "IT"));
employeeListGenerics.add(new Employee(2, "Ravi", "CE"));
employeeListGenerics.add(new Employee(3, "Alan", "IT"));
//employeeListGenerics.add("Sandeep"); //Uncomment this to get compilation error
for(int i=0; i<employeeListGenerics.size();i++) {
System.out.println(employeeListGenerics.get(i).getId());
}
}
}