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

[470 byte] By [billrobertson42a] at [2007-11-27 8:38:40]
# 1
> 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.
petes1234a at 2007-7-12 20:36:26 > top of Java-index,Java Essentials,Java Programming...
# 2
I would have, but I deleted it as soon as it didn't work. Sorry.
billrobertson42a at 2007-7-12 20:36:26 > top of Java-index,Java Essentials,Java Programming...
# 3

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));

}

}

petes1234a at 2007-7-12 20:36:26 > top of Java-index,Java Essentials,Java Programming...
# 4
I didn't think about the cast. I'll give it a shot when I get back to my desktop.
billrobertson42a at 2007-7-12 20:36:26 > top of Java-index,Java Essentials,Java Programming...
# 5

What about:

public static <T extends Comparable><? super T>> boolean eq(T a, T b )

{

return (a.compareTo(b) == 0);

}

FAMUGamblera at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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.

petes1234a at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 7

> ...

> 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.

prometheuzza at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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.

petes1234a at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 9
> ...> Thanks! Time for me to read more up on generics.You're welcome, petes.
prometheuzza at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 10

> 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 ...

manishmulani@nitka at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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

petes1234a at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 12
thanx a lot ..petes1234
manishmulani@nitka at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 13

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!

billrobertson42a at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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.

petes1234a at 2007-7-12 20:36:27 > top of Java-index,Java Essentials,Java Programming...
# 15
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.
billrobertson42a at 2007-7-21 22:44:31 > top of Java-index,Java Essentials,Java Programming...
# 16
> 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.
petes1234a at 2007-7-21 22:44:31 > top of Java-index,Java Essentials,Java Programming...
# 17
At least it compiled, and it was an interesting thought.
billrobertson42a at 2007-7-21 22:44:31 > top of Java-index,Java Essentials,Java Programming...