Results 1 to 3 of 3
  1. #1
    airkiller1 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default A perplexing problem about"Bounded Type Parameters"Help!

    As Java Tutorial wrote,
    "Consider the following method that counts the number of elements in an array T[] that are greater than a specified element elem." Finally,it led to the following code snippet:

    public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
    int count = 0;
    for (T e : anArray)
    if (e.compareTo(elem) > 0)
    ++count;
    return count;
    }

    Please note the Bounded Type Parameter-"T extends Comparable<T>",if I change it into "T extends Comparable"(ie.without <T>),compiler will generate a warning message saying that raw type is used.
    But when I try to practise the way mentioned above,I get some surprising results which turn out quite opposite.Please see my attempt:
    First,I declare an generic interface "Measurable" which includes two abstract methods:

    public interface Measurable <T>{
    public boolean isPositive();
    public T retrieve();
    }
    Later,I define a generic class to implement that interface.
    class AnyNumber <T extends Number> implements Measurable<T>{

    //this class only holds one field to store the generic type
    T value;

    //Generic Constructor,to finish a simple assignment
    public AnyNumber(T value){
    this.value = value;
    }

    //implement the interface!
    public boolean isPositive(){
    //doubleValue() is a common method(abstract) in Class Number shared by all its SubClasses
    if(value.doubleValue() > 0)
    return true;
    else
    return false;
    }

    public T retrieve(){
    return value;
    }
    }

    At last,as Tutorial told me,I declare a generic method that ONLY accpet the generic interface type,in my case,"Measurable<T>".Right after,I'd like to believe any classes that implement "Measurable<T>" could use this method.

    So,I try it out with the following snippet:

    public class Test{
    public static <T extends Measurable<T> , U extends Measurable<U>> boolean isBothPositive(T t , U u){
    if(t.isPositive() && u.isPositive())
    return true;
    else
    return false;
    }

    public static void main(String[] args){
    byte a = -1;
    float b = 10.625f;
    AnyNumber<Byte> an1 = new AnyNumber<>(new Byte(a));
    AnyNumber<Float> an2 = new AnyNumber<>(new Float(b));
    if(an1 instanceof Measurable && an2 instanceof Measurable){
    System.out.println(an1.retrieve());
    System.out.println(an2.retrieve());
    System.out.println("is both positive? "+ isBothPositive(an1,an2));
    }else{
    System.out.println("Something is wrong!");
    }
    }
    }

    Unfortunately,it won't even compile,but after removing "<T>",leaving "U extends Measurable" behind,it compiles and no warnings!The result is ideal.
    What's the problem that cause that to happen,I can't figure it out after long time trying.
    Hope someone could fix it,Many THX!

    The error message is:
    G:\>javac Test.java
    Test.java:17: error: method isBothPositive in class Test cannot be applied to given types;
    System.out.println("is both positive? "+ isBothPositive(an1,an2));
    ^
    required: T,U
    found: AnyNumber<Byte>,AnyNumber<Float>
    reason: inferred type does not conform to declared bound(s)
    inferred: AnyNumber<Byte>
    bound(s): Measurable<AnyNumber<Byte>>
    where T,U are type-variables:
    T extends Measurable<T> declared in method <T,U>isBothPositive(T,U)
    U extends Measurable<U> declared in method <T,U>isBothPositive(T,U)
    1 error



  2. #2
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: A perplexing problem about"Bounded Type Parameters"Help!

    T and U are both classes declared in method isBothPositive and are subclasses of the Measurable<T> and Measurable<U> Interface. For the method isBothPositive(T,U), objects of type or a subclass of type T and U are expected. What is actually passed is two objects of type AnyNumber<Byte> and AnyNumber<Float>. T and U are both subclasses of the Measurable class, and so is AnyNumber, meaning they are sibling classes. Sibling classes can not be referred to by one another's class pointer. So for example, Sibling1 s = new Sibling2(); is not valid code. You can refer to them by their parent, or superclass.

    That seems to be the issue, to me.

  3. #3
    airkiller1 is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: A perplexing problem about"Bounded Type Parameters"Help!

    Quote Originally Posted by christopherx View Post
    T and U are both classes declared in method isBothPositive and are subclasses of the Measurable<T> and Measurable<U> Interface. For the method isBothPositive(T,U), objects of type or a subclass of type T and U are expected. What is actually passed is two objects of type AnyNumber<Byte> and AnyNumber<Float>. T and U are both subclasses of the Measurable class, and so is AnyNumber, meaning they are sibling classes. Sibling classes can not be referred to by one another's class pointer. So for example, Sibling1 s = new Sibling2(); is not valid code. You can refer to them by their parent, or superclass.

    That seems to be the issue, to me.
    Thx for your reply.You're totally right!

    I figure it out when I check the error message once again.
    The problem is :

    1,while defining a generic inteface,you are not defining a specific interface for a specific type,instead you are designing a bunch of interfaces(protocols) that suit different types.

    2,It's not precise to say Class AnyNumber<T extends Number> implements the interface Measurable<T>.In fact,It ONLY implements some of the interface that suits condition:<T extends Number>.

    3,The bounded type parameter-<T extends Measurable<T>> means T should at least implement Measurable<T> interface.When T is assigned a self-created Class TypeAnyNumber<T>,T changes its meaning formerly used because it no longer satisfies <T extends Number>.
    Moreover,when T is represented by TypeAnyNumber<T>,you get <TypeAnyNumber<T> extends Measurable<TypeAnyNumber<T>>.Of course,compiler won't pass because no such a class has ever implemented interface Measurable<TypeAnyNumber<T>>
    In fact,It is interface Measurable<T> not Measurable<TypeAnyNumber<T>> that Class TypeAnyNumber<T> implements.So,<T extends Measurable> will compile for the reason that it is equivalent to <T extends Measurable<? extends Object>>.In that case,T needs only to implement one of the all interfaces Measurable<T> can define.
    Under that same reason,<T extends Measurable<? extends Number>> is also OK.

    4,As for the code snippet mentioned on the Official Java Tutorial:
    public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
    int count = 0;
    for (T e : anArray)
    if (e.compareTo(elem) > 0)
    ++count;
    return count;
    }
    It works fine for primitive type objects because they all implement Comparable<T> in java.lang package at the very beginning.When you create a new class based on these primitive type classes,the new class itself must also implement interface Comparable<T> so that its object can utilize the methods within.Or else ,as formerly shown,your new class TypeAnyNumber ONLY partially makes primitive type objects Byte&Float "Measurable".However,you can't even expect your methods to accpet these object,unfortunately,none of them,according to the officially provided documentations,explictly implements your self-made interface Measurable<T>!

    Thx again,Anyone has a different idea can post it straight!Your opinion matters.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. "double" data type problem
    By jackripley in forum New To Java
    Replies: 2
    Last Post: 11-25-2011, 10:00 AM
  4. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 08:50 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •