Solved

Why is this type bound incorrect?

Posted on 2011-09-18
18
295 Views
Last Modified: 2012-08-13
My textbook says the type bound inside the angled brackets of the code below is incorrect. Then it said adding "extends Comparable" inside the angle brackets is naive. I typed out the paragraph from the textbook. Why does it say this solution is naive?

"Suppose we want to write a findMax routine. Consider the code below. This code cannot work because the compiler cannot prove that the call to compareTo at line 6 is valid; compareTo is guaranteed to exist only if AnyType is Comparable. We can solve this problem by using a type bound. The type bound is specified inside the angle brackets <>, and it specifies properties that the parameter types must have. A naive attempt is to rewrite the signature as:

public static <AnyType extends Comparable>...

This is naive because, as we know, the Comparable interface is now generic."
public static <AnyType> AnyType findMax( AnyType[] arr)
{
     int maxIndex = 0;

     for(int i=1; i<arr.length; i++)
         if(arr[i].compareTo(arr[maxIndex]) > 0)
              maxIndex = i;

     return arr[maxIndex];
}

Open in new window

0
Comment
Question by:shampouya
  • 12
  • 5
18 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
0
 

Author Comment

by:shampouya
Comment Utility
That's a little too complicated for me. I just started working with generic types and I don't know much about compareTo.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
This code compiles without warning because
compilr know that type T implement generic comarable<T> interfcae
so it can use compareTo method insde the method findMax
    public static <T extends Comparable<T>> T findMax( T[] arr)
{
     int maxIndex = 0;

     for(int i=1; i<arr.length; i++)
         if(arr[i].compareTo(arr[maxIndex]) > 0)
              maxIndex = i;

     return arr[maxIndex];
}

Open in new window

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
Comment Utility
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Of course "T" can be everywhere re[laced by "AnyType" as you hasd it before

   public static <AnyType extends Comparable<AnyType>> AnyType findMax( AnyType[] arr)
{
     int maxIndex = 0;

     for(int i=1; i<arr.length; i++)
         if(arr[i].compareTo(arr[maxIndex]) > 0)
              maxIndex = i;

     return arr[maxIndex];
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
If you make it like that (see below; Comparable without <AnyType> as you suiggested in the beginning)
then it will still compile but will give unchecked warning
because compiler will not know that your type AnyType implements Comparable interface
which has method comparing values specifically of AnyType type

    public static <AnyType extends Comparable> AnyType findMax( AnyType[] arr)
{
     int maxIndex = 0;

     for(int i=1; i<arr.length; i++)
         if(arr[i].compareTo(arr[maxIndex]) > 0)
              maxIndex = i;

     return arr[maxIndex];
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

 > naive attempt is to rewrite the signature as
>This is naive because, as we know, the Comparable interface is now generic

"naive" is a nice way of saying "not very informed", "not very smart"

because Comarable should be used in its generic form applied to the same generic type which is mentioned in the declaration,
so they basically meant the same thing, that one should use

public static <AnyType extends Comparable<AnyType>> AnyType findMax( AnyType[] arr)

instaed of this:

public static <AnyType extends Comparable> AnyType findMax( AnyType[] arr)

(see my posts above)






 
0
 

Author Comment

by:shampouya
Comment Utility
Your posts are helpful, but in my mind I don't see the connection between these two things:

1. <AnyType extends Comparable<AnyType>>
2. the AnyType class implements Comparable interface

That's confusing because (1.) uses the word "extends" and (2.) uses the word "implements", why is that? I didn't realize that you could extend an interface. I thought you could only implement.

Also confusing, why is an interface like Comparable given angled brackets and treated like a generic collection? I thought Comparable was just a simple interface, and I didn't think it could be treated like a generic collection.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Comparable<AnyType> is designation of the type ; so AnyType should be the type which extends the type which implements Comparable with respect to AnyType
So it does not extend interface - it extends the type(class) inmplementing interface. in general "T" in generic is substitute for Type
(Type is broader than class - Type may be either instance of class or instance of class implementing Interface).
So there is no class Comparable but there is type Comaparable

generic is by no means  necessarrily Collection; generic can be also method and as  method can be generic, so interface can be generic  

and even class can be generic, see here for example:

http://download.oracle.com/javase/tutorial/java/generics/gentypes.html

Certainly generic is much more general than generic collections, as you see

so in this case AnyType should extend the type which implements generic interface Comparable<AnyType>
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 47

Expert Comment

by:for_yan
Comment Utility
We should not think about generics only in connection with Collections
Collections are just the most famous case

In general any class can be generic if some field in the class has parametrized  type,.

This class is the wrapper arround the instance of any type:

public class Box<T> {

    private T t; // T stands for "Type"

    public void setEntity(T t) {
        this.t = t;
    }

    public T getEntity() {
        return t;
    }
}

Open in new window


Once the class can be generic, so the method can be generic
and interface can be generic

And collections with parametrized type of their elements are just very important and natural cases of generics
0
 

Author Comment

by:shampouya
Comment Utility
Ok that makes sense. The only thing I don't understand now is why Comparable<AnyType> is considered a type and not an interface. Does adding the <AnyType> next to the Comparable interface turn it into a type?
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
No , any interface corresponds to type.
Type is a broader idea than the class.

Type is anything you can use in the declaration of variable.

You can declare variable in the code, e.g.

Comparable c = new String("abc");

Anything you can use in the declaration of varaible is a Type.

So Type can be either class or interface, because interface may be used in order to declare the type of the variable  along with the class.

Therefore Comparable is a Type
and
Comparable<AnyType> is also a Type , just generic type with parameter AnyType



0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

any Comparable represents a Type

Comparable<AnyType> just emphasizes that this is the type for which
the method  compareTo(AnyType o) is defined, where AnyType is the same Type which
is used in the geteneric method  AnyType findMax( AnyType[] arr)




0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

That's why it is important to write

T extends Comparable<T>  (I'm writing T as it is shourter than AnyType and there is obviously no difference)

in declaration:
public static <T extends Comparable<T>> T findMax( T[] arr)

It means that we need type which extends some other type which implements method compareTo(T o)  
where T is the same type which is returned by this method and the same type which array is ion the argument
and the same type which is mentioned in the body of the method
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

If you don't write this way  <T extends Comparable<T>> and just write <T extends Comparable> then compiler cannot be sure
that method
 arr[i].compareTo(arr[maxIndex]) 

Open in new window

is defined because element of array arr  is isntance of T, therefore compiler needs just this way <T extends Comparable<T>> and this
<T extends Comparable> is not sufficient for compiler to be sure that this method is defined

0
 

Author Comment

by:shampouya
Comment Utility
Ok so allow me explain <T extends Comparable<T>> crudely and tell me if I'm basically correct:

"The first T inside the angled brackets allows my method to make use of this generic type T. Then, the Comparable<T> part that we extend, that means that our generic type T inherits the abilities of a type Comparable<T>, which implements the compareTo method specifically using the generic type T. And since we extend that type, we can now use the compareTo method in our findMax method for any type of argument passed into the findMax method."
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility
Yes, I think this is correct
0
 

Author Closing Comment

by:shampouya
Comment Utility
thanks mate
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now