java13 object-oriented in-depth

Posted Jun 15, 20205 min read

Java13 object-oriented in-depthTwo

Outline

package and import

package

Overview

The significance of the package is to prevent naming conflicts from causing inconvenience.

The package is similar to a folder, and there are various files in the folder. The attachment relationship between package and package is connected with ".", similar to the subfolder in the parent folder. For example, java.lang.String is the String file in the lang folder in the java folder. java.io.InputStream is the InputStream file in the io folder in the java folder.

Files with the same name cannot exist in the same folder, and files with the same name are allowed in folders with different names, so different folders(that is, the same class name is allowed in different packages).

In order to facilitate the management of a large number of classes in large software systems and solve the problem of class naming conflicts, Java introduced a package mechanism to provide multiple class namespaces for classes.

Format

The general name is:Company domain name reverse + function name|module name.

The package statement is the first statement in the Java source file, indicating the package in which the class defined in the file is located.(If the statement defaults, it is specified as an unnamed package).

package pkg1[.pkg2[.pkg3…]];

example:

package com.java01.test;

The Java compiler corresponds the package to the directory management of the file system. In the package statement, use "." to indicate the level of the package(directory). For example, use the statement:package com.java01, then all the classes in the file are located in the .comjava01 directory under

note:

  • Do not define the same package and the same class as jdk, otherwise it will cause a lot of problems that you find inexplicable
  • Add packages when writing projects, do not use the default package.
  • com.oop and com.oop.test, these two packages are not included, they are two completely independent packages. It seems logical that the latter is part of the former.

import

If there is a package name for a class, when using the class under other packages, you must use the fully qualified name(full name or full class name for short, com.java01.MyClass) before the compiler can find the class; you can also use import Introduce the class to be used at the beginning of the file.

import java.util.Scanner;
import java.io.*; //Fuzzy match all classes under the current io package
public class ImportDemo {
    public static void main(String[]args) {
        Scanner sc=new Scanner(System.in); //Because the import keyword is used to guide the package, you can use
        java.util.ArrayList list=new java.util.ArrayList(); //Name the permissions
        //Import collection
        //Use *, fuzzy matching
        File file=new File("D://test.txt");
        InputStream isInputStream=new FileInputStream(file);
    }
}

The classes that do not need to use import are:

  1. Classes under the language package(java.lang)(String, System...)
  2. Classes under the same package

Static import:
The import statement can not only import classes, but also add the function of importing static methods

//Import static properties
import static java.lang.Math.PI;
import static java.lang.System.out;
//Import static methods
import static java.lang.Math.sin;

public class ImportDemo {
    public static void main(String[]args) {
        out.println(PI);
        sin(1.1);
    }
}

to sum up

  • If you want to put a class in a package, use package on the first line of the class source file

  • Must ensure that the class file of this class is located in the correct directory

  • If you want to visit another category:

    1. Write the full name

    2. Introduce

      • Fuzzy matching(will introduce all the classes used in the package), will reduce the compilation speed, but will not affect the running speed
      • Specific class name
      • Static import
    3. Classes under the same package do not need to be imported

​​
A brief introduction of commonly used packages in JDK:

  1. java.lang-language pack:commonly used functions in the language, such as:String, Math, System, Integer, Thread...
  2. java.util-Toolkit:provides some practical tools, such as:containers(List, Set, Map...), date class
  3. java.io – Input and output packages:provide related classes for reading files, such as File, InputStream, OutputStream...
  4. java.net-network package:the class of operating remote resources, such as:InetSocketAddress, DatagramPacket, ServerSocket...
  5. java.sql-database package:JDBC operation class, Connection, Statement, ResultSet...

Garbage collection mechanism(gc)

Overview

Garbage Collection garbage collection mechanism

Every programmer has encountered a memory overflow situation. When the program runs, the memory space is limited. So how to clear out the objects that are no longer used and release the memory in a timely manner. This is what GC needs to do.

Speaking of GC, most people regard this technology as a companion product of Java language. In fact, GC has a longer history than Java. As early as 1960, Lisp language used dynamic memory allocation and garbage collection technology.

Programmers know that the jvm memory structure is divided into five areas:program counter, virtual machine stack, local method stack, heap area, and method area. Among them, the virtual machine stack, the local method stack, and the program counter are generated with the thread and destroyed with the thread, so there is no need to consider too much memory garbage collection, because when a method call ends or the thread ends, the memory naturally Followed by recycling.

We will focus on the method area and the heap area. This part of the memory allocation and recovery is dynamic, which is what the garbage collector needs to pay attention to.

GC mainly does the work of cleaning up objects and organizing memory.

Release of object space in different languages:

  • The traditional C/C++ language requires the programmer to reclaim the allocated memory. Disadvantages of explicit garbage collection:

    1. The program forgets to recycle it in time, resulting in memory leaks and reduced system performance.
    2. Program error reclaims the memory of the program's core class library, causing the system to crash.
  • Java language does not require the programmer to directly control the memory recycling. The JRE automatically recycles the memory that is no longer used in the background, called garbage

    Recycling mechanism(Garbage Collection).

    1. Can improve programming efficiency.
    2. Protect the integrity of the program.
    3. Its overhead affects performance. The Java virtual machine must track useful objects in the program and determine which ones are useless.

Key points of garbage collection mechanism:

  • The garbage collection mechanism only reclaims the object space in the JVM heap memory.
  • Powerless to other physical connections, such as database connection, input stream output stream, socket connection
  • The current JVM has a variety of garbage collection implementation algorithms with different performances.
  • The occurrence of garbage collection is unpredictable, and the program cannot accurately control the execution of the garbage collection mechanism.
  • You can set the reference variable of the object to null, implying that the garbage collection mechanism can recycle the object.
  • Programmers can notify the system of garbage collection through System.gc() or Runtime.getRuntime().gc(), which will have some effects, but it is still uncertain whether the system performs garbage collection.
  • Before the garbage collection mechanism reclaims any object, it will always call its finalize method(if you override this method and let a new reference variable re-reference the object, the object will be reactivated).
  • Never actively call the finalize method of an object, it should be handed over to the garbage collection mechanism.