Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic