Common Java classes and reflections, class loading, and java reflection Loading

Source: Internet
Author: User

Common Java classes and reflections, class loading, and java reflection Loading
1. System-related classes

Java provides the System class and Runtime class to interact with the platform where the program is running.

A. the System class represents the running platform of the current Java program.

A. System class is a final class. All attributes and methods of this class are static. You can call it directly without creating an object.

B. attributes: in, out, and err

C. Common Methods: currentTimeMillis (), exit (), getProperties (), gc ()

B. The Runtime class represents the Runtime environment of the Java program.

Note: applications cannot create their own Runtime instances. However, you can use the static method getRuntime () of the Runtime class to obtain the associated Runtime object.

A. current number of processors: rt. availableProcessors ()

B. Idle memory: rt. freeMemory ()

C. Total memory size: rt. totalMemory ()

D. Maximum available memory: rt. maxMemory ()

2. String-related classes

Java provides classes such as String, StringBuffer, and StringBuilder to encapsulate strings. These classes also provide a large number of methods for operating String objects.

Note: Each character of a string in Java has an index value. The index value starts from 0, and each string object has a length () method.

Method: length (), charAt (), indexOf (), subString ()

A. String class

The String class contains an unchangeable String. Once a String object is created, the content contained in this object cannot be changed until the object is destroyed.

Method for creating a String object: a. String str = new String ("hello"); B. String str = "hello ";

B. StringBuffer class: content change allowed

How to Create a StringBuffer object:

A. StringBuffer str = new StringBuffer (""); str. append ("world ");

C. StringBuilder class

A. StringBuilder is a lightweight version of The StringBuffer class. Basically all StringBuilder methods are available in StringBuffer.

B. StringBuilder is thread unsafe, and StringBuffer is thread safe.

C. Performance: StringBuilder> StringBuffer> String

3. Date-related classes

Java provides a series of classes to form a standard Date and Time Processing class. The date data type is in the java. util package, and the date format class is in the java. text package.

A. SimpleDateFormat class: inherits from DateFormat

Translate a time object into a custom string and a custom string into a time object.

B. Calendar abstract class and GregorianCalendar class

Java. util. GregorianCalendar is a specific subclass of Calendar. It provides a standard Calendar system used by most countries in the world. It uses the GregorianCalendar class to obtain the time, instead of the Date class.

Note: The SimpleDateFormat class only supports objects of the Date type. To Format objects of the GregorianCalendar class, you must first drop the getTime () method of the GregorianCalendar object, then operate on it

4. mathematical operations

A. 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, you can directly use "Math. the method name () is called to facilitate the actual use of programmers.

B. Use the Random class as the Random number generator.

5. Common packages

A. Java supports basic data types and reference data types. In some applications, the basic data type is not required and the reference data type must be used.

B. the Java language provides us with a packaging class corresponding to the basic data type:

Byte ---- Byte Class

Short ---- Short class

Int ---- Integer class

Long ---- Long class

Float ---- Float class

Double ---- Double

Boolean -- Boolean class

Char ---- Character class

C. Common Characteristics of packaging:

The values contained in the packaging class are unchangeable and both have the XXXValue () method and valueOf () method. Both have two constructor methods.

6. Regular Expression:Is a method to describe a string using a specific symbol. That is, the regular expression is a String template, which is also a String and matches with the String matches ().

7. Class Loading Mechanism

A. Steps for loading classes:

A. class loading: This is completed by the Class loader. After the class file of the Class is read into the memory, a java. lang. class object is created. Once a class is loaded into JVM, the same class will not be loaded again.

B. Join: Merge binary data of the class into JRE.

C. Initialization: JVM initializes the class, that is, static attributes. In Java, you can specify an initial value for a static attribute in two ways: (1) specify an initial value when declaring a static attribute; (2) specify an initial value for a static attribute using a static initialization block.

8. Reflection

Java reflection technology is one of the characteristics of java programs. It allows running Java programs to check themselves, or "self-Review", and can directly manipulate internal properties of the program, using Reflection, you can obtain and display the names of each member in the Java class. In short, reflection is a technology that allows you to get objects (classes, attributes, and methods) by name.

A. Reflection steps:

A. Obtain the java. lang. Class Object of the Class.

1). How to obtain the Class Object:

Method 1: Class c = Class. forName (strg );

Method 2: Class c = JButton. class; or Class c = Integer. TYPE;

Method 3: Class c = Object Name. getClass ();

B. Information about inquiry classes: A class is usually composed of three parts: attributes, common methods, and constructor.

1). Filed class: Provides property information about the class or interface.

Common Filed methods:

Field getField (String name) obtains the specified public Field

Field [] getFields () obtains all public fields of the class.

Field getDeclaredField (String name) to obtain the specified Field

Field [] getDeclaredFields () obtains all fields declared by the class.

2). Constructor class: Provides information about a single Constructor of the class and its access permissions.

Common Constructor methods:

Constructor getConstructor (Class [] params) obtains the common constructors using special parameter types.

Constructor [] getConstructors () obtains all public constructors of the Class. Constructor getDeclaredConstructor (Class [] params) obtains constructors using specific parameter types (not related to the access level)

Constructor [] getDeclaredConstructors () obtains all constructors of the class (unrelated to the access level)

3). Method class: Provides information about a single Method (and how to access the Method) on the class or interface.

Common Method:

Method getMethod (String name, Class [] params) uses a specific parameter type to obtain the named public Method

Method [] getMethods () obtains all public methods of the class.

Method getDeclaredMethod (String name, Class [] params) uses a specific parameter type to obtain the Class declaration naming Method.

Method [] getDeclaredMethods () Get all methods declared by class

C. Use the reflection API to perform such operations.

There are three common operations using reflection technology:

1). Create an object through Constructor

2). Execute the Method

3). assign values to attributes through Field

 

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.