Help, Want to make a BASIC min value Method with Generics

What is wrong with the following?

Code Java:

public static <T> T Min(final T Value1, final T Value2)
{
return((Value1 < Value2) ? Value1 : Value2);
}

I am just trying to make a simple generic method so that I can do Min(1,2) and have it return the smaller of the two values (in this case 1). When compiling, I get this:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

The operator < is undefined for the argument type(s) T, T

How do I best do this?

I know in C using templates I can simply do:

Code C:

template <typename T> T Min(const T &Value1, const T &Value2)
{
return((Value1 < Value2) ? Value1 : Value2);
}

How do I do this in Java? All the examples I see use "Collections", but I don't. I just want to use simple Integers.

Re: Help, Want to make a BASIC min value Method with Generics

I think The < operator will only work for primitives. For classes you need to use a method.

Re: Help, Want to make a BASIC min value Method with Generics

Quote:

Originally Posted by

**Iglesias**
I just want to use simple Integers.

Why use generics then? Do you want to compare primitives or objects? To compare intergers, just pass primitives:

Code java:

int min(int val1, int val2){
return val1 < val2 ? val1 : val2;
}

Or just use Math

Comparing two objects via greater or less than should result in a compile time error (the meaning of greater than on an object doesn't make sense). If you have variable classes that hold a value you wish to compare, define an interface that those objects implement and use that as the parameter

Re: Help, Want to make a BASIC min value Method with Generics

I want to compare two objects, but they could be Double or they could be Integer or Float. I want to use T instead of Integer, Double, or Float. I am hoping that I could just use T... syntactic error I hope?

I know int, double would not work. So I would use the "wrapper" for these: Integer and Double.

Re: Help, Want to make a BASIC min value Method with Generics

Just use the Math class

Code java:

int i1 = 5;
int i2 = 10;
float f1 = 1.0;
float f2 = 5;
double d1 = 1;
double d2 = 10;
System.out.println(Math.min(i1,i2));
System.out.println(Math.min(f1,f2));
System.out.println(Math.min(d1,d2));

Java also has something called autoboxing, where primitive wrappers and their primitives can be used interchangably

Re: Help, Want to make a BASIC min value Method with Generics

I sort of wrote a program in C and wanted to convert it to Java. This method happens to be a part of the library of functions that I use, so I am hoping to convert my existing "legacy" code into a Java version, and switch to the Math.min() later. Does Java not have a direct syntactically similar way to convert from the following C code?:

Code C:

template <typename T> T Min(const T &Value1, const T &Value2)
{
return((Value1 < Value2) ? Value1 : Value2);
}

Re: Help, Want to make a BASIC min value Method with Generics

You could rely on the fact that the primitive wrappers all implement the Comparable interface...