Java Common classes and reflections, class loading

Source: Internet
Author: User
Tags mathematical constants

1. System-related classes

Java provides the system class and runtime class to interact with the platform that the program runs on

The A.system class represents the running platform of the current Java program

A. The system class is a final class, and all properties and methods of the class are static. You can call directly without creating an object

B. Properties: In, out, err

C. Common methods: Currenttimemillis (), exit (), GetProperties (), GC ()

B. Runtime class representing Java programs

Note: The application cannot create its own runtime instance, but the runtime object associated with it can be obtained through the static method GetRuntime () of the runtime class

A. Current number of processors: rt.availableprocessors ()

B. Free Memory: rt.freememory ()

C. Total Memory: rt.totalmemory ()

D. Maximum available memory: rt.maxmemory ()

2. String-related classes

Strings, StringBuffer, StringBuilder, and other classes are provided in Java to encapsulate the string, which in turn provides a number of ways to manipulate string objects

Note: Every character in a string in Java has an index value starting at 0, with each string object having a length () method

Method: Length (), CharAt (), IndexOf (), subString ()

A. String class

The String class contains a string that is immutable. Once a string object is created, the content contained in the object is immutable until the object is destroyed

How to create a String object: A.    String str = new string ("Hello"); b. String str = "Hello";

B. StringBuffer class: Allow content to change

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, StringBuffer has a method StringBuilder basic

B. StringBuilder is thread insecure, StringBuffer is thread-safe

C. Performance: StringBuilder > StringBuffer > String

3. Date-related classes

The Java language provides a series of classes that collectively form the standard DateTime processing class. Where the date data type is in the Java.util package, and the date formatting class is in the Java.text package

A. SimpleDateFormat Class: Inherited from DateFormat

Translates a time object into a custom-formatted string, translating a custom-formatted string into a Time object

B. Calendar abstract class and GregorianCalendar class

Java.util.GregorianCalendar is a specific subclass of the calendar that provides the standard calendar system used in most countries of the world, using the GregorianCalendar class to obtain time, rather than using the date class

Note: because the SimpleDateFormat class only supports objects of date type, if you want to format an object of the GregorianCalendar class, you must first drop the gettime of a GregorianCalendar object () method, and then manipulate it

4. Mathematical Operations related classes

A. The math class is a mathematical tool class in which common mathematical constants and mathematical methods are inside the class, and both constants and methods are static types that can be called directly through the "Math. Method name ()", making it easy for programmers to actually use

B. The random class is made as a stochastic number generator

5. Common Packing Class

A. The Java language is divided into basic data types and reference data types. In some applications where basic data types cannot be used, reference data types must be used

B. The Java language provides us with wrapper classes that correspond to the basic data types:

BYTE----byte class

Short----the short category

int----Integer Class

Long----a long class

Float----Float Class

Double----Double Class

boolean--Boolean class

Char----character class

C. The common denominator of the packaging category:

The values contained in the wrapper class are immutable, with the following: Xxxvalue () method, ValueOf () method, all two construction methods

6. Regular Expressions: is a way to describe the set of strings using a specific symbol. That is, the regular expression is a string template, which is itself a string, using the string matches () to match

7. Loading mechanism for classes

A. Steps for class loading:

A. Class loading: Completed by the ClassLoader, a class file is read into memory and a Java.lang.Class object is created. Once a class is loaded into the JVM, the same class will not be loaded again

B. Connection: Merging the binary data of the class into the JRE

C. Initialize: The JVM is responsible for initializing the class, that is, initializing the static property. In a Java class, there are two ways to specify an initial value for a static property: (1) Specify an initial value when declaring a static property, or (2) specify an initial value for a static property using a static initialization block.

8. Reflection

Java's reflection technology is one of the characteristics of Java programs, it allows the running Java program to check itself, or "self-audit", and can directly manipulate the internal properties of the program, using reflection can get the name of each member of the Java class and display it. Simply put, reflection is a technique that allows you to get objects (classes, properties, methods) by name.

A. Reflection use steps:

A. Obtaining a Java.lang.Class object for a class

1). How to get the class object:

Mode one: Class c = class.forname (STRG);

Mode two: Class c = jbutton.class; or Class C = integer.type;

Method Three: Class c = object name. GetClass ();

B. Exploring the information of a class: A class is usually made up of three parts--attributes, common methods, construction methods

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

Common methods of the filed class:

Field GetField (String name) gets the specified public field

Field[] GetFields () gets all the public fields of the class

Field Getdeclaredfield (String name) gets the specified field

Field[] Getdeclaredfields () Get all the fields declared by the class

2). Constructor class: Provides information about a single construction method of a class and access to it

Common methods of the constructor class:

Constructor GetConstructor (class[] params) get public constructors that use special parameter types

Constructor[] GetConstructors () gets all public constructors for the class Constructor Getdeclaredconstructor (class[] params) to obtain a constructor that uses a particular parameter type (with access-level No matter what)

Constructor[] Getdeclaredconstructors () gets all constructors for the class (regardless of access level)

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

Method Class Common methods:

Method GetMethod (String name, class[] params) uses a specific parameter type to obtain a named public method

Method[] GetMethods () gets all public methods of the class

Method Getdeclaredmethod (String name, Class[]params) uses a specific parameter type to get the named methods of the class declaration

Method[] Getdeclaredmethods () Get all methods of class declaration

C. Use the reflection API to manipulate this information

There are three common operations that use reflection technology:

1). Creating objects from constructor

2). Method execution

3). Assign a value to a property by field

Java Common classes and reflections, class loading

Related Article

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.