0

So I searched up what Anytype means and this is what I found:

"There is no particular construct in Java named “AnyType“. It is used to create “generic” classes that can perform operations on literally any data type or AnyType that extends a base class."

I was wondering if someone could explain this to me in simpler terms?

We are learning nodes and linked lists, and some code I found online for it looked like this:

public AnyType removeFirst() //Removes the first element in the list.
{
  AnyType tmp = getFirst();
  head = head.next;
  return tmp;
}

This was retrieved from https://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/code/LinkedList.java AND https://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html

I don't understand its use in this context. Sorry if there are better places for to search.

ShaheerL
  • 63
  • 7
  • 2
    It's a type parameter, much like the `T` in `ArrayList`. It serves as a placeholder for the name of a class (with some [caveats](https://stackoverflow.com/questions/339699/java-generics-type-erasure-when-and-what-happens)). Unfortunately, the naming `AnyType` doesn't meet common conventions. – nanofarad Oct 08 '18 at 23:37
  • @AndreyAkhmetov what do you mean by placeholder? As in another way to name a method such as public static String or public static Int etc? – ShaheerL Oct 08 '18 at 23:39
  • At run-time it means `Object`. Generics are a compile time type checking feature. – Elliott Frisch Oct 08 '18 at 23:41
  • It means that you can re-use the `LinkedList` class with any type of contents--i.e. you can use the same `LinkedList` code to get a linked list of strings, a linked list of `Integer`s, a linked list of files, etc. I unfortunately cannot write up a full answer right now, but https://docs.oracle.com/javase/tutorial/java/generics/ would be a good resource compared to my simplified and scattered remarks here. – nanofarad Oct 08 '18 at 23:41
  • @AndreyAkhmetov oh ok thanks man :) will be sure to check that out – ShaheerL Oct 08 '18 at 23:55
  • Thanks @ElliottFrisch :) – ShaheerL Oct 08 '18 at 23:55

0 Answers0