The difference between the Java final and instanceof keywords _java

Source: Internet
Author: User

Final can be applied to the scope:
Cosmetic class: A class that uses this modifier cannot be inherited

Modifier function: The modified cannot be overridden

Cosmetic properties:
1.final decorated member variable is constant, value cannot be modified
and Java's naming convention: constants are capitalized

When the parametric uses final to modify the base type variable, the variable cannot be modified in the function
Reference type variable: cannot change address

Copy Code code as follows:



/* Final class A


{


public final void Eat () {


System.out.println ("test");


    }


} */

Class A
{

}
Class B extends A
{
   //test
    public void Eat () {
        System.out.println ("Methods that cannot override parent classes");
   }

    static final double pi=3.1415926

    public void Test (final int x,int y) {
       //x=12;   Here is the immutable
        y=33;
        System.out.println ("x=" +x+ "y=" +y);
   }
    public void Test (final int[] x) {
       //representing the address of an array passed over nbsp Can change the value inside
        x[0]=1;
       /This is also the wrong!!! X=new int[]{23};
        System.out.println (x[1]);
   }
}
Class Demo4
{
    public static void main (string[] args
    {
        new B (). Test (2,3);
        new B (). Test (new int[]{20,3});

        A A =new a ();
        b b=new B ();
        System.out.println ("A is an object (instance) of B");
        System.out.println ("instanceof" + (a instanceof a));
        System.out.println ("instanceof" + (a instanceof B));
        System.out.println ("instanceof" + (b instanceof b));
        System.out.println ("instanceof" + (b instanceof A));
        System.out.println ("final");
   }

}

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.