java13 object-oriented in-depth

Java13 object-oriented in-depth 2

Outline

package and import

package

Overview

The meaning of the package is to prevent inconvenience caused by naming conflicts .

A package is similar to a folder, and there are various files in the folder. The affiliation relationship between package and package is connected with ".", similar to a subfolder in a 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).

<font color='red'> In order to facilitate the management of a large number of classes in large-scale software systems and solve the problem of class naming conflicts, Java introduces a package mechanism to provide multiple class namespaces for classes. </font>

java13 object-oriented in-depth

format

The general naming is: <font color="blue"> company domain name reversed + function name|module name. </font>

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

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

Example:

package com.java01.test;

The Java compiler manages the package corresponding to the directory 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 at .\com \java01 directory

note:

  • Don't define the same package and the same class as jdk, otherwise it will cause a lot of problems that you find inexplicable
  • You must add packages when writing projects, do not use the default packages.
  • com.oop and com.oop.test, these two packages have no containment relationship and are two completely independent packages. It just seems logically that the latter is part of the former.

import

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

import java.util.Scanner;
import java.io.*; //模糊匹配当前io包下所有类
public class ImportDemo {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in); //因为使用import关键字导包,可以使用
        java.util.ArrayList list=new java.util.ArrayList(); //权限定名
        //导入集合 注意:导入是不要导错包
        //使用*,模糊匹配
        File file=new File("D://test.txt");
        InputStream isInputStream=new FileInputStream(file);
    }
}

The classes that do not need to use import are:

  1. The classes (String,System...) under the language pack (java.lang)

  2. Classes in the same package

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

//导入静态属性
import static java.lang.Math.PI;
import static java.lang.System.out;
//导入静态方法
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 in the first line of the source file of the class

  • Must ensure that the class file of this class is located in the correct directory
  • If other classes want to visit:
    1. Write full name
    2. Introduce
      • Fuzzy matching (all classes used under the package will be introduced), will reduce the compilation speed, but will not affect the running speed
      • Specific class name
      • Static import
    3. Classes in the same package do not need to be imported


JDK packages commonly used in brief:

  1. java.lang-language pack: commonly used functions in the language, such as: String, Math, System, Integer, Thread...

  2. java.util – Toolkit: Provides some useful tool classes, such as containers (List, Set, Map...), date classes

  3. java.io-Input and output package: Provides related classes for operating and reading files, such as: File, InputStream, OutputStream...

  4. java.net-network package: classes for operating remote resources, such as InetSocketAddress, DatagramPacket, ServerSocket...

  5. java.sql-database package: classes for operating JDBC, Connection, Statement, ResultSet...

Garbage collection mechanism (gc)

Overview

Garbage Collection garbage collection mechanism

Every programmer has encountered a memory overflow situation. When the program is running, the memory space is limited, so how to clear the no longer used objects in time to release the memory, this is what the GC has to do.

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

java13 object-oriented in-depth

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 and destroyed with the thread. Therefore, there is no need to consider the problem of excessive memory garbage collection, because when a method call ends or a thread ends, the memory is naturally Follow the recycling.

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

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

Release of object space in different languages:

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

    1. The program forgets to recycle in time, which causes memory leaks and reduces system performance.
    2. A program error reclaims the memory of the program's core class library, causing the system to crash.
  • The Java language does not require the programmer to directly control the memory recovery. The JRE automatically recovers the memory that is no longer used in the background, which is called garbage.

    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 the useful objects in the program and determine which ones are useless.

The key points of the garbage collection mechanism:

  • The garbage collection mechanism only reclaims the object space in the JVM heap memory.
  • There is no way for 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 precisely control the execution of the garbage collection mechanism.
  • The reference variable of the object can be set to null, which implies that the garbage collection mechanism can reclaim 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 this method is overridden and a new reference variable re-references the object, the object will be reactivated).
  • Never actively call the finalize method of an object, it should be called by the garbage collection mechanism.

Guess you like

Origin blog.51cto.com/14966126/2542623