wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Question " Watch "Generics Question " New topic
Author

Generics Question

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Hi all,

Question from Whizlabs SCJP5:

Vector v = new Vector<Integer>(); //Line 1
v.add(1);
v.add(2);
v.add(3);

for(int i: v) //Line 2
System.out.print(i);


The above code will not compile as 'v' returns an Object which cannot be cast to an int or Integer on line 2.

If line 1 was changed to Vector<Integer> v = new Vector(); or Vector<Integer> v = new Vector<Integer>(); the code will work.

Can anyone explain why Vector v = new Vector<Integer>(); returns an object as i believed since v is set to <Integer> it should return an Integer object?


Thanks


Santiago
My Path to SCJP Certification My Path to SCWCD Certification
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Vector v declares a variable 'v' that has the type Vector (not Vector<Integer>). The compiler treats this as any other legacy (pre-generics) Vector, so anything you get anything out of 'v' will be a reference of type Object, and you need to explicitly downcast to the actual type.

When you declare the variable using generics, Vector<Integer> v, this tells the compiler that it needs to do some extra work. In this case, when you get something out of 'v' the compiler knows that it's (supposed to be) an Integer, and it inserts the downcast for you behind the scenes.

(Edit: Disabled smilies.)
[ November 05, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Thanks mate

I get this now
suresh pilakal babu
Ranch Hand

Joined: Jul 01, 2008
Posts: 31
What is the use of specifying <Integer> after = .

Vector v = new Vector<Integer>();

I mean both
Vector<Integer> v = new Vector<Integer>();
and
Vector<Integer> v = new Vector();
are same?


Thanks
Suresh
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by suresh pilakal babu:
...I mean both
Vector<Integer> v = new Vector<Integer>();
and
Vector<Integer> v = new Vector();
are same? ...

When you try to compile these, you will see the difference.
suresh pilakal babu
Ranch Hand

Joined: Jul 01, 2008
Posts: 31
I know the statment

Vector<Integer> v = new Vector();

will give warning message(not type safe operation)

Marc.. this is the only difference?

I don't think so..anybody else can answer it..?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

see this code



The exception came because the compiler automatically placed a cast there

Integer i = (Integer)typed.get(0);

So basically if you don't use Vector<Integer>, then the compiler will not insert any cast, and you will have to cast yourself



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
suresh pilakal babu
Ranch Hand

Joined: Jul 01, 2008
Posts: 31
Ankit,

According to you below code should produce Compiler warning and Classcast Exception

Vector<Integer> v = new Vector();
v.add(10);
Integer i = v.get(0);

I am geting compiler warning but its executing fine ..no classcast exception
[ November 08, 2008: Message edited by: suresh pilakal babu ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by suresh pilakal babu:
...I am geting compiler warning but its executing fine ..no classcast exception...

Here's one way it could happen. The following code compiles with the warning, but fails at runtime.
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Thanks Ankit and Henry. It all makes sense now
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Originally posted by Santiago Bravo:
Thanks Ankit and Henry. It all makes sense now


Where's Henry in this thread. Marc helped you my dear...
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Whoops!

thanks Marc and anyone else who has helped
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Question
 
Similar Threads
How to write generic method for this method?
Generics Question - Reference type
Array problem
Can vectors only hold objects?
Using add on Vector<?>