Use of placeholders in Java
The format () method of the String class
The format () method of the string class is used to create a formatted string and to concatenate multiple string objects. Students familiar with C language should remember the C language of the sprintf () method, the two have Similarities. There are two overloaded forms of the format () method.
Format (string format, Object ... Args) a new string that uses the local locale to develop a string format and parameters to generate a new formatted string.
Format (locale locale, string format, Object ... Args) uses the specified locale to develop string formatting and parameters to generate a formatted string.
public staticvoidmain (string[] Args) {String str=NULL; STR=string.format ("hi,%s", "wang li"); System.out.println (str); STR=string.format ("hi,%s:%s.%s", "wang nan", "li li", "wang zhang"); System.out.println (str); System.out.printf ("the capitalization of the letter A is:%c%n", ' a '); System.out.printf ("the result of 3>7 is:%b%n", 3>7); System.out.printf ("half of 100 is:%d%n", 100/2); System.out.printf ("100 of 16 binary number is:%x%n", 100); System.out.printf ("100 of 8 binary numbers are:%o%n", 100); System.out.printf ("50 Yuan Book 8.5 discount is:%f yuan%n", 50*0.85); System.out.printf ("16 of the above price is:%a%n", 50*0.85); System.out.printf ("the Index of price above means:%e%n", 50*0.85); System.out.printf ("the index of the above price and the length of the floating-point result is shorter:%g%n", 50*0.85); System.out.printf ("the above discount is%d%%%n", 85); System.out.printf ("the hash code for the letter a is:%h%n", ' a '); }
%s String Type
%c Character Type
%b Boolean type
%d integer type (decimal)
%x integer type (hex)
%o integer type (octal)
%f floating-point types
%a hexadecimal floating-point type
%e Index Type
%g Common floating-point types (shorter in type F and E)
%h Hash Code
percent percent Type
%n line Break
%tx date and Time type (x represents a different date and time conversion character
Modifiers for Java
Java has four types of access restriction modifiers: private,public,protected, default
The package (friendly is the default, and is not a keyword in java.)
Private: personal, Internal access, well understood
Public: shared, accessible, Better understood
Package (friendly: can be accessed with a single packet)
Protected: the same package or subclass can be accessed
class modifier |
Public |
can be accessed from other classes |
Abstract |
This class cannot be instantiated |
Final |
Subclass can no longer be declared |
constructor modifier |
Public |
can be accessed from all classes |
Protected |
can only be accessed from its own class and its subclasses |
Private |
can only be accessed in this class |
Class modifiers are integrated
A Class
Modifiers for the Class:
Public: can be used in any other class, default to any class under the uniform Package.
Abstraction: abstract class, cannot be instantiated, can contain abstract methods, abstract methods are not implemented, no specific functions, can only derive subclasses.
Final: cannot be Inherited.
Two Variable
Variable modifiers:
The declaration of a member variable of a class must be in the class body, not in a method, and a local variable is declared in the Method.
1. Accessible modifiers:
2. Static: class variable: A variable owned by a class, not a variable for each instance of the class. A class variable is one that allocates memory for a class variable only the first time the class is called, regardless of how many objects the class creates, and all objects share the class variables of that class, so the class variable can be accessed through the class itself or an Object.
3. Final: Constant.
4. Volatile: declares a variable that is controlled and modified by several threads that may be concurrently running.
Instance variable: corresponds to a class variable, that is, each object has its own independent instance Variable.
Three Methods: (and variable objects are divided into example methods and class methods, and with no static modification of the Difference)
Class Methods: methods described using the static keyword
1. The first call to a class with a class method is that the system only creates a version of the class that is shared by the class and all instances of the class.
2. Class methods can only manipulate class variables and cannot access instance Variables. A class method can be called in a class without having to create an instance to invoke, or it can be called through an object.
Instance Methods: instance methods can operate on instance variables of the current object, and can access class Variables.
Methods can be overloaded and require the same method name, but the parameters must be Different. (different parameters can make different types, different order, the number of Different)
The return type of the method: if there is no return type, it is declared VOID.
Variable scope in the Method:
1. Member Variables: the entire class.
2. Local variables: defines the end of the method Block.
3. Method Parameters: the entire method or construction Method.
4. Exception Handling Parameters: parameters are passed to the exception handling Method.
Constructor method: a method with the same name as the class. Initializes the newly created object after creating a memory space for the new Object. cannot be explicitly called with an object.
Static Initializer: format:static{< Assignment Statement group;}
Java placeholder usage and modifiers