Java Method Dispatch-Overrides vs. overloading difference __java

Source: Internet
Author: User

Description: These two days encountered some of the Java methods assigned to the problem, combined with their own books, Google, and the Caesar taught me to make a summary.

Write method assignment refers to how the virtual machine determines which method should be executed!

A lot of content can participate in this blog post in http://rednaxelafx.iteye.com/blog/652719:

The explanations for many of the concepts in this article are excerpted from the posts above, so I'm not going to point it out. Thanks to the help of Gath.

There are also some explanations (including code) from < deep Java Virtual machine-JVM advanced features and best practices, a good book, recommended for students interested in the JVM to buy

In addition http://hllvm.group.iteye.com/group/topic/27064 this post may also help you to understand the method allocation. 1 Static allocation

For this static assignment, Nathaniel prefers to call it a Non-virtual method assignment (and, of course, according to his own words: I don't like to call Static dispatch).

First of all, explain what is called virtual method. The concept of virtual methods is a little hard to say, but the other is the virtual method. Haha non-virtual methods are all class methods (i.e., declared static methods) + all instance methods declared as final or private.

Because the Non-virtual method cannot be override, it naturally does not produce the polymorphic effect of the subclass replication. In this case, the method invoked by the portal may only be one. And the compiler can say that. Which method the JVM needs to execute is already determined in the compiler, and will not change at runtime. A very specific example is the overload of the method.

    See the following example, excerpt from < deep Java Virtual machine-JVM advanced features and best practices >

  Java code    public class staticdispatch {               static abstract class Human{                   }              static class Man extends Human{                   }               static class Woman extends Human{                   }           public void sayhello (Human human) {            system.out.println ("Human say hello");       }   & NBsp;          public void sayhello (Man man) {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Man say hello");        }              public  void sayhello (Woman woman) {            System.out.println ("Woman say hello");       }               /**       *  @param  args        */       public static void  Main (String[] args)  {           human man  = new man ();           Human woman  = new womAn ();           StaticDispatch sd = new  Staticdispatch ();           sd.sayhello (man);           sd.sayhello (woman);       }      }  

The final output is the console writes human say hello
Human Say hello

This is a typical static dispatch. Look at this code.

Human mans = New Man ();
Human woman = new Woman ();

The human is called the static type of the variable, and the man behind it is called the actual type of the variable. Static types are visible in the compiler, and dynamic types must be known at run time. Then analyze the method of this call

Staticdispatch sd = new Staticdispatch ();
Sd.sayhello (Mans);
Sd.sayhello (woman);

We see that the recipient of the calling method is OK, all SD. In a static assignment, how the JVM determines which target method to call exactly depends on the number of incoming arguments and the data type. and is based on the static type of the data. Because of this, the two SayHello methods eventually call the public void SayHello (Human Human);

However, a closer look will reveal that the example I have given, although it is actually statically assigned, is a virtual method. In other words, virtual methods can also be statically assigned. Note that overloading is statically assigned

In fact, the static allocation of the Non-virtual method is perfectly reasonable, and then an example will be given to determine that as long as it is a non-virtual method, it must be statically assigned.

The final question in this section is whether the static dispatch of method overloads in the Java language is a rule of the JVM specification or a language level.

This question once made me confused. Because of this overloaded example above, the Java code Sd.sayhello (man); Sd.sayhello (woman);

These two SayHello methods are all used invokevirtual instructions (for this instruction, which will be followed by a special section), then in fact, dynamic allocation can be used, according to the man and woman To determine which method to call. But in reality the JVM lacks the ability to do so. I've been waiting until I read the Java language "single allocation or multiple dispatch," the content of the answer. The following section is devoted to this single assignment and multiple dispatch. This question is also answered in the answer.

2 Dynamic Dispatch

    It can be said that dynamic method allocation is an important basis for Java implementation polymorphism. Because it is the basis for one of the Java polymorphism----overrides. Look at the following code, excerpt from < advanced features and best practices in Java Virtual machine-JVM > Java code    public class dynamicdispatch {               static abstract class Human{            protected abstract void sayhello ();        }              static class Man  extends human{               @Override    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.