• 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

Difference between the code snippets

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain the difference between the following two code snippets and the outputs produced by them too.



 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<?> can be assigned a List of any type.
But List<T> must be assigned a List of type T. Since we do not know if at runtime, type T will be String or any other type, we cannot return ArrayList<String> from a method that is declared to return List<T>.
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not clear. Please explain the output and also why it results in that output. Also one more code snippet has to be added to the above list :


Please can someone explain the output of these three code snippets alongwith proper explanation. I am quite confused although I think I am clear about the last one but still please.
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code does not produce any output, because there is no main method
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well caught.

Actually I wan't to know whether the codes will compile or throw runtime exceptions. If compiler reports error then at which line and why.

 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code will give compilation error "Type mismatch: cannot convert from ArrayList<String> to List<T>" reason being T might vary at run time and
would not be equal to return type List<T> always, the correct syntax would be as mentioned below



Regards
Salil Verma
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code will give compilation error "Cannot instantiate the type ArrayList<?>" reason being. if meth3 is called from any function,by no means wild character (?) in the return statement would be replaced by a valid object. As we know that wild character (?) does not represents any specific object, the compilation error completely make sense.
The possible correction of this might be as follows -



Regards
Salil Verma

 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code is perfect, runs without any error so I hope it does not require any explanation

Regards
Salil Verma
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As my good friend Jim Yingst used to say:

"Hmmm, if only there was an inexpensive way to find out on my own if a certain bit of code would compile, and if so what the output would be... if only I could find out for myself..."
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic