Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Simple method but hard with generics: compareTo()
I adjusted my getMaximum() method to generics to avoid the compiler warnings:
Bound mismatch: The method compareTo(? extends Comparable) of type
Comparable<? extends Comparable> is not applicable for the arguments
(Comparable<? extends Comparable>). The wildcard parameter ? extends
Comparable has no lower bound, and may actually be more restrictive than argument
Comparable<? extends Comparable>
static public Comparable<Comparable> getMaximum(Comparable<Comparable> comparable1, Comparable<Comparable> comparable2) { Comparable<Comparable> result = (comparable1 != null) ? comparable1 : comparable2; //init if (comparable1 != null && comparable2 != null) { int c = comparable1.compareTo(comparable2); result = (c > 0) ? comparable1 : comparable2; //comparable1 > comparable2? } return result; }//getMaximum()But now, my other classes that call this method e.g. with 2 dates or with 2 numbers don't compile anymore. I tried
static public Comparable<? extends Comparable> getMaximum(Comparable<? extends Comparable> comparable1, Comparable<? extends Comparable> comparable2) { Comparable<? extends Comparable> result = comparable1; //default if (comparable1 != null && comparable2 != null) { int c = comparable1.compareTo(comparable2); //compile error if (c <= 0) { result = comparable2; } } else if (comparable1 == null) { result = comparable2; } return result; }//getMaximum()but this code doesn't compile:
Bound mismatch: The method compareTo(? extends Comparable) of type
Comparable<? extends Comparable> is not applicable for the arguments
(Comparable<? extends Comparable>). The wildcard parameter ? extends
Comparable has no lower bound, and may actually be more restrictive than argument
Comparable<? extends Comparable>
Comments
-
public class Test { static public <T extends Comparable<T>> T getMaximum(T comparable1, T comparable2) { T result = (comparable1 != null) ? comparable1 : comparable2; if (comparable1 != null && comparable2 != null) { int c = comparable1.compareTo(comparable2); result = (c > 0) ? comparable1 : comparable2; } return result; } static <T extends Comparable<T>> void test(T t1, T t2) { System.out.printf("max(%s, %s) = %s%n", t1, t2, getMaximum(t1,t2)); } public static void main(String... args) { test(1, 2); test(1.0, 2.0); test("aaaa", "bbbb"); } }
You really need to read the generics tutorial:
http://java.sun.com/j2se/5/pdf/generics-tutorial.pdf -
Thanks, I already browsed this tutorial. I just don't understand why the "?" isn't working.
-
I also tried to translate the 1.4 code of TableSorter.java (code from Java Tutorial) but wasn't able to fix the warning of this code:
public static final Comparator COMPARABLE_COMAPRATOR = new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) o1).compareTo(o2); //warning } };
Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized
I tried to adjust it with similar code as your suggestion, but without success. -
I also tried to translate the 1.4 code of
TableSorter.java (code from Java Tutorial) but wasn't
able to fix the warning of this code:<div class="jive-quote">public static final Comparator COMPARABLE_COMAPRATOR = new Comparator() {<br /> public int compare(Object o1, Object o2) {<br /> return ((Comparable) o1).compareTo(o2); //warning<br /> }<br /> };</div>
There is no way to do that without getting a warning. The
type system is not expressive enough. -
Now, that's what I call a default behaviour (show warnings) that wasn't thought through to the end. I guess, most developers want to avoid not only errors but also warnings. And you say, that for this code, it's impossible to resolve this warning?
-
I also tried to translate the 1.4 code ofHow about this:
TableSorter.java (code from Java Tutorial) but wasn't
able to fix the warning of this code:public static final Comparator COMPARABLE_COMAPRATOR = new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) o1).compareTo(o2); //warning } };
Type safety: The method compareTo(Object) belongs
to the raw type Comparable. References to generic
type Comparable<T> should be parameterized
I tried to adjust it with similar code as your
suggestion, but without success.class Comparables { static <T extends Comparable<T>> Comparator<T> comparatorOfComparables() { return new Comparator<T>() { public int compare( final T first, final T second ) { return first.compareTo( second ); } }; } }
?? -
Now, that's what I call a default behaviour (showThe warnings are thought through to the end. However,
warnings) that wasn't thought through to the end.
we had multiple constraints. An important constraint is
compatibility as discussed by Neal Gafter on his blog:
http://gafter.blogspot.com/2004/09/puzzling-through-erasure-answer.html
Maintaining compatibility means to accept programs that
can't be expressed in the new type system. To accomplish this,
we give a warning instead of a error.
But sometimes it is just impossible to make your pre-generics
code generic because you can't redesign your entire application
with generics in mind. So you will be able to suppress unchecked
warnings on per-method basis. Use the @SuppressWarnings("unchecked")
annotation. Currently, the compiler will simply ignore it, but
that will change in future versions. -
How can we use this @SuppressWarnings("unchecked")? I didn't found an answer in this forum or in the Annotations doc (http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html).
-
I found an article "What is the SuppressWarnings annotation?" (http://www.langer.camelot.de/GenericsFAQ/FAQSections/TechnicalDetails.html#What%20is%20the%20SuppressWarnings%20annotation?) on the internet, so I added
@SuppressWarnings("unchecked") public class TableSorter extends AbstractTableModel { ...
but I still get the warnings:
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
I also tried this:@SuppressWarnings(value={"unchecked","unsafe"}) public class TableSorter extends AbstractTableModel {...
with no success. (Note: this TableSorter class is the only class with unchecked warnings, so there are no other classes that can cause these warnings.) -
Go back and read the last sentence of what Peter said more carefully.
This discussion has been closed.