Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

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

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    What is wrong with the following?
    	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:
          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.
    Last edited by helloworld922; September 12th, 2010 at 11:15 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,439
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

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

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

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

    Quote Originally Posted by Iglesias View Post
    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:
    int min(int val1, int val2){
    return val1 < val2 ? val1 : val2;
    }
    Or just use Math
    Math.min(1,2)
    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

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by Iglesias; September 12th, 2010 at 09:22 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

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

    Just use the Math class
    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
    Integer i1 = new Integer(5);
    Integer i2 = new Integer(10);
    System.out.println(Math.min(i1,i2));

  6. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?:
    template <typename T> T Min(const T &Value1, const T &Value2)
    {
    return((Value1 < Value2) ? Value1 : Value2);
    }
    Last edited by helloworld922; September 12th, 2010 at 11:16 PM.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default 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...
    public static Comparable min(Comparable c1, Comparable c2){
    	return c1.compareTo(c2) < 0 ? c1 : c2;
    }

Similar Threads

  1. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM
  2. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 AM
  3. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 01:25 AM
  4. Implementing Multiple Interfaces with Generics
    By darkestfright in forum Collections and Generics
    Replies: 5
    Last Post: February 10th, 2010, 07:44 PM
  5. Identify and avoid some of the pitfalls in learning to use generics
    By JackyRock in forum Java Theory & Questions
    Replies: 0
    Last Post: February 6th, 2010, 04:12 AM