Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent to C# dynamic class type?

I'm coming from the world of c#.

in C# i am able to use the class dynamic http://msdn.microsoft.com/en-us/library/dd264741.aspx

This allows me to not have to use templated/generic classes but achieve a simliar feel for certian situations.

I'm have been unsuccessfull in internet searchs as unfortunately 'dynamic' and 'java' keywords turn up alot of unrelated infromation on dynamic architectures.

I have dabbled a bit in javaFX and there is a type var which appears to have the same usage as c#'s dynamic. However it doesnt appear to be usable in Java.

thanks, stephanie

like image 388
Without Me It Just Aweso Avatar asked Oct 14 '10 17:10

Without Me It Just Aweso


People also ask

Is Java similar to C?

C is a procedural, low level, and compiled language. Java is an object-oriented, high level, and interpreted language. Java uses objects, while C uses functions. Java is easier to learn and use because it's high level, while C can do more and perform faster because it's closer to machine code.

Why is C so similar to Java?

C# and Java are similar languages that are typed statically, strongly, and manifestly. Both are object-oriented, and designed with semi-interpretation or runtime just-in-time compilation, and both are curly brace languages, like C and C++.

Is Java similar to C or C++?

Both C++ and Java fall into the family of C-like languages, as they generally resemble C in their syntax. The most significant difference is their ecosystems: While C++ can seamlessly call into libraries based on C or C++, or the API of an operating system, Java is best suited for Java-based libraries.

Is Java built on C?

The very first Java compiler was developed by Sun Microsystems and was written in C using some libraries from C++. Today, the Java compiler is written in Java, while the JRE is written in C.


1 Answers

Java doesn't support dynamic typing, but you can simulate something like that using dynamic proxy in Java. First you'll need to declare an interface with operations you want to invoke on your objects:

public interface MyOps {
  void foo();
  void boo();
}

Then create Proxy for dynamic invocation on myObjectInstance:

MyOps p = (MyOps) Proxy.newProxyInstance(getClass().getClassLoader(), //
    new Class<?>[] { MyOps.class }, //
    new MyHandler(myObject));
p.foo();
p.boo();

where MyHandler is declared like this:

public class MyHandler implements InvocationHandler {
  private final Object o;

  public MyHandler(Object o) {
    this.o = o;
  }

  public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
    Method method = o.getClass().getMethod(m.getName(), m.getParameterTypes());
    return method.invoke(o, args);
  }
}

so, if myObject has methods foo() and boo(), they will be invoked, or else, you'll get a RuntimeException.

There is also number of languages that can run in JVM support dynamic typing, e.g. Scala, Groovy, JRuby, BeanShell, JavaScript/Rhino and many others. There is some JVM changes are coming in Java 7 to support a native dynamic dispatch, so these languages could perform much better, but such feature won't be directly exposed in statically typed Java language.

like image 70
Eugene Kuleshov Avatar answered Sep 19 '22 09:09

Eugene Kuleshov