The mystery of instanceof and type conversion

Source: Internet
Author: User

public class Colorpoint{public static void Main (string[] args) {String s = null; System.out.println (S instanceof String); ColorPoint CP = (colorpoint) new Object ();}}

Output what? To compile and pass?

Debugging found that you can javac the past, but Java will output false, and then throw an exception.

public class Colorpoint{public static void Main (string[] args) {System.out.println (New ColorPoint () instanceof String);}}

Output what? Can I compile the past?

Try to find that the compilation is not over.

Import java.util.*;p ublic class Colorpoint{<span style= "White-space:pre" ></span>public static void Main ( String[] args) {<span style= "white-space:pre" ></span>colorpoint cp = (colorpoint) new Point (); <span Style= "White-space:pre" ></span>}}class point{}

Can you compile the past? compilation does not pass.

A few requirements for instanceof

1, when the instanceof do the operand is null, the return value is false,

2. If two operands are classes, one of them must be a subtype of the other

Questions about type conversions

Like Instanceof, when two is a class then one must be another subclass, or it will not compile, and as for conversion, it is the run-time thing.

The mystery of instanceof and type conversion

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.