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?

ErikE
  • 48,881
  • 23
  • 151
  • 196
CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • There is no implcit conversion like this. (the `//convert c to x` is indeed empty, right?) – fge Apr 12 '14 at 21:54
  • http://stackoverflow.com/questions/295224/what-are-major-differences-between-c-sharp-and-java – user2864740 Apr 12 '14 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. :( – John Arrowwood Aug 31 '21 at 23:57

1 Answers1

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.
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
DwB
  • 37,124
  • 11
  • 56
  • 82
  • 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 '14 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 '14 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 '14 at 00:12
  • Is there a list anywhere of items that can be explicitly casted and implicitly casted in Java? – CodeCamper Apr 14 '14 at 02: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 '14 at 21:49