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

Generics question

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Source : Johns mock exam


public static void main( String args[] )
{
List<? extends Number> type = new ArrayList<Integer>(); // 1
for ( Integer n : type ) // 2
{
System.out.println(n); // 3
}
}
public <T> void seth(List<?> type) // 4
{
type.add("hi"); // 5
}

Output is Line 2 and Line 5 have compiler errors.

I understood line 5 but for line 2 this was the explanation

type in main() has to be referenced by a Number not an Integer. So the for in loop fails to compile.


Can anybody explain me the above line?

Thanks All
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Hi All,
Source : Johns mock exam


public static void main( String args[] )
{
List<? extends Number> type = new ArrayList<Integer>(); // 1
for ( Integer n : type ) // 2
{
System.out.println(n); // 3
}
}
public <T> void seth(List<?> type) // 4
{
type.add("hi"); // 5
}

Output is Line 2 and Line 5 have compiler errors.

I understood line 5 but for line 2 this was the explanation

type in main() has to be referenced by a Number not an Integer. So the for in loop fails to compile.


Can anybody explain me the above line?

Thanks All



In line 2, you define the type in the List to be any type that extends Number.

That means that the List could refer to a List containing Integer, but it could also refer to a List containing Double, Float, or other subclasses of Number.

So the only thing guaranteed about the type in the List is that it is a Number.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All you have been given is a list of things of some type "don't know". So how do you know that they are Integers? Any other clues? Yes - these things are all Numbers. Great, so you can at least write:


[ May 03, 2007: Message edited by: Barry Gaunt ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry and kieth i didnt get
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well both Keith and I said basically the same thing.
I suggest that you read though Angelika Langer's Generic FAQ to get a good understanding of Java Generics.
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nik,

In your code "type" contains the elements which extends Number so it can be Integer,Float or Double, so in the same way when you are getting elements out of your collection "type" then also it can be Integer or Float ....
So the only thing that can come out guaranteed from "type" is Number.

Abdul Mohsin
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry for the link and Thanks Abdul and Keith i got it
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic