• 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

Doubt i a generics question

 
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Suvojit Chakraborty
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.....
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

[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
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic