• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding an array to a List  RSS feed

 
Samuel Weston
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry- I know this is a simple question, but I forgot the answer

Im making a simple code to add an array to a List


(the code im referring to is <String> )


my simple question is - what are the <String> ...<String> for?
I understand it makes the list1 variable a string, but why is it made like this?
do we usualy use <String> when we need to make a variable a String?
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are called Generics and it makes sure that you can't store anything else in that collection except what you've declared, in your case, a String. The advantage of this is that it turns a possible runtime error into a compilation error. Without the generic declaration in there, the code would compile and run and wouldn't blow up until your code tried to store say an Integer. By declaring the type with a Generic, the compiler can catch the error before it becomes a runtime problem.
 
Sooraj Rajagopalan
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The List, maps, set, SortedSet in java (also known as collections) are all interfaces. And everytime you use the collections, the Interface is implemented. In this example, now that you have narrowed it down to <String>, the implementation of the interface "List", stores String.
Now consider the following


The same thing happens, which is the implementation of the interface. Only that list1 stores Lists(which store String).
Hope it helps than making you all the more confused. cheers!!
 
Samuel Weston
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Those are called Generics and it makes sure that you can't store anything else in that collection except what you've declared, in your case, a String. The advantage of this is that it turns a possible runtime error into a compilation error. Without the generic declaration in there, the code would compile and run and wouldn't blow up until your code tried to store say an Integer. By declaring the type with a Generic, the compiler can catch the error before it becomes a runtime problem.




Thank you very much for your response. You've given some things to think on, but I understand it now. Much obliged
 
Samuel Weston
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sooraj Rajagopalan wrote:The List, maps, set, SortedSet in java (also known as collections) are all interfaces. And everytime you use the collections, the Interface is implemented. In this example, now that you have narrowed it down to <String>, the implementation of the interface "List", stores String.
Now consider the following


The same thing happens, which is the implementation of the interface. Only that list1 stores Lists(which store String).
Hope it helps than making you all the more confused. cheers!!



I really appreciate your response (I honestly mean that). Just to make sure, with <String> I am telling java that of all the data types that you have, I want you to use String.
Correct?

Thanks again

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Weston wrote:with <String> I am telling java that of all the data types that you have, I want you to use String.
Correct?

Sort of. It's actually telling the compiler that the list referred to by this variable can only contain Strings. If the compiler finds a piece of code that tries to add anything else to the list, it should flag up an error.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!