Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need (LinkedList)?

So I have this one line of code here:

LinkedList<Integer> p = (LinkedList) obj.c.clone();

I was wondering why do you need the (LinkedList) part before obj.c.clone()?

like image 378
user3854309 Avatar asked Dec 25 '22 03:12

user3854309


1 Answers

The answer is: You need type casting to get access to target class methods and fields.

Object and LinkedList is same type hierarchy, so you can cast, otherwise You would get ClassCastException in Java. For more information about type casting, See this link.

like image 92
changtung Avatar answered Jan 11 '23 05:01

changtung