Results 1 to 4 of 4
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Generics CAP#1 argument mismatch error

    When I was testing generics; I found a problem that I find hard to solve.

    This is the code that I was testing.

    Java Code:
    interface compNum<T extends Number>
    {
    	T min(T numOne,T numTwo);
    	T max(T numOne,T numTwo);
    }
     
    class CustComp<T extends Number> implements compNum<T>
    {	
    	
    	@Override
    	public T min(T numOne,T numTwo)
    	{
    		
    		if(numOne.doubleValue() < numOne.doubleValue()){return numOne;}
    		else{return numTwo;}
    		
    	}
    	
    	@Override
    	public T max(T numOne,T numTwo)
    	{
    		if(numOne.doubleValue() < numTwo.doubleValue()){return numOne;}
    		else{return numTwo;}
    	}
    	
    }
    
    public class Test
    {
    	
    	public static void main(String[]args)
    	{
    		CustComp<?> ccr = new CustComp<Double>();
    		System.out.println(ccr.min(new Double(13.4),new Double(14.4)));
    	}
    }
    And this was some of the error I had had got

    Java Code:
    required: CAP#1, CAP#1
    found: Double, Double
    reason: argument mismatch;Double cannot be converted to CAP#1 where T is a type-variable

    Luckily, I found a workaround but it required me to reform my code.
    My question is, is there a workaround to fix this problem without reforming some significant parts of the code e.g. removing min and max parameters.

    I found some Java documentation that If I encounter that kind of error I should use a helper method
    https://docs.oracle.com/javase/tutor...s/capture.html

    But, I can't find a way to implement a helper method to this code.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,964
    Rep Power
    7

    Default Re: Generics CAP#1 argument mismatch error

    Why don't you use it like this?
    Java Code:
    CustComp<Double> ccr = new CustComp<>();
    System.out.println(ccr.min(13.4, 14.4));
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Re: Generics CAP#1 argument mismatch error

    I'm just testing if I can compare multiple Objects by using that CustComp class

    For example

    CustComp<?> ccr = new CustComp<Double>();
    System.out.println(ccr.min(new Double(13.4),new Double(14.4)));

    ccr = new CustComp<Float>();
    System.out.println(ccr.min(new Float(13.4),new Float(14.4)));


    ccr = new CustComp<Integer>();
    System.out.println(ccr.min(new Integer(13),new Integer(14)));

    I know it looks silly and there are many workarounds to achieve it. I'm just intrigue with the error.
    Maybe this code is fundamentally wrong in the first place. I just want to know if there's a possible
    fix with this.

    Regards,

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,189
    Rep Power
    12

    Default Re: Generics CAP#1 argument mismatch error

    The wildcard ? means unknown type. So you can't assign or use it to operate on a specific type. You may, however,
    assign a type of ? to a type Object and use the methods in the Object class. This is because ALL objects are sub types of
    class Object.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 8
    Last Post: 09-22-2013, 12:20 AM
  2. Mismatch error thrown?
    By Teclis in forum New To Java
    Replies: 16
    Last Post: 04-19-2011, 02:58 AM
  3. Generics Problem (Bound Mismatch)
    By castiel in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 12:05 AM
  4. Type Mismatch error
    By and0rsk in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 11:16 AM
  5. Invalid Argument error in IE7
    By Kaviurs in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-01-2008, 09:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •