5

In Java all public non static methods are virtual. This means that which method to call is decided on run time(dynamic binding). In C++ virtual functions (dynamic binding) is implemented by using vpointer and vtable. I want to know that how this is implemented by Java. Does Java also use vpointer and vtable like C++ or some other technique to know which method to call on run time?

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
Sanjeev Kumar
  • 680
  • 8
  • 16
  • 5
    It's not implemented "*by Java*", it's implemented by particular JVM (Sun/Oracle, IBM, etc.) And each JVM can have a different approach, this is not part of the JLS. Also check out: http://www.artima.com/insidejvm/ed2/jvmP.html – Tomasz Nurkiewicz Aug 18 '12 at 19:21
  • It may be worth mentioning that I some JVMs also try to un-virtualize the call. Eg if a certain method takes a List which the JVM has seen is always an ArrayList, it can compile in a quick check that a given instance is ArrayList and, if so, invoke the exact ArrayList methods without a vtable lookup. Andrew cooke's link below mentions this. – yshavit Aug 18 '12 at 19:33
  • @yshavit - that's the inline caching that Marko mentions. – andrew cooke Aug 18 '12 at 19:36
  • @andrewcooke ah yes, and your link mentions it as well (I just updated my comment). – yshavit Aug 18 '12 at 19:38
  • http://home.cogeco.ca/~ve3ll/jatutor5.htm Take a look at the official docs as well: http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html – Sebi Aug 18 '12 at 19:19
  • OP is asking for the underlying implementation details of polymorphism in Java, not for an explanation on what _is_ and how to _use_ polymorphism in Java. – Óscar López Aug 18 '12 at 19:22
  • Those links explain the behavior, not the low-level implementation (which is what the OP asked about). – yshavit Aug 18 '12 at 19:23
  • Oh you mean the byte code generation and usage? – Sebi Aug 18 '12 at 19:23
  • http://stackoverflow.com/questions/1207124/learning-about-java-bytecode-and-the-jvm – Sebi Aug 18 '12 at 19:28
  • It's not just bytecode. When e JIT compiles the bytecode to machine code, how does it implement virtual calls? In C++, as OP mentions, this is done via vtables. – yshavit Aug 18 '12 at 19:30

4 Answers4

5

vtables, as described at https://wikis.oracle.com/display/HotSpotInternals/VirtualCalls

[edit Tomasz makes a good point in the question comments - this is for Oracle's hotspot]

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
0

Since this is the world of the Java Virtual Machine, the story is not that simple. The best you can do is learn about all the dozens of tricks employed for all the various special cases. This wouldn't be a bad starting point: Inline caching.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Basically, it's implemented (conceptually) using a virtual function table, and indexes into that. But there are a number of twists, notably "invokespecial" calls and interface calls, where additional/different processing is required.

In practice, a lot of the data is cached, to improve call efficiency.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

Not all non static methods are bound at runtime. In may cases, it can be determined at compile time which version of a method needs to be called. In particular, when the compiler can with 100% certainty determine which method needs to be called. One such easy situation:

public class Foo {
   public final void foo() { ... }

   public void bar() { ... }

   public void bar(String s) { ... }
}

Foo foo = new Foo();
// All three of these could would be bound at compile time.
foo.foo();
foo.bar();
foo.bar("baz");

There are also cases based on overloading where a compile time binding can be determined. I'm not 100% sure what all the different cases are, but there are a few.

Here's a good article on the subject:

http://www.dzone.com/links/r/difference_between_static_and_dynamic_binding_in.html

Matt
  • 11,523
  • 2
  • 23
  • 33