19

In C# I can create my own implicit conversion for my classes as follows...

public static implicit operator TypeYouWantToChangeTo (ClassYouAreChanging c)  
{ 
    TypeYouWantToChangeTo x;
    //convert c to x
    return x;
}

How do you make a class be able to implicitly convert to another class? Also in C# I can change implicit to explicit to also create the ability to cast manually. How do you accomplish this in Java?

3
  • There is no implcit conversion like this. (the //convert c to x is indeed empty, right?) Commented Apr 12, 2014 at 21:54
  • stackoverflow.com/questions/295224/… Commented Apr 12, 2014 at 21:55
  • Scala has this feature, and used judiciously it can greatly enhance code readability when writing a DSL. I lament the lack of this feature in Java. :( Commented Aug 31, 2021 at 23:57

1 Answer 1

26

You can not overload operators with Java. Add a method to your class of choice to perform the conversion. For example:

public class Blammy
{
    public static final Blammy blammyFromHooty(final Hooty hoot)
    {
        Blammy returnValue;

        // convert from Hooty to Blammy.

        return returnValue;
    }
}

Edit

  • Only built-in types have implicit conversions.
  • Explicit conversion (in java) is called a cast. for example, int q = 15; double x = (double) q;
  • There can never be implicit conversions of your custom types.
  • extends and implements are not conversion.
  • All implicit conversions are for primitive types.
Sign up to request clarification or add additional context in comments.

5 Comments

So only the built in types have implicit conversions and explicit conversions? Is there ever cases when your custom classes can do explicit or implicit conversion? Such as extends and implements? For example the different number types have custom implicit conversions so they can do it but we are not allowed to?
How does Java determine whether you can explicit convert or not? Only if you extends or implements you can cast your custom types?
extends and implements are not conversions. explicit conversion is a cast. for example int x = (int)0x00; A "cast" between types is not a conversion. it is just instructions to the compiler to "treat this reference as though it was a reference to this other type".
Is there a list anywhere of items that can be explicitly casted and implicitly casted in Java?
implicit conversion is typically called "boxing" and "unboxing" in java, and is only done for the Java.lang.Number classes + char. It's used to get from the 16 bits on the stack created for a 'char' to the 16 bits in heap needed by a 'Character', or the 32bits on the stack of an "int" to the 32bits of heap needed by an 'Integer', etc. Ant you cannot define boxing and unboxing yourself. As a moderately experienced programmer in Java and C# I can say that overloaded casting operators can be a really good way to hang yourself, but they can also replace a huge number of "builder...build()" calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.