• 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

Generics Question - Reference type

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

Question from whizlabs mock exam:



I thought that since line 1 is declared as an <Integer> type, then line 4 should get autoboxed. However the correct answer is that the variable 'v' returns and Object and there is a compiler error on line 5.

Now if this is the case then what is the point in the declaration in line 1? If its an object type then I could modify the above code to do this:




The abve code compiles as you can add anything to the vector (although there are some warnings)

Can anyone explain what line 1 actually achieves?


Thanks
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 1 actually achieves to show the dangers of mixing generic types with non-generic types. As you yourself have shown that you could add a String, Object and so on to a Vector which is actually supposed to refer to a Vector of type Integers only.
This is the price that sun engineers thought of paying in order to introduce generic types in java and not breaking the old non-generic type code which would be interacting with the new generic type code.
Hence, when mixing non-generic with generic code it's the sole responsibility of the developer to avoid any runtime gotchas.

Hope it makes sense.
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, so its basically there to interact with legacy code.

If I changed the code to :




Then the compiler will complain about adding a String object to the vector.

Although I have a another query, is


the same as:



They both achieve the same goal - which is to ensure only Integer objects (or int primitives) are added to the vector. However the first statement will give a warning.

Are both declarations equal?



Thanks
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all you must know that generics is just a compile time check which saves you from using the wrong types in the wrong places.

ArrayList<Integer> al = new ArrayList();

and

ArrayList<Integer> al = new ArrayList<Integer>();

means the same thing. The compiler warns you on the first one as things might be more complicated then just that. Let's take an example



So basically when you assign an un-typed list to a typed one, the compiler warns you that it cannot assure you know that you are not entering a wrong value into the collection...
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic