Discuss Generic Java Type comparison algorithm in the Java Help forum on Dev Shed. Generic Java Type comparison algorithm Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 1,405
Time spent in forums: 2 Weeks 1 Day 21 h 39 m 2 sec
Reputation Power: 1594
Generic Java Type comparison algorithm
Hi, I have a few questions to pose here, hopefully someone with experience can give me some insights and answers to these questions:
1. I wonder if there's any generic way of performing a comparison between two java variables regardless of the data type being passed in (of course, both variables must have the same data type)?
2. Is it possible to convert whatever data type into bytes and then perform a comparison by using XOR ?
3. If it is possible to do so, will the process be slow? To me, performance (execution speed) is one issue in my mind now.
4. If the approach of converting values of all data types into bytes for comparison is possible, how about java object types? Can I use the same way to compare Java objects?
__________________
When the programming world turns decent, the real world will turn upside down.
Posts: 1,001
Time spent in forums: 2 Weeks 3 Days 19 h 42 m 49 sec
Reputation Power: 838
Quote:
Originally Posted by tvc3mye
1. I wonder if there's any generic way of performing a comparison between two java variables regardless of the data type being passed in (of course, both variables must have the same data type)?
Depends what you mean by "generic". If you what to do it on Java 1.4, check the sources for java.util.Arrays.sort(..) methods to see how the comparison is done. On Java 1.5 you can use the feature called 'generics'.
Quote:
Originally Posted by tvc3mye
2. Is it possible to convert whatever data type into bytes and then perform a comparison by using XOR ?
Yes, but pointless (because an object variable is just a reference to the object; you'll end up comparing memory addresses) except for primitive types (where relational operators are more efficient anyway)
Quote:
Originally Posted by tvc3mye
3. If it is possible to do so, will the process be slow? To me, performance (execution speed) is one issue in my mind now.
See anwser 2.
Quote:
Originally Posted by tvc3mye
4. If the approach of converting values of all data types into bytes for comparison is possible, how about java object types? Can I use the same way to compare Java objects?
See again answer 2.
[IMO the only sensible approach of comparing to objects is making them implement java.lang.Comparable interface.]