In this case, one Java method, adder , is a candidate for executing the call site. This method does not have to be a Java method. Instead, if several such methods are available to the runtime system, each handling different argument types, the mybsm bootstrap method could dynamically select the correct method, based on the dynMethodType argument.
You can use the invokedynamic instruction simplifies and potentially improves implementations of compilers and runtime systems for dynamic languages on the JVM. The invokedynamic instruction enables the language implementer to define custom linkage. This contrasts with other JVM instructions such as invokevirtual , in which linkage behavior specific to Java classes and interfaces is hard-wired by the JVM. Each instance of an invokedynamic instruction is called a dynamic call site.
When an instance of the dynamic call site is created, it is in an unlinked state, with no method specified for the call site to invoke. The dynamic call site is linked to a method by means of a bootstrap method. A dynamic call site's bootstrap method is a method specified by the compiler for the dynamically-typed language. The method is called once by the JVM to link the site. The object returned from the bootstrap method permanently determines the call site's activity.
The invokedynamic instruction contains a constant pool index in the same format as for the other invoke instructions. The adder method and mybsm method are defined in The Challenge of Compiling Dynamically Typed Languages line breaks have been added for clarity :.
Invoking a dynamically-linked method with the invokedynamic instruction involves the following steps:. At runtime, the first time the JVM encounters an invokedynamic instruction, it calls the bootstrap method. This method links the name that the invokedynamic instruction specifies with the code to execute the target method, which is referenced by a method handle. The next time the JVM executes the same invokedynamic instruction, it does not call the bootstrap method; it automatically calls the linked method handle.
The bootstrap method's return type must be java. The CallSite object represents the linked state of the invokedynamic instruction and the method handle to which it is linked. This entry contains references to other entries in the constant pool and references to attributes.
See, java. The following example shows an excerpt from the constant pool for the class Example , which contains the bootstrap method Example. The constant pool entry for the invokedynamic instruction in this example contains three values:. The value, 0 refers to the first bootstrap method specifier in the array of specifiers that are stored in the BootstrapMethods attribute. Bootstrap method specifiers are not in the constant pool table. They are contained in this separate array of specifiers.
The following example shows an excerpt from the same constant pool that shows the BootstrapMethods attribute, which contains the array of bootstrap method specifiers:.
The constant pool entry for the bootstrap method mybsm method handle contains the following values:. See the next section, Using the invokedynamic Instruction , for more information about this subtag. The first four instructions put the integers 40 and 2 in the stack and boxes them in the java. Integer wrapper type. The fifth instruction invokes a dynamic method. This reference to a bootstrap method specifier is not in the constant pool table.
It is contained in a separate array defined by a class file attribute named BootstrapMethods. This means that this bootstrap method will create a method handle for a static method; note that this bootstrap method is linking the dynamic call site with the static Java adder method. For example if you want to add a number and a string in your program then the specified code must convert the string into a number to be used of an addition operator and produce an error free result.
This makes your code easier to understand and more verbose. Lets see, how this example converts a string into a number explicitly:.
The Addition after converting is Download this program. Weakly Typed Languages: In weakly Typed languages, you can simply declare a variable without assigning it a data type. Once you have declared a variable of that type, it will only accept the values that are of the same type and will remain of that type indefinitely. In such languages the type checking is performed at runtime. The JavaScript language is such type of language where the data conversion is not required explicitly , and all the conversion are handled implicitly.
For example, If we pass a string to the addition operator then it will be interpreted as a number or cause an error if the contents of the string are not converted into a number automatically. Safety and strong typing make possible the early detection of many errors. The beginning programmer will benefit from safety and strong typing more than the professional programmer, simply because the beginner tends to make more of each type of error.
But the professional programmer will also gain from a safe and strongly type language. When a program consists of hundreds of files, it is too easy to get confused and make mistakes, and the more checks we have on our programs, the better off we are. Finally, with a language like Java, whose programs can be accessed on web pages by writing applets , safety and strong typing aid the user as well, for these properties make it more difficult for malicious people to hack into systems through such applets.
Cornell University Department of Computer Science. Pages People more options. Transition to OO programming. Spring --self-paced version. Safety and strong typing Safety One property that is sometimes looked for in a programming language is safety. There is some confusion as to what safety means, but here is a definition used in the year by an ad hoc committee that recommended that the Advanced Placement AP test in programming be based on the programming language Java: Safety : Any attempt to misinterpret data is caught at compile time or generates a well-specified error at runtime.
0コメント