This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About generics.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About generics.." Watch "About generics.." New topic
Author

About generics..

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
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

Joined: Mar 15, 2008
Posts: 210

yes it gives one warning


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
so the answer is the 2. am i right?
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

Yes and one correction it gives two warnings
[ October 27, 2008: Message edited by: ramesh maredu ]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816


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


I think only one warning for unsafe cast at


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
yes it gives one warning.
so which is the correct one?
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

below line also gives warning
ArrayList b = a;
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

Did you compile it?



Here b is declared as unsafe so, no warnings .
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

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

Joined: Mar 24, 2008
Posts: 3816

Ahh..... that's the reason (haven't tried it in eclipse)
[ October 28, 2008: Message edited by: Vijitha Kumara ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: About generics..
 
Similar Threads
Generics Doubt (Casting)
generic
Warnings during Generic assignments
mock test for generics & collections
DownCast and Generics