Make a class autobox & autounbox?

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?
deleydSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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.
deleydSoftware EngineerAuthor Commented:
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.
Jeffrey Dake Senior Director of TechnologyCommented:
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


http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo%28T%29

Like CEHJ said, the autoboxing is really only for primitives, so you will need to use function calls for your case.
CEHJCommented:
but you could make your class implement comparable
Yes, that's the best you can do

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.