Differences between Java final and instanceof keywords
- 2020-04-01 02:15:04
- OfStack
Final applicable scope:
Modifier: a class that USES this modifier cannot be inherited
Modifier: a modifier cannot be overridden
Modified properties:
1. Final modifies a member variable as a constant, and the value cannot be changed
In Java, constants are capitalized
When a parameter final modifies a primitive type variable, it cannot be modified ina function
Reference type variable: the address cannot be changed
class A
{
}
class B extends A
{
//test
public void eat(){
System.out.println(" Cannot override methods of a parent class ");
}
static final double PI=3.1415926;
public void test( final int x,int y){
//X = 12; You can't change that
y=33;
System.out.println("x="+x+"y="+y);
}
public void test( final int[] x){
//Represents the address of the passed array & PI; You can change the values inside
x[0]=1;
//Wrong here too!! 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 Whether it is B Object (instance) of ");
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");
}
}