How do you make code like this Generic?
public class Comp {
public static boolean eq(Comparable a, Comparable b) {
return a.compareTo(b) == 0;
}
...
This emits the following warning.
Comp.java:22: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
return a.compareTo(b) == 0;
Think before you post. :^) None of the things that I have tried have worked.
Message was edited by:
billrobertson42
> Think before you post. :^) None of the things that I have> tried have worked.Perhaps you'd do well to post what you have already tried.
I would have, but I deleted it as soon as it didn't work. Sorry.
Have you already tried this?
public class Comp
{
public static <T> boolean eq(Comparable<T> a, Comparable<T> b )
{
return (a.compareTo((T)b) == 0);
}
public static void main(String[] args)
{
Integer a = new Integer(4);
Integer b = new Integer(3);
System.out.println(Comp.eq(a, b));
b = new Integer(4);
System.out.println(Comp.eq(a, b));
}
}
I didn't think about the cast. I'll give it a shot when I get back to my desktop.
What about:
public static <T extends Comparable><? super T>> boolean eq(T a, T b )
{
return (a.compareTo(b) == 0);
}
> What about:
> public static <T extends Comparable><? super
> T>> boolean eq(T a, T b )
>{
>return (a.compareTo(b) == 0);
> }
cool! but I couldn't get that to work, though this did:
public static <T extends Comparable> boolean eq2(T a, T b )
{
return (a.compareTo(b) == 0);
}
Also, I'm not particularily fond of my cast in the prior post. I'm thinking that generics are all about getting rid of casting.
> ...
> public static <T extends Comparable> boolean eq2(T a, T b )
>{
>return (a.compareTo(b) == 0);
> }
I would even say:public static <T extends Comparable<T>> boolean eq(T a, T b) {
return a.compareTo(b) == 0;
}
> Also, I'm not particularily fond of my cast in the
> prior post. I'm thinking that generics are all about
> getting rid of casting.
True.
> I would even say:public static <T extends
> Comparable<T>> boolean eq(T a, T b) {
>return a.compareTo(b) == 0;
>
Thanks! Time for me to read more up on generics.
> ...> Thanks! Time for me to read more up on generics.You're welcome, petes.
> public static <T extends Comparable><? super
> T>> boolean eq(T a, T b )
>{
>return (a.compareTo(b) == 0);
> }
hey friends, i will be pleased if u tell me wat those tags ( dont know if this term is right ) < T > stand for...
or atleast give me link where i can find those
coz i've encountered it sometime b4 but couldn't understand ...
waiting for reply ...
> hey friends, i will be pleased if u tell me wat those
> tags ( dont know if this term is right ) < T > stand
> for...
> or atleast give me link where i can find those
> coz i've encountered it sometime b4 but couldn't
> understand ...
> waiting for reply ...
Google: java generics tutorial
You'll get lots of helpful links. One near the top that I like:
http://www.stanford.edu/class/cs108/handouts062/03JavaGenerics.pdf
Ok, from post 3 we have:
public static <T> boolean eq3(Comparable<T> a, Comparable<T> b ) {
return (a.compareTo((T)b) == 0);
}
Comp.java:23: warning: [unchecked] unchecked cast
found: java.lang.Comparable<T>
required: T
return (a.compareTo((T)b) == 0);
I think I had tried this one last night, from Post 6...
public static <T extends Comparable> boolean eq6(T a, T b ) {
return (a.compareTo(b) == 0);
}
Comp.java:27: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
return (a.compareTo(b) == 0);
And finally, from post 7, with no warnings...
public static <T extends Comparable><T>> boolean eq7(T a, T b) {
return a.compareTo(b) == 0;
}
Thanks all!
> Ok, from post 3 we have:
>
> public static <T> boolean eq3(Comparable<T>
> a, Comparable<T> b ) {
> return (a.compareTo((T)b) == 0);
>
> Comp.java:23: warning: [unchecked] unchecked cast
> found: java.lang.Comparable<T>
> required: T
> return (a.compareTo((T)b) == 0);
>
You are wise enough to ignore this piece of cr鈖, correct? prometheuzz is your man.
Relax! He was just trying to help. :)Yes, I could see the right answer. :) I just wanted to take the examples that compiled and summarize the what the result.Anyway, thanks for the feedback and the link.
> Relax! He was just trying to help. :)Oh, I'm relaxed. I'm just sort of embarrassed that my answer was the piece of crp.but ya gotta learn somehow.
At least it compiled, and it was an interesting thought.