About the instanceof keyword in Java

Source: Internet
Author: User

Let's talk about the instanceof keyword 1 in Java. Preface: When studying a piece of code today, we can see the following [java] if (colleague instanceof parallel house ){//...} instanceof, the keyword contained in it, is the first time you have seen it. After reading the relevant information, you have learned how to use it. Now let's write a blog to talk about it. Ii. Body: After Baidu, we can know that instanceof is a binary operator of Java, and is like =,> and <. Because it is composed of letters, it is also a reserved keyword of Java. It is used to test whether the object on the left is an instance of the class on the right of the object and return boolean data. For example: String s = "I am an Object! "; Boolean isObject = s instanceof Object; we declare a String Object reference, point to a String Object, and then use instancof to test whether the Object it points to is an instance of the Object class, obviously, this is true, so True is returned, that is, the value of isObject is true. The following is a detailed example: [java]/***** @ Author Sun Jia * @ Date 2013-11-13 * @ At XUST * @ All Copyright by Sun Jia **/class Vehicle {// transportation Vehicle //. ..} class Bicycle extends Vehicle {// Bicycle //...} class Car extends Vehicle {// Car //...} public class Test {public static void Type (Vehicle vehicle) {if (vehicle instanceof Bicycle) {System. out. println ("Bicycle's transformation object");} if (vehicle instanceof Car) {System. out. printl N ("Car transformation object") ;}} public static void main (String [] args) {Vehicle vehicle = new Vehicle (); Bicycle bicycle = new Bicycle (); car car = new Car (); Vehicle vehicle_bicycle = new Bicycle (); Vehicle vehicle_car = new Car (); System. out. println (vehicle instanceof Vehicle); // The output result is: ture System. out. println (bicycle instanceof Bicycle); // The output result is: ture System. out. println (car instanceof Car); // The output result is: ture. System. out. println (vehicle_bicycle instanceof Bicycle); // The output result is: true System. out. println (vehicle_car instanceof Car); // The output result is: ture Type (vehicle_bicycle); // The output result is the Type (vehicle_car) of the transformation object on the Bicycle ); // The output result is: Car's transformation object} The above Type () method implements two subclass processing using one method. However, this approach is generally considered to be failing to take advantage of object-oriented polymorphism. In fact, the above functional requirements can be fully implemented by using method overloading, which is an appropriate approach for object-oriented development and avoids returning to the structured programming mode. As long as the two names and return values are the same, you can accept methods with different parameter types: [java] public static void Type (Bicycle vehicle ){//...} public static void Type (Car vehicle ){//...} therefore, using instanceof is not recommended in most cases, and polymorphism should be well utilized.

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.