Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Exploring the Java Collection Framework Using ArrayLists

Pedro Silva
Pedro Silva
5,363 Points

why can i set "new ArrayList<String>()" for the "List<String> results"?

it's because ArrayList inherit from List? i'm still very confused about this process in the "List<String> results = new ArrayList<String>();" line.

Also, this is a generic? or generic is whole concept that contains this process?

Thanks

1 Answer

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Pedro..

Let me try to understand your question first okay?

  1. Why Java allows you to declare and instantiate this: List<String> result = new ArrayList<String>(); ?
  2. What is a generic?

I hope I get your question correctly. Now as for the answer for the first question:

Java not just allows you to do List<String> result = new ArrayList<String>(); declaration but Java insisted you to do exactly that. The main reason here is because List is actually an interface. What is an interface? Hmmm... the easiest explanation is: interface has methods declared inside it just like class, but the methods inside interface is all empty. So basically interface only has method declarations such as:

interface Example {
    void addItems(Object item);
}

So to be able to make this interfaces works we need to wrap it up into class that implements the interface. This class provides method bodies to the implemented interfaces, such as:

class Wrapper implements Example{
   void addItems(Object item) {
  //do something about the item;
      itemList.add(item);
  }
}

Therefore, in the List<String> result = new ArrayList<String>(); the ArrayList is a class object that implements List interface and act as a wrapper for the List interface to provide more method definition on how it should behave.

okay for the second question: What is generic?

Hmmm tough question.... What you see in List<String> result = new ArrayList<String>(); is an example of generic type instantiation. The part that make it generic is code inside the '<....>' characters. So what it means to be generic? To be honest I have not dive that deep yet, but from what I seen so far makes me look back when Craig still showing us the Treet apps. Remember when he taught about casting? He uses instanceOf to make sure that elements of a list (I forget the name of that list) in Java Repl is really are a Treet. Then he do the same thing to make sure if the other element is a String.

How does this matter? Well it should because instanceOf is there to save us all from the worst kind of exception (errors for us muggles) the Runtime Exception. The hardest one to catch. So what a generic have to do with this? Well generic solve the problem before you even need an instanceOf method. If at that time Craig do this:

List<Treet> treets = new ArrayList<Treet>();
treets.add(treet); //<-- this will be okay since treet is a Treet
String aTreet = "I am a Treet";
treets.add(aTreet);//<-- this is not okay because aTreet is not a Treet it is a String

But the not okay thing at that code level will be recognized and caught at compiler level not runtime level (after becoming a machine language and runs). This will make the code easier to repair.

Thus from that example we can learn that generics (which is the <....> part) is used to let us just say define what should be inside an object. The formal definition of a generic is to parameterized over types. Types here being like a String, Boolean, and others. Thus generic instantiation give boundaries as what type of parameter can be processed of inputted inside and Object like List<>.

I hope this can help a little. Any other inputs are welcome since I am also still studying.

Pedro Silva
Pedro Silva
5,363 Points

Thank you Yanuar!

You shed some light in something that was making me crazy! The thing that i wasn't understanding was why ArrayList was used there, but when you said that it was a class that implemented List and saw the documentation everything made sense.

About the generic i think that what made me confused was the word "generic", it made think that anything could be used and not that i was specifically setting a type.

Sorry for the bad wording in my answer, english is not my first language, but you really helped me here, so again, thanks!