• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

GENERIC

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
11. Given:


1. import java.util.*;
2. public class Fruits {
3. public static void main(String [] args) {
4. Set c1 = new TreeSet();
5. Set o1 = new TreeSet();
6. bite(c1);
7. bite(o1);
8. }
9. // insert code here
10. }
11. class Citrus { }
12. class Orange extends Citrus { }

Which, inserted independently at line 9, will compile? (Choose all that apply.)
A). public static void bite(Set<?> s) { }
B). public static void bite(Set<Object> s) { }
C). public static void bite(Set<Citrus> s) { }
D). public static void bite(Set<? super Citrus> s) { }
E). public static void bite(Set<? super Orange> s) { }
F). public static void bite(Set<? extends Citrus> s) { }
G). Because of other errors in the code, none of these will compile.

The answers are A, E, and F.Why?Can someone explain it?Sorry for my english,i'm italian..
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interesting even Set <Integer> s=c1; is compiling fine........
 
Prahlad Joshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in a new thread started by me i got the right answer which am posting here
Case one:

code:


List<Integer> a = new ArrayList(); //warning
a.add(1);
Integer i = a.get(0);
System.out.println(i);



The first line produces a warning.
Because variable a has a generic type List<Integer> and whatever you get out of the list is guaranteed to be an Integer. Therefore the third line will compile and the whole snipped would output a 1.

Why is it allowed to have the ArrayList non-generic here?
Because old classes compiled with java 1.4 or lower still have to work, and an old 1.4 class can easily have an ArrayList as return type.

Case two:

code:


List a = new ArrayList<Integer>();
a.add(1); // warning
Integer i = a.get(0); //
System.out.println(i);



Here a is of raw type. It stores only objects and you can retrieve only objets from that collection. Even if the object is of type ArrayList<Integer>.
Therefore you get now a compiler error in the third line. Because the cast (Integer) is missing.
You have to insert it manually (in comparison to generics on both side, the compiler would do this for you behind the scenes).
And now the second line produces a warning. Compiler also would warn you if the first line were List a = new ArrayList(); (raw type on both sides).
Because only the compile type matters, and it is non-parameterized.
You can store anything into it. A line a.add("howdy"); in this context would compile also.
Again this "half-generics" is also allowed because it must be guarantied that generic and legacy code can interact.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy,

the question itself and/or the answers are wrong.


If the question was: which of them compile?
Then the correct anwer would be:
All of them.

If the question was: which of them compile without a warning?
Then "only A" would be the correct answer.
Because only a collection of unknown (Collection<?> you can assign to a raw type collection without the risk of type unsafety. Doing this way is like using no generics at all:

Answer Z:
public static void bite(Set s) { }
would also compile without warning


From what source does the question comes from?
http://faq.javaranch.com/view?QuoteYourSources



By the way, Antonio:

Welcome to the Ranch!




Yours,
Bu.
 
antonio ciambellari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read it on this site http://www.javabeat.net/javabeat/scjp5/mocks/MockExam02.php
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for providing the source, Antonio!

Perhaps you should better check the given answers through compiling...

Bu.
 
it's a teeny, tiny, wafer thin ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic