Analysis on instanceof, which is not commonly used in Java

Source: Internet
Author: User
, I am dizzy, I am very dizzy ~~.
When I look at the JDK wishing code, I see some uncommon keywords, at least I rarely use them, or I will not use them. I found some materials and sorted them out.
Instanceof: it is useful to know the object type at runtime. When programming, we must determine the object type. One method is forced conversion. Another method is to use instanceof. But we have also seen a lot of illegal forced conversion with ourselves. You can't blame it if you don't do it during the test.
What is instanceof? For example, everyone on earth understands.

Public void test (object Value ){
If (value instanceof integer ){
// Enter this field if the value is an integer.
} Else {
//.....
}
}
If forced conversion is adopted
Public void test (object Value ){
Integer integer = (integer) value; // if you call text (object) {} elsewhere and pass in another object, an exception may be thrown here
Integer. // call integer regardless of 3721.
}
It's almost time for dinner.

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.