Automatic type conversion in Java?

Posted by davr on Stack Overflow See other posts from Stack Overflow or by davr
Published on 2010-03-09T01:57:28Z Indexed on 2010/03/09 2:06 UTC
Read the original article Hit count: 523

Filed under:
|
|

Is there a way to do automatic implicit type conversion in Java? For example, say I have two types, 'FooSet' and 'BarSet' which both are representations of a Set. It is easy to convert between the types, such that I have written two utility methods:

/** Given a BarSet, returns a FooSet */
public FooSet barTOfoo(BarSet input) { /* ... */ }

/** Given a FooSet, returns a BarSet */
public BarSet fooTObar(FooSet input) { /* ... */ }

Now say there's a method like this that I want to call:

public void doSomething(FooSet data) {
    /* .. */
}

But all I have is a BarSet myBarSet...it means extra typing, like:

doSomething(barTOfoo(myBarSet));

Is there a way to tell the compiler that certain types can automatically be cast to other types? I know this is possible in C++ with overloading, but I can't find a way in Java. I want to just be able to type:

doSomething(myBarSet);

And the compiler knows to automatically call barTOfoo()

© Stack Overflow or respective owner

Related posts about java

Related posts about type-conversion