This chapter mainly discusses static variables, static methods, final keywords. and describes how to format the output of numbers and dates. These things are sorted out here. This chapter also describes the Java.util.Date and Java.util.Calendar to operate the date, but this direct look at the API is relatively clear, this is not written. Static static marked, regardless of the instance object of the class static method:use the static keyword to mark the Statics method. Some methods do not need to use instance variables of a class, and static variables represent "a behavior that does not require an object without relying on an instance variable." Math is a static method Call static method: Class name. Method name (parameter) For example:Public class math{public static int min (int a,int b) {//Return small values of A and B }} The call does not need to be instantiated, and direct math.min (12,23) can Static variables:variables marked with the static keyword, such as private static int num=0; some points to note:1. A static method cannot invoke an instance variable (because a static method is independent of a particular instance, so it does not know which instance variable is called)2. Static methods cannot invoke non-static methods, but static variables can be called3. A static method can also be called using a reference variable, but the actual parser will parse the original class first, and it is not recommended to refer to the static variable4. Static variables are shared and are initialized only when the class is first loaded, and all instance variables of the same class share a static variable (each instance sees the same static variable value). Instance variables one for each instance, one for each class of static variables5. Static variables are initialized before any objects of the class are created, and are initialized before any static methods of the class are executed6. If the static variable is not assigned a value, the default value is determined based on the variable type. 7. Static variables are placed in the static variable area (storing constants, static variables). Java memory is divided into 4 extents, the static variable area and the stack, the code area is tied, not the dependency8. Static methods are placed in the memory's method area9. You can import static methods with import so that you can call a static method with less content, but it is easy to create a name conflict (easily duplicate names) Final final modified, cannot change content static final variable is constant, cannot change content1. The constant naming specification is capitalized, separated by underscores2. Constants must be assigned in two different ways. Assign a value at static initialization by using =, or final static{}For example:1.public static final double pi=3.14159262.public static final double PI;//The following section will be executed when the class is loadedstatic{pi=3.1415926 } All final modified content cannot be modified or used in methods, instance variables, classes1.final variable cannot be changed value (final int size=3)2.final method cannot be overwritten override (Final void Calcwhuffie () {})3.final class cannot be inherited (final class mymostperfectclass{}) Numeric OperationsArrylist can only be placed into objects and cannot be placed directly into a primitive type. The primitive data type needs to be packaged and re-routed, including when taking values from the ArrayList, as well as unpackingthe primitive type needs to pack and unpack itself before JAVA5, and the primitive type is automatically packaged after Java5. The math method is that some static methods, such as Math.Abs (), are taken as absolute values. Formattingformat using the format () method of String. Formatting consists mainly of two parts: a formatted instruction, a value to be formatted 1. Formatting instructions:format () the first parameter in% and subsequent syntax (format of the first parameter is to output the string content, to be parameterized where to use% to replace) number formatting directives:there will be a maximum of 5 parts (excluding%), and the following [] are optional items, only% and type are required, the order of formatting is specified and must be in the following order%[argument Number][flags][width][.precision]type
| Argument number |
If there are multiple parameters that need to be formatted, you can choose which one to format |
| Flags |
Specific options for type, such as "," for a number, to split the integer part (for example, 10000 to 10,000) |
| Width |
Minimum number of characters limit, output character can exceed this width, if less than width will be automatically complete |
| . Precision |
Accuracy, there is a point in front. For example, 2f is the two digits after the decimal point. |
| Type |
The type of parameter to format, for example, D is an integer, and F is a floating-point number |
date formatting directives:
| %tc |
Full date and Time (String.Format ("%TC", new Date ());) |
| %tr |
Show Time Only |
| %ta |
Just a few weeks. |
| %tb |
Return month |
| %td |
Return Day |
If you use the same parameters consecutively, you can use%< to tell the formatter to reuse the last used parameterFor example:Date Today=new date ();String.Format ("%ta,%<tb,%<td", today); 2. What to format:the second, third, nth parameter after format (). Used to put it in the previous% format Directive . examples of formatting are as follows:String S=string.format ("The number is%,.2f", 476578.09876);the output S is 476,578.09
Java Foundation Five [digital and Static] (read head first Java record)