• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

doubt generics

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , can any one explain this code , this is from generics from k& b book

public class Test
{
static void addandDisp(List list)
{
list.add(987654321);
list.add(12.345);
for(Object e:list)
{
System.out.print(e.getClass()+" ");
}
}
public static void main(String[] args)
{
List<String> ls = new ArrayList<String>();
ls.add("Java");
addandDisp(ls);
}
}

What will be the output?

a)Prints class java.lang.String class java.lang.long class java.lang.Double
b)Prints class java.lang.String class java.lang.Integer class java.lang.Double
c)Prints class java.lang.String class java.lang.Double class java.lang.Float
d)Compile time Error
e)Run time Error
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The answer is b.

Use generics is not a must... but this can cause some problems.

Your method does not use generics so you will be able to add any object to your collection.
But as your original variable uses generics it will assume that all objects inside it is a String.. that can cause a ClassCastException in some calls of the list methods.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Further to the above.

I'm fairly certain that you will get a compile time warning (as the compiler will see that you're passing a Genericised list into a Legacy style function).

You are expected to have a good understanding of the sort of scenarios that will cause errors and warnings for the exam when using generics.

I *strongly* suggest you paste the above code and compile it and satisfy yourself that you can see why you get what you get. I then suggest that you go through a series of "So that means if I change this line to blah, blah, blah I'll get such and such a response", then do the change and check your understanding (further refining your knowledge if you figured an incorrect result). Repeat that exercise for a whole host of changes to the above code (and lots of other code). Get to the stage where you're confident that you see some code and know the response.

Sorry, but some hard graft and good broad *knowledge* is required.

NB: I went into the exam expecting a gentle introduction with the first few questions, but WHAM!!!, they hit me with some taxing generics stuff that knocked me back a bit. I took it on the chin and ploughed through, some introduction :-( .
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by S J Martin:
I'm fairly certain that you will get a compile time warning (as the compiler will see that you're passing a Genericised list into a Legacy style function).

No. You will not get a warning at this point.
 
S J Martin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Klug:
No. You will not get a warning at this point.



Ok, I was /nearly/ right. The above code will generate a warning - but on the list.add(...) lines within the addandDisp method.

Zero marks for "Nearly right" though :-(
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
S J Martin was fairly certain


that you will get a compile time warning (as the compiler will see that you're passing a Genericised list into a Legacy style function).



These kinds of warning occur when you assign a generic variable to a "legacy object":

but only a warning, the code compiles and prints null twice.

and S J wrote:

Zero marks for "Nearly right" though :-(




One fish of comfort for "nearly right" from me:


Yours,
Bu.
 
S J Martin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:
These kinds of warning occur when you assign a generic variable to a "legacy object":



Indeed, and the compiler would see the call to addandDisp as "Legacy" as there was no generics decorator. Then, for those lines that add items to the list, warnings will be generated.
 
I think she's lovely. It's this tiny ad that called her crazy:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic