Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 956037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:28:50+00:00 2026-05-16T00:28:50+00:00

In Java, how would I use generics to create a max function that takes

  • 0

In Java, how would I use generics to create a max function that takes as parameters two Comparable objects of the same type and returns the larger one?

I tried:

public static <T extends Comparable> T max(T obj1, T obj2)
{
    return ( ((obj1).compareTo(obj2) >= 0) ? obj1 : obj2);
}

(It returns obj1 if they are both equal.)

The method is based on code I found at http://www.informit.com/articles/article.aspx?p=170176&seqNum=3.

However, when I compile it I get this warning (compiling with -Xlint:unchecked):
warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:28:51+00:00Added an answer on May 16, 2026 at 12:28 am

    That happens because you use the raw type Comparable, and the compiler can’t be sure what you’re comparing against, since compareTo uses the type parameter of Comparable. You need to parametrize Comparable as well:

    public static <T extends Comparable<? super T>> T max(T obj1, T obj2)
    

    You need to use ? super T since you may have a class A that extends a class B which implements Comparable<B>, but A doesn’t implement Comparable<A>. So then you can pass max two A objects, and B matches ? super A, so you can call the compareTo method defined in B even for A objects. If you would use <T extends Comparable<T>> and tried to pass two A objects the compile would complain that they don’t implement Comparable<A>, even though they inherit a compareTo method from B.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's a Java library that I would like to use in my project. My
I'm planning to create something that would do automated translation from Java to C#
Is there a way to convert Java code that uses generics to not use
A good example of when exactly to use interfaces specifically in Java would be
How can I get output from Java anonymous classes? In .Net I would use
I started learning Java. When would I use a HashMap over a TreeMap?
I have an array of Integers in Java, I would like use only a
I would like to use PKCS7 encryption to encode a value together with Java
I am using the java MongoDB driver for gridfs and would like to use
I have downloaded Eclipse Java EE version (3.5) and I would like to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.