Detailed Explanation of Class Object and Class System in Java Language

  • 2021-10-24 22:50:08
  • OfStack

Object is the base class of all java classes, the top of the whole class inheritance structure, and the most abstract class. Everyone uses toString (), equals (), hashCode (), waite (), notify (), getClass () and other methods every day. Maybe they don't realize that they are Object methods, and they don't see what other methods Object has and think about why these methods should be put into Object.

1. Introduction to the Java Object class-Superclass for all classes

Object is a special class in the Java class library and is the parent of all classes. That is, J ava allows you to assign objects of any type to variables of type Object. When a class is defined, if no inherited parent class is specified, the default parent class is the Object class. Therefore, the following two classes represent the same meaning.

public class MyClass {…}
Equivalent to
public class MyClass extends Object {…}

1.1. Common Methods

Since all Java classes are subclasses of the Object class, any Java object can call methods of the Object class. Common methods:

Object clone () creates a new object with the same class as this object
boolean equals (Object) compares two objects for equality
void finalize () The object garbage collector calls this method when the garbage collector determines that there are no more references to the object
Class getClass () returns an instance class of 1 object at runtime
int hashCode () returns the hash value of the object
void notify () Activates 1 thread waiting on the object's monitor
void notifyAll () activates all threads waiting on the object's monitor
String toString () returns a string representation of the object
void wait () causes the current thread to wait before another thread calls the notify () method or notifyAll () method of this object

2. Detailed explanation of Java System class

The System class is located in the java. lang package, which represents the running platform of the current Java program. Many system-level attributes and control methods are placed inside the class. Because the constructor of the class is private, the object of the class cannot be created, that is, the class cannot be instantiated.

The System class provides a number of class variables and methods that can be called directly through the System class.

The System class has three static member variables, PrintStream out, InputStream in, and PrintStream err.

2.1. PrintStream out Standard Output Stream

This stream is open and ready to receive output data. Typically, this stream corresponds to the display output or another output target specified by the host environment or the user.

For example, a typical way to write 1 line of output data is:

System.out.println(data);

Among them, the println method belongs to the flow class PrintStream, not the method in System.

2.2. InputStream in Standard Input Stream

This stream is open and ready to provide input data. Typically, this stream corresponds to keyboard input or another input source specified by the host environment or the user.

2.3 Error Output Stream of PrintStream err Standard

The syntax is similar to System. out, and no arguments are required to output an error message. It can also be used to output other information specified by the user, including the value of variables.


Related articles: