How does the instanceof in Java come true?

Source: Internet
Author: User

Start completely confused Ah, back to read the introduction of the Java instruction set, gradually understand.

https://www.zhihu.com/question/21574535/answer/18998914

The following answer is more straightforward.

instanceof instructions.
instanceof6. The Java Virtual machine instruction Set, the JVM specification Java SE 7 version According to the specification to imagine the implementation can be sorta to mix this problem. Address: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.instanceof

The definition of the above instruction is explained in detail as follows (explanations and important points already floated Red pointed out):

instanceofOperation

Determine if object is of given type

Format


instanceof
Indexbyte1
Indexbyte2

Forms

instanceof = 193 (0xc1)

Operand Stack

..., objectref →

..., result

Description

the  objectref , which must be of Type reference , is popped from the operand stack. the unsigned  indexbyte1  and  indexbyte2  are used to construct an index Into the run-time constant pool of the current class (§2.6), where the value of the index is ( indexbyte1   <<  8) |  indexbyte2 . The Run-time constant pool item at the index must is a symbolic reference to a class, array, or interface type.

If objectref null is, the instanceof instruction pushes a int result of 0 as an On the int operand stack.

This means that the left side is null and returns FALSE. (also, the right side cannot be null)

Otherwise, the named class, array, or interface type is resolved (§5.4.3.1). If objectref is an instance of the resolved class or array or implements the resolved interface, the instanceof instruction pushes an int result of 1 as a on int the operand stack; otherwise, it p Ushes an int result of 0.

This is just an overview, here is a detailed explanation. Divided into multiple minutes.

the following rules is used to determine whether An  ObjectRef  that is Not null  is An instance of the resolved Type:if s is the class of the object referred to By  objectref  and < Span class= "Type" >t is the resolved class, array, or interface Type,  instanceof  determines whether  ObjectRef  is An instance Of t as follows:

  • if s is an Ordinary (Nonarray) class, then:

    • if t is a class type, Then s must be the same class as& nbsp t, Or s must be a subclass Of T;

    • if t is an interface type, Then s must implement Interface t.

  • if s is an interface type, then:

    • if  t is a class type, Then t  Must be object .

    • if t is an interface type, Then t must be the same interface As s or a superinterface of s.

  • If S is a class representing the array type SC, that's, an array of components [] of type SC, then:

    • If T is a class type and then T must be Object .

    • If T is a interface type, then T must are one of the interfaces implemented by arrays (jls§4.10.3).

    • If is a array type TC, that's, an array of components [] of type TC, then one of the following must be T Rue:

      • TC and SC are the same primitive type.

      • TC and SC are reference types, and type SC can is cast to TC by these Run-time rules.

Linking Exceptions

During resolution of the symbolic reference to the class, array, or interface type, any of the exceptions documented in§5 .4.3.1 can be thrown.

Notes

The instanceof instruction is very similar to the checkcast instruction (§checkcast). It differs in its treatment null(上面提到,左侧null会直接返回false) of, its behavior when its test fails (checkcast throws an exceptio N, instanceof pushes a result code), and its effect on the operand stack.

The following answer is more obscure.

You pay more than 10000 of the job interview in Java Senior research and development positions, focus on performance tuning what. This position does not directly involve the development of the JVM, but because performance problems often stem from "abstract leaks",
The idea of the implementation of the JVM actually used needs to be understood. The interviewer's knowledge of the JVM may also be at this level.
This can be dealt with in a paper: Fast subtype checking in the HotSpot JVM.
There was a discussion post that discussed the interpretation of this paper: Ask a share/vm/oops code to do a fast subtype check problem

It refers to the "abstract disclosure", the following will be a single article analysis.

Look at the following code example (http://jurisp.iteye.com/blog/815704)

String s =NULL; System.out.println (SinstanceofObject);//falseSystem.out.println (SinstanceofString);//false//The following cannot be compiled incompatible conditional operand types String and string[]//Boolean as = (S instanceof string[]); //cannot compile Syntax error on token "null", invalid Referencetype//System.out.println (s instanceof null); //cannot compile incompatible conditional operand types Typeinstancetest and String//System.out.println (New solution () instanceof String);System.out.println (NewString ()instanceofString);//trueSystem.out.println (NewString ()instanceofObject);//true//will throw an exceptionSolution test = (solution)NewObject ();

The results of the operation are as follows:

false false true true Exception

It is not possible to use instanceof between two types, if the type of two operands is a class, one must be the subtype of the other .

Also, take note of the obscure statement below.

Accesses a static method, using an expression as a qualifier. The run-time type of the object referenced by the value of the expression does not work when determining which method is called, and the object has an identifier, and its identifier has no effect.

The code examples are as follows:

class Solution {  publicstaticboolean  canfinish () {        System.out.println ( "Here");         return true ;  }    Public Static void Main (string[] args) {        null). Canfinish ();  }} Run to get the correct call result: here

Note that the static method can be called with NULL.

How does the instanceof in Java come true?

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.