It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Question - Reference type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Question - Reference type" Watch "Generics Question - Reference type" New topic

Generics Question - Reference type

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
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?


My Path to SCJP Certification My Path to SCWCD Certification
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
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

Joined: Jul 25, 2008
Posts: 226
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?

Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

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();


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...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: Generics Question - Reference type
It's not a secret anymore!