Home  >  Article  >  Java  >  How to use Object class and System class in Java?

How to use Object class and System class in Java?

WBOY
WBOYforward
2023-04-23 23:28:14971browse

Object is the base class of all Java classes, the top of the entire class inheritance structure, and the most abstract class. Everyone uses toString(), equals(), hashCode(), wait(), notify(), getClass() and other methods every day. Maybe they don’t realize that they are methods of Object, and they don’t look at what other methods Object has. And think about why these methods should be placed in Object.

1. Introduction to Java Object class - the super class of all classes

Object is a special class in the Java class library and the parent class of all classes. In other words, Java allows any type of object to be assigned to a variable 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. Commonly used methods

Due to Java All classes are subclasses of the Object class, so any Java object can call methods of the Object class. Common methods:

Object clone() Creates a new object of the same class as the object
boolean equals(Object) Compares whether two objects are equal
void finalize() When the garbage collector determines that it is not equal The object garbage collector calls this method when there are more references to the object
Class getClass() Returns an instance class of the object at runtime
int hashCode() Returns the hash code value of the object
void notify() activates a thread waiting on the monitor of the object
void notifyAll() activates all threads waiting on the monitor of the object
String toString() returns the string representation of the object
void wait() causes the current thread to wait before other threads call the notify() method or notifyAll() method of this object

2. Detailed explanation of Java System class

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

The System class provides some class variables and class methods, allowing these class variables and class methods to be called directly through the System class.

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

2.1. PrintStream out standard output stream

This stream has been opened and is ready to receive output data. Typically, this stream corresponds to monitor output or another output destination specified by the host environment or the user.

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

System.out.println(data);

Among them, the println method is a method belonging to the stream class PrintStream. Rather than the method in System.

2.2. InputStream in standard input stream

This stream has been opened and is 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. PrintStream err standard error output stream

The syntax is similar to System.out, and error information can be output without providing parameters. It can also be used to output other information specified by the user, including the value of variables.

The above is the detailed content of How to use Object class and System class in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete