• 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

 
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 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mate

I get this now
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit and Henry. It all makes sense now
 
Ankit Garg
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops!

thanks Marc and anyone else who has helped
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic