1. Method (i.e. function)
Definition: A block of code that accomplishes a specific function
Format: Modifier/Public Static/return value type method name (argument type parameter name 1, ){
Method body;code to complete the function/
return value;return result of the function/
}
Two explicit: The parameter type of the return value result
Number of parameter parameters, and type
Considerations for Methods
A: Method does not call do not execute
B: Methods and methods are lateral relationships, cannot be nested defined
C: When the method is defined, the arguments are separated by commas
D: Do not pass the data type at the time of method invocation
E: If the method has a definite return value, it must be returned with a return
2. Method overloading
Definition: In the same class, more than one method of the same name is allowed, as long as the number of arguments or the type of the parameter are different
Characteristics:
A. Regardless of the return value type, see only the method name and argument list
B. At invocation time, the virtual machine distinguishes the method with the same name by different argument lists
3. Arrays
Definition: An array is a collection of multiple elements that store the same data type, that is, a container that stores the same type of data.
Arrays can store either the base type or the reference type
Format:
A: Data type [] array name;
Int[] arr;
An array of type int arr
B: Data type array name [];
int arr[];
arr[of type int]
Initialization
Arrays in Java must be initialized before they can be used.
So-called initialization
is to allocate the memory space for array elements in the arrays and assign values to each array element.
A. Dynamic initialization: Only the array length is specified when initializing, and the initial value is assigned by the system array.
data type [] Array name = new data type [array length];
B. Static initialization: Specifies the initial value of each array element when initialized, and the length of the array is determined by the system.
data type [] Array name = new data type []{element 1, element 2,...};
Simplification: data type [] Array name = {element 1, element 2,...};
Get array element: Array name [index];
Two common exceptions for arrays
A. ArrayIndexOutOfBoundsException: Array index out-of-bounds exception
A non-existent index was accessed
B. NullPointerException: null pointer exception
Reference no longer points to heap memory, variable may be null
The calendar of the array: Each element in the output array
Array Traversal standard format:
for (int x=0; x< array name. length; x + +) {
SYSTEM.OUT.PRINTLN (array name [x]);
}
4. Class
Definition: Used to describe things in the real world
Composition: A feature of a member variable (a variable outside of a method in a class)
Member method (no Static-modified method) function
The difference between a local variable and a member variable:
A: The position in the class is different
Outside the method in the member variable class
Inside a local variable method or on a method declaration
B: The location in memory is different
Member Variable heap memory
Local variable stack memory
C: Different life cycle
The member variable exists as the object exists and disappears as the object disappears
Local variables exist with the invocation of the method and disappear as the call to the method finishes
D: Initialize Value
Member variables have default initialization values
Local variables do not have default initialization values
5. Encapsulation
Definition: Refers to the properties and implementation details of hidden objects, and provides public access only externally.
Advantages:
A: Improved reusability of the code
B: Improve security
Reflected:
Hide content that you don't need to provide externally.
Practice:
Hides the property and provides a public way to access it.
Private (proprietary) keyword function: You can modify member variables and member methods, the content that is private decorated, can only be accessed in this class. This keyword function: Demonstrates the effect of modifying the variable name of a local variable and the same as a member variable, if a local variable has a variable and member variable name, then precedence is used for local variables. Nearest principle.
6. Construction method:
function: Initialize the data of the object
format features:
a: The method name is the same as the class name.
public void Student () {}
&NBSP;B: no Return The return value type.
Modifiers return value type method name (...) {...}
&NBSP;C: no return value.
does not return with a definite value return.
return;
considerations
a: If you do not provide a constructor method, the system will give the default parameterless construction method
b: If you provide a construction method, the system will no longer provide a default parameterless construction method
a: Use your own given parameter constructs.
&NBSP;B: Either provide a non-parametric construct yourself
It is best to give a non-parametric construction method yourself. Assign A value to A member variable: A: Create an object from A parameterless construct and use the Setxxx () method
To assign a value
B: By means of the parameter construction method
7.static keywords
Role: You can modify member variables and member methods
Characteristics:
A: Loaded with the load of the class
B: Precedence over object existence
C: Being shared by all objects of a class is also the basis for us to decide whether to use static retouching of a data.
D: Can be called by the class name
Static variables: class variables
Non-static variables: instance variables, object variables
Non-static: Create object access
Static: Can be accessed through the class name or through the object.
Precautions:
A: There is no this keyword in the static method
Cause: Static content is loaded with the load of the class, and this is present as the object is created, so there cannot be this in static.
B: Static methods can only access static member variables and static member methods
A static method can only access static members.
8.String
Definition: is a string of data consisting of multiple characters. (character sequence)
Construction Method:
Public String (): No parameter construction method
public string (byte[] bytes): Converts a byte array to a string
public string (char[] value): Converts a character array to a string
public string (char[] Value,int offset,int count): Converts part of a character array to a string
public string (string original): Converts a string to a string
Member methods of the String class
A: Judging function
Boolean equals (Object obj): Compares the contents of two strings with the same, strictly case-sensitive. (user name, password)
Boolean equalsignorecase (String str): Compares whether the contents of two strings are the same, ignoring case. (Verification Code)
Boolean contains (String str): Determines whether a substring is contained in a string.
Boolean startsWith (String str): Determines whether to start with the specified string
Boolean endsWith (String str): Determines whether to end with the specified string
Boolean IsEmpty (): Determines whether the contents of the string are empty
B: Get Features
The Get function of the string class:
int length (): Returns the length of the string. is actually the number of characters.
char charAt (int index): Returns the character at the specified index in the string.
int indexOf (int ch): Returns the index of the first occurrence of the specified character in the string.
The character is clearly said, why is it int?
The reason is that the int type can also receive the char type.
The ' a ' is the same effect.
But if the argument is a char type, you can't write 97.
int indexOf (String str): Returns the index of the first occurrence of the specified string in the string.
string substring (int start): Intercepts a string from start to end.
string substring (int start,int end): Intercepts a string from start to end.
C: Conversion function
Byte[] GetBytes (): Converts a string to a byte array
Char[] ToCharArray (): Converts a string to a character array
static string ValueOf (char[] CHS): Converts a character array to a string
static string valueOf (int i): Converts data of type int to string
ValueOf (): You can convert any type of data to a string.
String toLowerCase (): Turns the string to lowercase
String toUpperCase (): Turns the string into uppercase
String concat (String str): stitching string, before we used + string concatenation, not professional.
D: Other functions
A: Replace function
String replace (char Old,char new)
String Replace (string old,string new)
B: Remove whitespace at both ends of string
string trim ()
9. StringBuffer:
Construction method:
stringbuffer (): Constructs a string buffer with no characters, with an initial capacity of 16 characters.
stringbuffer (int capacity): Constructs a string buffer with no characters, with an initial capacity of capacity characters.
stringbuffer (String str): Constructs a string buffer with characters whose initial capacity is a character.
Member Methods:
public int Length (): Returns the length (number of characters). Actual value
public int Capacity (): Returns the current capacity. Theoretical value
Add Features: Add elements and return objects of their own.
A:public stringbuffer Append (String str): Append data, add at the end
B:public stringbuffer Insert (int offset,string str): Insert data, add at specified location
Remove Features:
Public stringbuffer deletecharat (int index): Deletes the character at the specified index
Public StringBuffer Delete (int start,int end): Delete data from start to end, package left without packet right
Replacement function:
Public stringbuffer replace (int start,int end,string str): Replace data from start to end with Str
Invert function:
Public StringBuffer Reverse ()
Interception function: The return value type is a string type and does not change itself
Public String substring (int start)
Public String substring (int start,int end)
Convert each other:
String–stringbuffer
String s = "Hello";
Mode 1
StringBuffer sb1 = new StringBuffer (s);
Mode 2
StringBuffer SB2 = new StringBuffer ();
Sb2.append (s);
Stringbuffer–string
StringBuffer sb = new StringBuffer ("World");
Mode 1
String S1 = sb.substring (0);
Mode 2
String s2 = sb.tostring ();
Mode 3
String s3 = new String (SB);
Java Fundamentals 2