• 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: mock exam question

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

I came across this question in Inquisition (John Meyer) and the explanation is a bit questionable to me.
This is the code:

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
}

The answer says "Lines 2 and 5 have compilation errors". with the following explanation:
You can't add anything to a List reference that has a "?" ( unless it has a super keyword followed by a class name ) and type in main() has to be referenced by a Number not an Integer. So the for in loop fails to compile.

But I think it is lines 1 and 5.
Because line-1 cannot compile. I tried it and gave an "incompatible types" error. And also this is one of the most stressed out points in the K&B book. The reference type and the object type MUST BE THE SAME.
So there is no way "List<? extends Number> type = new ArrayList<Integer>();" can compile in my understanding.

Am I getting this wrong or there is a mistake in the question?

Thank you.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line marked 2 will give compiler error because the List can contain anything Number such as Float, Double etc. So not all the elements can be read as Integer.

Line marked 5 will give compiler error because we are to use a List when type of its elements is still a 'question mark' for the compiler.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
But the problem is, line-1 doesnt seem to compile either.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also have another question:

In the following method:
public static <T> List<T> method(List<? super Integer> type)
{
System.out.println(type.add(3));
return new ArrayList<T>();
}


The complete way of invoking this method is:
ClassName.<Integer>method(IntList);

In this case T=Integer gets assigned. assuming IntList is of type Integer.

But if I invoke the method as:
method(IntList);

What does the type T gets assigned to? how would I know which type the returned ArrayList wil be?

Thank you.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that you got a ERROR.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maduranga Liyanage wrote:Thank you.
But the problem is, line-1 doesnt seem to compile either.



Why not? It looks like a valid assugnment to me.

Henry
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops.
Sorry. Actually I have done <? super Number>. 'extends' work.
Sorry for the mistake. It compiles fine.

Would appreciate a help on the Generic method.
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maduranga Liyanage wrote:Oops.
Sorry. Actually I have done <? super Number>. 'extends' work.
Sorry for the mistake. It compiles fine.

Would appreciate a help on the Generic method.



Well, the compiler should try to find a T, within the limits of any bounding, that will match the requirements of the parameters -- and the return type that you need. In your example, since T is not bounded, and it is not used in the paramters, you should be able to assign it to any List type that you want.

Henry
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry.
I tried it with Integer and String in place of <T> but it doesn't seem to work.
GIves me:


So I guess I need to explicitly mention <T> in the method invocation.
 
Have you no shame? Have you no decency? Have you no tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic