Java programming things 74 -- Java. lang Package Introduction 1

Source: Internet
Author: User
Tags mathematical constants

Java programming things 74 -- Java. lang Package Introduction 1

Chen yuefeng

From: http://blog.csdn.net/mailbomb

9.4 jdk api package name Overview

The entire jdk api contains about 1 or 200 packages. In general, there are three main types of Package Names: Java, javax, and org. The package name starting with Java is the Basic Language Pack of JDK, and the package starting with javax is a JDK extension package (X is short for extend ), the Feature Pack starting with org is provided by a third-party organization (Org is short for organization ). The jdk api also contains some. the name of the package starting with sun. These are some function packages provided by Sun. Because the classes in these packages change greatly with the JDK version, they are not compatible, therefore, it is not made public in the standard jdk api documentation.

In the subsequent sections of this chapter, we will first introduce the functions and basic usage of common classes. These introductions mainly cover the content in the Java. lang Package and Java. util package.

9.5 java. lang Package

Java. the lang package is a basic Java Language Pack (Lang is short for the language). This package contains the basic function classes and interfaces required by the Java language, is the basis for Java programming.

Because this package is frequently used during Java programming, it is introduced by default in Java.

The following describes the features and usage of common classes in the package.

9.5.1 object

The soul of the object-class Java language, because all classes (except the object class) are subclasses of the class, even if the class is not inherited by writing, the system will automatically inherit the class, therefore, object is the only root of the entire Java language inheritance tree, which is a single inheritance system characteristic of the Java language. Methods In this class are also implemented, including arrays.

Because of the single inheritance system of the Java language, many complex features, such as multi-thread control, are easily implemented in the structure of the Java language, you can also easily update the entire Java language system.

Because the object class is the parent class of all classes in the Java language, the methods in the object class will appear inside each class and are familiar with the usage of common methods in this class, it is the foundation for every programmer to learn.

1. Equals Method

The equals method is used to determine whether the content of the two objects is the same. The implementation of this method in the object class is very simple. The code for implementing the equals method in the object class is as follows (Note: This code can be found in src.zip under the JDK installation directory ):

Public Boolean equals (Object OBJ ){

Return (this =
OBJ );

}

The implementation of methods in the object class is relatively simple. If you really need to compare them in the project, the function of this equals method cannot meet the actual requirements. Therefore, if the classes involved in the project need to compare objects of this type, the equals method must be overwritten.

The following uses a simple class as an example to compile a simple equals method. The source code is as follows:

/**

* Equals method compiling example

*/

Public class myequals {

/** Object member variable */

String name;

/** Member variables of the basic data type */

Int N;

/**

* Determine whether the object content is the same

* @ Param OBJ: object to be compared

*/

Public Boolean equals (Object OBJ ){

// If the content to be compared is its own

If (OBJ = This ){

Return true;

}

// Different object types

If (! (OBJ instanceof
Myequals )){

Return false;

}

// Convert to the current class type

Myequals M = (myequals) OBJ;

/* Compare each variable in the object in sequence */

// Different name attributes

If (! Name. Equals (M. Name )){

Return false;

}

// N attributes are different.

If (! (N = M. N )){

Return false;

}

// If both are the same, true is returned.

Return true;

}

}

In actual comparison, first determine whether the object is itself, and then determine whether the object type meets the requirements. You can use the instanceof keyword to determine whether the syntax format of this operator is:

Object Name instanceof Class Name

If the object name is an object of the subsequent class name type, the result is true; otherwise, the result is false.

If the type meets the requirements, you can compare whether the values of each attribute in the object are the same in sequence. If the values of one attribute are different, they are not equal.

2. Finalize method

The Finalize method is opposite to the previous constructor method. The constructor is used to initialize an object, while the Finalize method is used to release the memory space occupied by an object, methods automatically called by JVM.

Note: The Finalize method serves the same role as the destructor in C ++.

If you need to perform some operations when the object is released, you can overwrite the Finalize method in the class, and then write the code to be executed inside the method.

3. hashcode Method

The hashcode method is used to obtain a value, which is generally called a hash code. This value can be used to quickly determine whether two objects are different. It is mainly used to quickly determine classes in the Collection framework.

For two objects with the same content, the return value of the hashcode method must be the same, while the values of the hashcode of the two objects may be the same.

If the classes you write need to be stored in the Collection class, overwriting this method can improve the execution efficiency of the Collection class.

4. tostring Method

The tostring method is a method automatically called by the system when the object content is displayed. When an object content is output, the system automatically calls the tostring method of this class, for example, the object OBJ of the output object type, the functions of the following two sets of code are the same:

System. Out. println (OBJ );

System. Out. println (obj. tostring ());

The implementation of the tostring class in the object class is relatively simple. The source code is:

Public
String tostring (){

Return
Getclass (). getname () + "@" + integer. tohexstring (hashcode ());

}

If you need to output your class objects in a certain format, you can overwrite the tostring method in the class you designed, and then design the required output format.

For other methods in the object class, the following is a basic introduction:

L clone method: copy an object. That is, to create an object with the same content as the object, the new object has independent memory space.

L getclass method: Mainly obtains the object type. This method is mainly used for the implementation of reflection technology.

In addition, wait, notify, policyall, and other methods are implemented to implement multithreading. They will be detailed in the subsequent multithreading technology.

9.5.2 math

Math is a mathematical tool class. in Java, common mathematical constants and mathematical methods are in this class, and both constants and methods are static, it is convenient for programmers to make practical use.

The following sample code calls the ABS method in the math class to calculate the absolute value of a number:

/**

* Basic use of the math class

*/

Public class mathdemo {

Public static void main (string [] ARGs)
{

Int M =-10;

Int n = math. Abs (m );

System. Out. println ("absolute value:" + n );

}

}

The math class has simple functions, so we will not give an example here. For details, see the jdk api documentation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.