Make a class autobox & autounbox?

Posted on 2014-07-16
Last Modified: 2014-07-17
I see the Double class has autoboxing and unboxing, so I can do things like:
Double a = new Double(1.0);
Double b = new Double(2.0);
If (a < b) {

Open in new window

Can I make my own data class that behaves the same way? Say I have a variable celsius, and celsius is an immutable instance of a Double, but it's a special kind of Double, it's a double that represents a Celsius temperature reading, so I would like to create a class Celsius, which is a Double, so I can do:
Celsius c = new Celcius(23.0);
Celsius d = new Celsius(24.0);
If (c < d) {

Open in new window

But Java doesn't have operator overloading, and Double is a final class, so I can't extend it. Is there another way?
Question by:deleyd
    LVL 86

    Expert Comment

    Say I have a variable celsius, and celsius is an immutable instance of a Double
    But it isn't. Double is final, so it can't be. What you mean is that you've initialized a custom class Celsius with a double. Boxing is only concerned with primitives, so is not relevant to your case.

    Author Comment

    Yea that's my problem. Can I get my class Celsius to do the same thing the class Double does, so I can compare them using simple = < > instead of the cumbersome:
    if(myCelsiusValue.compareTo(anotherCelsiusValue) > 0) {

    Open in new window

    Instead I'd like to be able to:
    if(myCelsiusValue > anotherCelsiusValue) {

    Open in new window

    But I'd also like the Celsius variable to be a Celsius variable so I can also do:
    public void myMethod(Celsius value) {

    Open in new window

    That way I can ensure the value being pass is a Celsius value and not just any Double.  e.g. it's not a Fahrenheit variable.
    LVL 10

    Assisted Solution

    by:Jeffrey Dake
    You wouldn't be able to use the autoboxing and the < format, but you could make your class implement comparable and then do something like.

    If (a.compareTo(b) < 0) 

    Open in new window

    Like CEHJ said, the autoboxing is really only for primitives, so you will need to use function calls for your case.
    LVL 86

    Accepted Solution

    but you could make your class implement comparable
    Yes, that's the best you can do

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Multiple issues with Java 14 37
    JVM encoding. How to change encoding. 27 55
    count8 challlenge 13 68
    word0 challenge 4 37
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    734 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

    20 Experts available now in Live!

    Get 1:1 Help Now