7 Replies - 3293 Views - Last Post: 25 December 2008 - 12:20 PM Rate Topic: -----

#1 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Collections.binarySearch return type

Post icon  Posted 24 December 2008 - 03:44 PM

The method detail in java-doc for Collections.binarySearch gives the return type as:

public static <T> int

Why? Is it an int, or not? If it's an int, it seems that no template is involved, so why the "<T>"?
Is This A Good Question/Topic? 0
  • +

Replies To: Collections.binarySearch return type

#2 amir_ju  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 82
  • Joined: 20-June 08

Re: Collections.binarySearch return type

Posted 24 December 2008 - 04:22 PM

The return type is int. the static modifier means this method is static member of class Collection. <> means the container class of this method is a generic.

This post has been edited by amir_ju: 24 December 2008 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Collections.binarySearch return type

Posted 24 December 2008 - 04:33 PM

<T> at the front is known as a Formal Type Parameter and is used to list the "place holders" where a type (to be designated later) will go. So for your example the binarysearch still returns an int, it is just saying that the "T" is going to be where a specific type is to be filled in later.

It could be E, A, whatever as long as it is not a reserved word. Then the compiler will know that each time it runs across a T or an E or an A that it is to put in the correct type in its place.

You can also impose boundaries on it using things like extends... <T extends Number> is going to say that T is the formal parameter and that is going to be of a number type. These are known as Bounded Type Parameters.

Hopefully that made the definition more clear for you. Just know that whatever is in the <> on the front is your list of types that will then be replaced in the method/class that follows. If the return value is of the same type, you will see something like "T" without the brackets.

:)

This post has been edited by Martyr2: 24 December 2008 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Collections.binarySearch return type

Posted 24 December 2008 - 10:19 PM

Sorry, I still don't get the syntax. The complete signature is this:

public static <T> int
binarySearch(List <? extends Comparable<? super T> >list,
												  T key)



I'd like to pick this apart & try to understand what each element means.

First of all, "T" appears both on the left side of "binarySearch" and on the right side and it seems to refer to 2 different things.

The "T" in "super T" is the type of object in the list, and the "T" in "T key" is the type of the object that is being searched for, which would be the same as the type of object that is in the list, so both "T"s in the argument list refer to the same type and I have no problem with that. But why is there a "<T>" in the return type, if the method simply returns a static int?

I'm also wondering about the "<? extends Comparable". The list doesn't extend Comparable, the Object in the list extends Comparable, so I guess that is there simply to specify that characteristic of type T. But "extends Comparable" is not optional - it's required that type T extends Comparable. So why is there a "?" there?

I'm also puzzled by the use of "?" which appears in "<? super T>, referring to the type of Object in the list. An entry "<"TYPE OF OBJECT IN LIST"> is required, not optional, when we call the method, so why is there a "?" in that part of the signature?
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Collections.binarySearch return type

Posted 24 December 2008 - 11:07 PM

public static <T> int
binarySearch(List <? extends Comparable<? super T> >list, T key)



1. First <T> is your formal type parameter saying if you see T in the following method/class, fill it with a type passed by the user. If it is List<Integer> then wherever you see T use "Integer"

2) <? extends Comparable.... A list which has an object of unknown type (hence the "wildcard" ?) but it must implement a Comparable interface. Why? Because List has several methods which compares elements in the list... aka sort, binarysearch etc.

3) Comparable<? super T>... Not only must the object used in the list extend from Comparable on T but on a superclass of T, e.g. Object.

So you add this together and it is saying that a binarysearch is to work on a list of unknown types but types that implement compariable on T or a superclass of T.

So if I was to put in Integer for T then it will have to extend comparable (which if you look up the integer class it does) and any objects which are superclasses of Integer.

If I was to create my own class called "MyClass" I could not pass a list of MyClass objects to binary search unless I implement the comparable interface. MyClass must extend from comparable. If I put in the <? super T> I am then saying that I could pass any superclasses of MyClass if need be.

Hope that has helped out. :)
Was This Post Helpful? 1
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Collections.binarySearch return type

Posted 25 December 2008 - 08:55 AM

(1) "Comparable<? super T>" : this, I guess, is because Java doesn't allow multiple inheritance, and therefore in order for T to extend Comparable, all of T's ancestors must extend Comparable, correct? (But if T's parent is P, and P extends comparable, why is it necessary for T to explicitly extend Comparable? Doesn't it do so implicitly by inheritance?

(2) "<? extends Comparable" : Why not "T extends Comparable"? Is this is the answer to question (1), i.e., T doesn't have to explicitly extend Comparable if T's parent does?
Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Collections.binarySearch return type

Posted 25 December 2008 - 11:42 AM

Man they are "BOUNDARIES" meaning that if an ancestor of T implements comparable then all its subclasses will too so you are cool. It is just laying down the law and saying if you want to use this method, your type T must have comparable implemented (be it a parent or the class itself).

You are way over thinking this. If you attack it from the idea that it is "limits" placed on the functions parameters then you will be better off. It has nothing to do with multiple inheritance, java doesn't support it, the parameter types are exactly as they should be and once you get it, it will hit you like a ton of bricks.

:rolleyes:
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Collections.binarySearch return type

Posted 25 December 2008 - 12:20 PM

I'll take that as a 'yes', as that's basically what I was getting at in #6. Thanks.
B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1