18

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?)
    – fge
    Apr 12, 2014 at 21:54
  • 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. :( Aug 31, 2021 at 23:57

1 Answer 1

24

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.
5
  • 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?
    – CodeCamper
    Apr 12, 2014 at 22:32
  • How does Java determine whether you can explicit convert or not? Only if you extends or implements you can cast your custom types?
    – CodeCamper
    Apr 13, 2014 at 11:56
  • 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".
    – DwB
    Apr 14, 2014 at 0:12
  • Is there a list anywhere of items that can be explicitly casted and implicitly casted in Java?
    – CodeCamper
    Apr 14, 2014 at 2:05
  • 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.
    – Groostav
    Oct 29, 2014 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.