File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt i a generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt i a generics question" Watch "Doubt i a generics question" New topic
Author

Doubt i a generics question

Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 60
Hello all,

I was solving this question from generics.

import java.util.*;
public class ListTest
{
public static Iterator reverse(List list) #1
{
Collections.reverse(list);
return list.iterator();
}
public static void main(String... p)
{
List list= new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
for(Object o: reverse(list))
System.out.println(o+" ");
}
}

I have few doubts in this.
The compilation(normal) shows warnings and suggest recompilation with -Xlint. Following generated as result of recompilation with -Xlint.

Output


D:\Education\Java\JavaStudyRoom>javac -Xlint ListTest.java
ListTest.java:12: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
list.add("1");
^
ListTest.java:13: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
list.add("2");
^
ListTest.java:14: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
list.add("3");
^
ListTest.java:15: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
list.add("4");
^
ListTest.java:16: foreach not applicable to expression type
for(Object o: reverse(list))
^
1 error
4 warnings


Here,
First, I dont understand why do I get warning with add(), I am using pre generics syntax completely, so why does it gives me warning???
Second,Regarding error I understand completely the reason, that since for loop syntax is violated, therefore I get the error. But I was really expecting another error.
In line public static Iterator reverse(List list)

are we not trying to copy the name of a method which is predefined? So isnt this a violation???

Please clear my doubts

Thanks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

<edit>Use code tag, while posting code</edit>

Suvojit Chakraborty wrote:


warnings are not errors.if you apply generic, it will disappear.

and syntax of enhanced for loop is
Collection extends Iterable interface so, you can use List.
modify your reverse code as below



hth

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hi, Suvojit Chakraborty

public static Iterator reverse(List list) -- This method return an iterator object, not Iterable, That is the problem. Warning will come if you JVM5 or after versions, but those are not problem.


Suvojit Chakraborty wrote :
..... But I was really expecting another error. In line public static Iterator reverse(List list)
What do you expect?

UseCodeTag please.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 60
warnings are not errors.if you apply generic, it will disappear.


Warning will come if you JVM5 or after versions, but those are not problem.


Okay, but are you guys trying to say, adding in a collection which is not typesafe and is pre-generics will always give a warning in Jdk5+ compilers??? What I read in K&B book is that when we try to mix Generics with non generics and attempt to add something in a typesafe collection which is being passed to a method that accepts a non-generic collection; we get a warning as the method(which accepts a non-generic collection) has no respect for the type safety of our typesafe collection and can add wrong things into it. So we get a warning.
Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 60


Suvojit Chakraborty wrote :
..... But I was really expecting another error. In line public static Iterator reverse(List list)
What do you expect?



What I meant was, we already have a definition for reverse()[Collections.reverse()] which is a static method. Are we not trying to redefine it in the program. So isnt this act of violation???Or is it okay to redefine the pre-defined methods(ie overwrite them) or oveload them(like in this case)?(I found the following in the Sun API for coll:

public static void reverse(List list)

)

So I was expecting a compiler error in Line#1....
I hope I made myself clear now......I am really confused.....
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

You are free to overload or override the methods unless they are fulfilling the overloading/overriding contract.
One more thing - Here in your example you are not extending any class so there will not any overriding. Overriding in only possible in case of inheritance.

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

If you extend that particular class and if the method is not final, then you are free to overload/override. If you not extend, then no problem, because class is template for a particular objects.

Class name, some time gives error in this case! - conditions applies!
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
I don't understand why do I get warning with add(), I am using pre generics syntax completely, so why does it gives me warning???




I think it may be to do with the fact that you are adding your list to the Collections.reverse() method. If you look at the API for the Collections class the reverse method signature is: public static void reverse(List<?> list) This being so, means that whilst you may certainly add your pre generic list, you will, none the less, see this warning as you are adding non generic code to a generic method.

I may be off on this, but that's my take on this issue


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
[b]public static void reverse(List list)


No, you are mistaken, look at the Java 5 API, the signature is actually public static void reverse(List<?>list)

Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 60
Stephen Davies wrote:
[b]public static void reverse(List list)


No, you are mistaken, look at the Java 5 API, the signature is actually public static void reverse(List<?>list)



Oh yes! you are rite, I was referring to j2se4 API:
Java4API
but now with generics it looks like:
Java5Api

and yes your explanation seems logical.....

But guys I still have not got the answer of the question
What I meant was, we already have a definition for reverse()[Collections.reverse()] which is a static method. Are we not trying to redefine it in the program. So isnt this act of violation??


I guess, I fail to make my question clear...
So one more time,

In the code we have following statement:


which is actually a predefined static method of Collections class.

But we also have following in the code:


and the call to reverse(which is actually calling ListTest's static reverse())


I am clear with the fact that since for loop syntax violation happens here, so we get the compilation error,
But what I really dont understand; is it ok to have user defined method reverse() in the code when we already have a reverse() in Collections ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt i a generics question
 
Similar Threads
Generics: warnings
Problem with Generics
Warnings after compiling program using Generics
collections doubt
Need Help on Generecs and non-Generics