transient

instanceof

A binary operator in java, similar to ==, >, <and other operators. It is a reserved keyword in Java, and its function is to test whether the object on its left is an instance of the class on its right and returns a Boolean data type.

package com.wzc.dao;

import java.util.ArrayList;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        Vector v = new Vector();
        displayObjectClass(v);

    }

    public static void displayObjectClass(Object o) {
        if (o instanceof Vector) {
            System.out.println("对象是 java.util.Vector 类的实例");
        } else if (o instanceof ArrayList) {
            System.out.println("对象是 java.util.ArrayList 类的实例");
        }else{
                System.out.println("对象是 " + o.getClass() + " 类的实例");
            }
        }
    }

volatile

synchronized

final class

Final is a keyword in java, which means "this part cannot be modified".
final can be defined: variables, methods, classes
final variable: If you set the variable to final, you cannot change the value of the final variable (it will be a constant)
Once the final variable is defined, it cannot be modified

class Test{
     final String name = "Hollis";
 
}

final method: If any method is final, it cannot be overridden
When we define a subclass of this class, the name method cannot be overridden, and the compilation will fail.

class Parent {
    final void name() {
        System.out.println("Hollis");
    }
}

final class:

If any class is declared as final, it cannot be inherited.
This class cannot be inherited:

final class Parent {
    
}
static

Represents the meaning of static, used to modify member variables and member methods, can form a static static code block.
Static variables:
Use static to express the variable level. A static variable in a class is not an object or instance of the class. Because static variables are shared with all object instances, they are not thread-safe.
Generally, static variables are often modified with the final keyword, which means general resources or can be used by all objects. If static variables have not been privatized, they can be used in the form of class name and variable name.

//static variable example
private static int count;
public static String str;

public class Test {
    String name = "Hello";//非静态变量
    static String hobby = "World";//静态变量
    
    //在静态方法中调用静态变量和非静态变量
    public static void print() {
        //System.out.println(name);//会报错
        System.out.println(hobby);
    }

But to call non-static variables in static methods, you can create an object of the class, and then access the non-static variable through the object:


public class Test {
    String name = "Hello";//非静态变量
    static String hobby = "World";//静态变量
    
    //在静态方法中调用静态变量和非静态变量
    public static void print() {
        Test test1=new Test();//创建类对象
        System.out.println(test1.name);//通过对象来实现在静态方法中调用非静态变量
        System.out.println(hobby);
       }
}
 

Static method:
Like static variables, static methods belong to a class and not an instance.
A static method can only use static variables and call static methods. Usually static methods are often used for other classes without creating an instance. For example: Collections class (class collection)
Java wrapper classes and utility classes contain many static methods. The main method is the entry point of the java program, which is a static method.
The static method can be called by the class name. The static method name, but the non-static method cannot be called directly, and the non-static method needs to be accessed through the object

public class Test {
    //非静态方法 
    public void test(){
        System.out.println("我是非静态方法test!");
        //Test.sayHello("Test");//调用静态方法编译通过
    }
    //静态方法2
    public static void test2(){
        System.out.println("我是静态方法test2!");
    }
    //在静态方法中调用非静态方法与静态方法
    public static void test3(){
        System.out.println("我是静态方法test3!");
        Test.test2();
        Test testA=new Test();
        testA.test();
    }
    
    public static void main(String[] args) {
        Test.test3();
    }
}
/**
 * output:
 *我是静态方法test3!
 *我是静态方法test2!
 *我是非静态方法test!
*/

And the non-static method can be called by the class name. The static method name, but the non-static method cannot be called directly, and the non-static method needs to be accessed through the object
Static code block:
The java static code block is a set of instructions that are executed in the memory with the java ClassLoader when the class is loaded.
Static blocks are often used to initialize static variables of a class, and are mostly used to create static resources when the class is loaded.

static{
    //can be used to initialize resources when class is loaded
    System.out.println(&quot;StaticExample static block&quot;);
    //can access only static variables and methods
    str=&quot;Test&quot;;
    setCount(2);
}

Static blocks are executed only once when the class is loaded into memory. Java does not allow the use of non-static variables in static blocks.

const

const is a reserved keyword for java, used for later expansion, and its usage is similar to final, not commonly used

At last

At the end of the article, the author has compiled a lot of information for everyone! Including a summary of the Java interview questions of the first-line manufacturers + the learning and thinking guide of each knowledge point + a summary of the Java core knowledge points of a 300-page pdf document! The content of these materials are all knowledge points that the interviewer must ask during the interview. The chapter includes a lot of knowledge points, including basic knowledge, Java collections, JVM, multi-threaded concurrency, spring principles, microservices, Netty and RPC, Kafka , Diary, design pattern, Java algorithm, database, Zookeeper, distributed cache, data structure, etc.
Welcome to pay attention to the official account: have a bright future, get it!


前程有光
936 声望618 粉丝