Why do you need (LinkedList)?

So, I have one line of code:

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

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

+4
source share
4 answers

Answer: To access the methods and fields of the target class, you need to enter a type.

Objectand LinkedListis a hierarchy of the same type, so you can use, otherwise you will get ClassCastExceptionin Java. For more information on type casting, see Link.

+3
source

The method Object.clone()returns the result of the type Object. If you want to use it as LinkedList, you need an explicit type cast result LinkedList.

+3

clone() Object, LinkedList

+2

LinkedList Java's > . , clone deffinition:

protected Object clone()

Object Java , .

LinkedList, ( "" ) /, Object.

+2

All Articles