laitimes

I'm not yours who -- java instanceof operator usage demystified

Backstory

"Once The Most Beautiful" is a song sung by Zhu Mingjie, written by Chen Jiaming and composed by Ye Liangjun, which is the theme song of the TV series "Crystal Love". The song lasts 4 minutes and 28 seconds. Song lyrics:

Can't see through your eyes

How much sorrow and joy are hidden

It's as delicate as ice and snow and so transparent

It's like you're going to get old in a moment

The loneliness of the whole city

More than one of you

Only far away

Imagine comforting the distance between us

I'm not yours

Can't bring you comfort

Bear with your withered roses

As if hope had turned to ashes

If not, it hurts the heart

Who remembers whom

Just clouds and months

Think of each other as each other's gains and weaknesses

Can't cry out that it's broken

The most beautiful ever

A familiar street alone

Don't ask who you're thinking

Not to regret has been haggard

Love the chance

The real has crushed the personnel is no longer the same

What else is most valuable

Not to repent has been shattered

The bull knife of the matchmaker

How to tell if it is who's who? Java has an instance of operator (relational operator) that can do this.

Prints out the result true

But what if which one of you doesn't exist? Take a look at the code:

Many people will say it in unison

false

I'm not yours who -- java instanceof operator usage demystified

You're right.

JSL-15.20.2 regulations

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

The Mandarin Duck Spectrum of the Matchmaking Point

What if two classes without any relationship use instanceof?

Many people will say: "The match is not successful"

I'm not yours who -- java instanceof operator usage demystified

Sorry, you fell into the pit again, this will report a compilation error

I'm not yours who -- java instanceof operator usage demystified

The type of the RelationalExpression operand of the instanceof operator must be a reference type or the null type, or a compile-time error occurs.

It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reference type that is reifiable (§4.7).

If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error (§15.16), then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.

Of course, cast can also be a compilation error

The mystery of the matchmaker

Compilers are not omnipotent and cannot detect all problems, see below:

At a glance, there is no problem, there is no problem with the compilation, but the runtime reports an error

Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to Point

The above program shows that when the static type of the expression to be transformed is a superclass of the transformed type

When the behavior of the transition operator. Same as the instanceof operation, if two in a transformation operation

All types are classes, so one must be a subtype of the other. Although for us, this turn

The type will obviously fail, but the type system is not yet strong enough to gain insight into the expression new Object().

The runtime type cannot be a subtype of Point. Therefore, the program will be thrown during runtime

ClassCastException exception.

Matchmaking competition is fierce

The relationship operator instanceof is not the only option on the market, and another guy with a back to the mountain should pay attention

Class's method

booleanisInstance(Object obj)

Determines if the specified Object is assignment-compatible with the object represented by this Class.

So when to use instanceof and when to use isInstance

My understanding is

instanceof is biased between classes

isInstance is biased towards comparing between instance and class

stackoverflow also has an answer to this question:

I take that to mean that isInstance() is primarily intended for use in code dealing with type reflection at runtime. In particular, I would say that it exists to handle cases where you might not know in advance the type(s) of class(es) that you want to check for membership of in advance (rare though those cases probably are).

For instance, you can use it to write a method that checks to see if two arbitrarily typed objects are assignment-compatible, like:

In general, I'd say that using instanceof should be preferred whenever you know the kind of class you want to check against in advance. In those very rare cases where you do not, use isInstance() instead.

Resources

【1】https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.20.2

[2] Java puzzle

【3】https://stackoverflow.com/questions/8692214/when-to-use-class-isinstance-when-to-use-instanceof-operator