Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Question

 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate

I get this now
 
suresh pilakal babu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
suresh pilakal babu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit and Henry. It all makes sense now
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops!

thanks Marc and anyone else who has helped
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic