• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Legacy generic and generic.

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone,

I am confused with the legacy code in generic type:




is this statement valid in two cases:
1) list is filled with 'string'
2) list is filled with 'string' and other.


I have read the kathy sierra book but it didn't mention? (or got?).
reversely i can understand which is :



the above statement is obsolutely valid right? but adding of element will cause the warning. am i right ?

Thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jamil. Welcome to The Ranch!

Because of type erasure (that is, the generic information does not exist at run time), everything depends on the reference type of the variable being used. If it's an ArrayList<String>, then you'll only be able to add Strings to it. If it's just an ArrayList, then you can add any object.

So:is valid, but pointless, as it provides no protection. You can add anything to list without any errors at all.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew one question with this....
Does that means I can add only single type of value in that ArrayList?
Or can add multiple kind of Objects like String, Number, Float, Object all together?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the variable type is ArrayList<T>, you can only add T objects (or instances of subclasses of T) - whatever T is. But as soon as you have a variable of type plain ArrayList, you can add any object you want.

Here's an example:
That will compile fine. Line 4 is allowed, because list is a plain ArrayList type. The loop on line 6 is allowed, because listStr has the generic type String.

Now try running it. Line 4 is still OK. Because of type-erasure, it doesn't matter that the list was created as an ArrayList<String> - there's no protection at run time. But since line 6 involves taking a value out and assigning it to a String variable, you'll get a ClassCastException at that point.
 
jamil lusa
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Matthew Brown, thanks your reply. but you didn't catch my question.

how about the first case which is mentioned:


add string to 'abc' list, add integer to 'abc' list, and so on .....

after that i assign this legacy list into generic list which is like thsi:


is the above statement syntaxticallly correct?
 
Greenhorn
Posts: 8
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics were added with version 5. They are just compile-time. At realtime, there doesn't exist any such thing as a generic.

will compile and work fine... and you can add any Object to the list.
But there will be compiler warnings.
 
jamil lusa
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am i asking something which is very confusing???

why people answer me wrongly agian and again??? i already understand this question : ArrayList list = new ArrayList<String>();

what i asked is the reverse assignment which is assign Legacy list into Generic List.

I think i should edit my question again.
 
Bhaskar Chakraborty
Greenhorn
Posts: 8
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I am sorry. But there existed some kind of ambiguity in your original question.

Yes,

is totally valid. But, again... there are going to be compiler warnings.
The only difference is that, this time you can add ONLY String objects to the list, because it depends on the declared type.
 
jamil lusa
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Bhaskar Chakraborty, thanks your reply.

sorry i was just frustrated that could not get the answer.

my office computer does not have java compiler so i could not test the program, that is why i have to ask the question here.

from thsi statement:



i wonder what will happen because the 'list' is having some elements which are not String.

when we retrieve the non-string elements, we will be getting runtime error or compile error?

case 1:
.

above statement will cause compiler error. I guess. (no compiler cannot try><)




this case 2 is confused, am i going to obtain ClassCast runtime error? but i didn't cast it. so ? hmmm..... any ideas?


anyway, wish everyone happy coming weekend. (i am very curious to test it but weekend is not programming day, haha).
 
Bhaskar Chakraborty
Greenhorn
Posts: 8
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the code snippet:


A ClassCastException will be thrown at runtime.

Have a nice day.

PS: Consume your anger, before it consumes you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic