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

About generics..

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it from,
http://www.javabeat.net/javabeat/scjp5/mocks/Generics-part1-6questions.php


import java.util.*;
class Vehicle {}
class Car extends Vehicle {}
class Bus extends Vehicle {}
class TestSamp {
public static void main(String [] args) {
ArrayList<Car> a = new ArrayList<Car>();
a.add(new Car());
ArrayList b = a;
ArrayList<Bus> c = (ArrayList<Bus> b;
c.add(new Bus());
for (Object obj : b)
System.out.println(obj);
}
}

1.compiler error
2.compiles with warning and gives some output
3.compiles without warning and gives some output
4.copiles and run with no output

Answer they given is: 3

I tried this and when I complied, it showed Note:Recompile with -Xlint:uncheked for details.
is that a warning? i did recompile to get the details and i got 1 warning. if this is true then 2 is the right answer?
please anyone explain me....getting confused.

Preparing Scjp5
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it gives one warning
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the answer is the 2. am i right?
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and one correction it gives two warnings
[ October 27, 2008: Message edited by: ramesh maredu ]
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ramesh maredu
----
Yes and one correction it gives two warnings


I think only one warning for unsafe cast at
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it gives one warning.
so which is the correct one?
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below line also gives warning
ArrayList b = a;
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you compile it?



Here b is declared as unsafe so, no warnings .
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled it in eclipse it is giving me two warnings.The warning is about using non generic version of ArrayList. I think IDE's gives this warning
[ October 28, 2008: Message edited by: ramesh maredu ]
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh..... that's the reason (haven't tried it in eclipse)
[ October 28, 2008: Message edited by: Vijitha Kumara ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic