I am new to java and reading a tutorial that instructs me to use a string-cast to assign a value from LinkedList
to a String object.
List l1 = new LinkedList();
l1.add("Michael");
String s1 = (String) l1.get(0);
But I did a type test and l1.get(0)
reported as a String type:
System.out.println(l1.get(0).getClass());
Result:
class java.lang.String
So I don't understand why the result of l1.get(0)
has a type of String
but I still need to type-cast it as a String in order to assign same to String variable.