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

Diamond " <> " [About List and ArrayList] what is that thing doing? :)

I'm so curious about this sign <> , I think JAVA call this for diamond.

When I code like this in Intellij it warns that I cant use by this code

List <String> hello = new ArrayList <String>();

it should be like this:

List <String> hello = new ArrayList<>();

Is it because of the new update JAVA8? :smile:

Next question:

List <> hello = new ArrayList<Integer>();

it warns me strange, I don't understand please explain me

Third Question:

// Question 3A)
List <Integer,Integer> number = new ArrayList<>();  

//Question 3B)
List <Integer,String> number = new ArrayList <Integer,String>(); 

//Question 3C)
List <> number = new ArrayList <Integer,String>();  

these three tests gave me warnings

please explain me every single questions STEP BY STEP ty :pray:


this post has been updated 3/01 :relaxed: Thank you MOD Seth Kroger :v:

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Sadly < and > are for HTML tags which makes it problematic to include them in posts. You can use &lt; &gt; to get around that. Or you can use three backticks, which is the key to the left of the 1 on many keyboards, for code and code blocks.

```java

// ...code here...

```

  // ...code here...

Now on to your questions:

First: The warning is just a warning, not an actual error. Both ways are correct. The "diamond operator" <> was added in Java 7 to simplify things because once you've declared the type it doesn't make a lot of sense to keep repeating it. The new way is preferred but the old way is still valid and there's a lot of code out there written like that.

Second: The diamond operator only works after you've already declared the type once. Using List<> someList; doesn't work because you haven't declared what it should be a list of yet.

Third: Lists are defined as having only one generic type parameter. Passing it two causes an error because it's undefined. So, in Java you can only have a list of one type (and all it's subtypes) of object.

Sir :pray: thanks for the wonderful answer! Now I understand this is about the Generic type? I think I must study about that! The third one : so you mean that I should ONLY type ONE type? Which means <String,String> is not allowed only one right?

But I just wondering in JAVA there is no called Tupels right? I mean what if I want a String and an Integer??

.. I think I have mixed with Haskell language..eww. :angry: