Java Learning Summary 1

Source: Internet
Author: User
Tags class manager modifiers

1. Breakpoint Debugging
A: Positioning (setting breakpoints)
B: Start Debugging
C: Stepping through the observation variable (F5 stepping F6 single Step)
D: Modify
2 static statics
static members, shared for all objects of the class
In a static method, only static members can be called directly and non-static members cannot be called directly
in member methods, you can call static and non-static members directly
call to static member: Class name Call
3 method overloads:
there are two and more than two method names in a class with different parameter lists (parameter type parameter number)
4 Constructors: initialization of objects
feature: The name of the constructor must be the same as the class name; no return value no return type
By default, there is a non-parametric construction method in the class
When we write a parametric constructor, the system default parameterless constructor is no longer generated. If......we need .....
5 Using this in the constructor
calling other constructors in the constructor
this (); (The This statement must be the first sentence of this constructor)

Method Summary
1 Char
charAt (int index)

Returns the char value at the specified index.
The index of the Index-char value.
2 string
Concat (String str)
Connects the specified string to the end of this string.
STR-string that is connected to the end of this string.

substring (int beginindex)
Returns a new string that is a substring of this string.
Beginindex-Start index (included).
3 Boolean
EndsWith (String suffix)
Tests whether this string ends with the specified suffix.
suffix-suffix.

StartsWith (String prefix)
Tests whether this string starts with the specified prefix.

Equals (Object AnObject)
Compares this string to the specified object.
AnObject-Object to compare with this String

Equalsignorecase (String anotherstring)
Compares this string to another string, regardless of case.
Anotherstring-string that is compared to this string.

4 int
indexOf (int ch)
Returns the index at which the specified character first appears in this string.
CH-one character

indexOf (int ch, int fromIndex)
Returns the index at the first occurrence of the specified character in this string, starting the search from the specified index.
CH-a character FromIndex-the index at which to start the search.

IndexOf (String str)
Returns the index at which the specified substring first appears in this string.
str-arbitrary string

IndexOf (String str, int fromIndex)
Returns the index of the first occurrence of the specified substring in this string, starting at the specified index.
STR-the substring to search for. FromIndex-the index location at which to start the search.
public int lastIndexOf (int ch)
public int lastIndexOf (iint ch, int fromIndex)
public int lastIndexOf (String str)
public int lastIndexOf (String str, int fromIndex)

Length ()
Returns the length of this string.

method Call Stack

All methods are maintained in a structure called the call stack, and the currently executing method is located in the call stack.

For a basic type of variable: the Java Virtual machine puts it on the stack.

For a variable of reference type: The reference variable itself is placed in the stack, and the object pointed to by the reference is placed in the Java heap. The reference does not contain the actual data of the object it points to, but instead points to the location of the object in memory.

If a variable is a reference type, the value of the variable is a memory address, which is the location of the object to which the reference refers.
Call by value


Refers to when a method call passes a parameter that is passed by a copy of the value.

Examples are as follows:

public class Temptest {

private void test1 (int a) {

Do something.

}

public static void Main (string[] args) {

Temptest t = new temptest ();

int a = 3;

T.test1 (a);//The parameter a passed here is passed by value

}

}

Passing important characteristics by value: Passing a copy of a value, that is, passing it off is unrelated.

Examples are as follows:

public class Temptest {

private void test1 (int a) {

A = 5;

System.out.println ("a=== in the Test1 method" +a);

}

public static void Main (string[] args) {

Temptest t = new temptest ();

int a = 3;

T.test1 (a);//After passing, the Test1 method changes the value of the variable without affecting the A

System.out.println ("a=== in the Main method" +a);

}

}

The result of the operation is: A===5 in the Test1 method

A===3 in the Main method


3.2: By reference what is referred to is when a method call, the passed parameter is passed by reference, in fact, the address of the reference to pass, that is, the variable corresponding to the memory space address.

3.3: An important feature passed by reference is a reference to a value, meaning that both before and after delivery point to the same reference (that is, the same memory space).

The example is as follows: line 1th public class Temptest {

private void Test1 (a a) {

A.age = 20;

System.out.println ("age= in the Test1 method" +a.age);

}

public static void Main (string[] args) {

Temptest t = new temptest ();

A = new A ();

A.age = 10;

T.test1 (a);

System.out.println ("age= in the Main method" +a.age);

}
}
Class a{
public int age = 0;

}

The results of the operation are as follows: Age=20 in Test1 method

Age=20 in the Main method

(1): "In Java parameter passing is passed by value" This sentence means: Pass by value is a copy of the value passed, by reference is actually passed the value of the referenced address, so collectively by value passed.

(2): There are only basic types in Java and strings that are defined in the following way are passed by value, others are passed by reference. is to define the string directly using double quotation marks: string str = "Java private";

Overloading of methods

When a class has two or more methods with the same name but a different argument list.
Examples of overloading are shown in the following example:

void Getarea (int w,int h);

void Getarea (float w,float h);

In the second case, the member Method Getarea () accepts two floating-point variables as its arguments, and the compiler determines which member method to call based on the different parameters that are called, and if you give two integers to the member method, the first member method is called; if you give two floating-point numbers to the member method, A second member method is called. When writing code to invoke one of these methods, it chooses the appropriate method based on the type of argument provided.
Note: As with the member method, the construction method can also be overloaded.
2.2: Overloaded rules for methods
(1): Method names must be the same

(2): The parameter list must be different (the number is different, or the type is different, or the parameter is arranged in different order).

(3): The return type of the method can be the same or different. Only the return type is not sufficient to be an overload of the method.
Array

An array is a collection of data that consists of several items of the same type. That is, the array is used to assemble objects of the same type and to refer to the collection by a name, and the array is a reference type.

declaration method:

Declaration one: int x[];

Statement two: int [] x=new int[3];

Declaration three: Int[]x=new int[]{3,4,5};

The method of array copying is to use the Arraycopy () method provided by the system class with the following syntax:
System.arraycopy (object src, int srcpos, object dest, int destpos, int length);
System.arraycopy (source array, starting position in source array, target array, starting position in target data, number of array elements to copy);
public class T {
public static void Main (String args[]) {
int arr1[] = {1,2,3,4,5};
int arr2[] = new INT[5];
System.arraycopy (arr1, 0, arr2, 0, 5);

for (int i = 0; i < arr2.length; i++) {
System.out.println (Arr2[i]);
}
}
}
Arrays Class
Some basic operations of an array, such as sorting, searching, and comparing, are common. The array is provided in Java to assist you with these operations, array is a class located in the Java.util package, and he provides several methods that can be used directly.

Sort () helps you sort the specified array by using the Quick Sort method


BinarySearch () allows you to search a sorted array for $ two, and returns the index of the value if the specified value is found, otherwise a negative number is returned

Fill () When you configure an array, the default value is given according to the data type. For example, an array of integers has an initial value of 0, and you can use the Arrays.fill () method to set all elements to the specified value

Equals () compares the values of the elements in the two array if they are all equal, and returns true if they are, otherwise false

Inheritance

public class Employee {

String name;

Date HireDate;

Date dateOfBirth;

String JobTitle; int grade;

... }

public class Manager extends Employee {

String Department;

Employee[] Subordinates;

... }

In such a definition, the Manager class is defined with all the variables and methods owned by the Employee. All of these variables and methods are inherited from the definition of the parent class.

The key word is extends.

In the Java programming language, the initialization of objects is very structured, and this is done to ensure security. In the previous module, you saw what happened when a particular object was created. Because of inheritance, objects are completed, and the following behaviors occur sequentially:

(1) The storage space is allocated and initialized to a value of 0

(2) Perform an explicit initialization

(3) Call construction method

Keyword Super

The keyword super can be used to refer to the parent class of the class, which is used to refer to a member variable or method of the parent class. The behavior of the parent class is called as if the behavior is the behavior of this class, and the invocation behavior does not have to occur in the parent class, it can automatically trace back to the upper class.
Features of the Super keyword:

(1): Point to a data member that is hidden by the quilt class in the parent class

(2): Take a method that is already covered

(3): A method name represents the parent class construction method

For example:

public class Employee {

private String name;

private int salary;

Public String getdetails () {

Return "Name:" + name + "\nsalary:" + salary;}

}

public class Manager extends Employee {

Private String Department;
Public String getdetails () {

return super.getdetails () +//Call the method of the parent class "\ndepartment:" + department;

}

}

Construction method cannot be inherited

Although a subclass inherits all methods and variables from the parent class, it does not inherit the constructor method, which is important to know. A class can be constructed with only two methods. or write a constructor method, or do not write the constructor method at all, the class has a default constructor method.

Calling the parent class construction method

In many cases, the default constructor method is used to initialize the parent class object. Of course, you can also use super to display the constructor method that calls the parent class.
public class Employee {

String name;

Public Employee (String N) {

name = N;

}

}
public class Manager extends Employee {

String Department;

Public Manager (string s, string d) {

Super (s);

department = D;

}

}

Note: Both super and this must be placed on the first line of the construction method.

Single inheritance

Single inheritance: When a class inherits from a unique class, it is called single inheritance. Single inheritance makes your code more reliable. Interfaces provide the benefits of multiple inheritance, and there are no (multiple inherited) drawbacks. The Java programming language allows a class to inherit only one other class, that is, a class can have only one parent class. This restriction is called single inheritance. The advantages of single inheritance and multiple inheritance are topics that are widely discussed among object-oriented programmers. The Java programming language strengthens the single inheritance limit and makes the code more reliable, although this sometimes increases the programmer's work. Later, a language feature called Interface (interface) is learned that allows most of the benefits of multiple inheritance without being affected by its drawbacks.

Debug Breakpoint Debug

1. Positional logic error (set breakpoint)

2, start debugging

3, single step, observation variables

4. Modification

There is a difference between null and "", which allocates space

Calling methods

When a method is executing, there are three cases

1. method returns a value

2. method does not reverse a value

3. Method throws an exception to the caller (later specifically discussed)

Method signature

Method signatures include information such as access modifiers, optional modifier, method name, parameter list, data type of the return value, and so on

For example: public static double Random ()

Access modifiers

Public, private, protected

Optional modifiers

Static, final, abstract, native, synchronized.

Static-decorated methods are statically members and are shared for all objects of the class

In a static method, only static members can be called directly, and non-static members cannot be called directly

A call to a static member called by a class name dot

In member methods (methods that are not modified by static), you can call static and non-static members directly

Formal parameters and actual parameters call method overloads by value (really a damn witty way, also called static polymorphism)

Method overloads are used when a class has two or more methods with the same name but has a different argument list.

Overloading a method overloads the method as long as the form parameter list of the method is completely different to the compiler.

Associated with the type of the parameter, regardless of the name.

For example: GetDay (int year) and getDay (int day) are not method overloads

In println (), you can call different methods if you enter different parameters.

Constructor person SomeOne = new person ();

Note: The name of the construction method must be the same as the class name, and the construction method has no return type

By default, there is a non-parametric construction method in the class

For example: public person () {

}

And we can initialize the data in this constructor method.
In addition, we can create a construction method with parameters.
Note: If we write a parametric constructor, the system will no longer produce a parameterless constructor, and if you want to use it, you must re-write it yourself.
A constructor is a special method that is called to execute when a class creates an instance.
The purpose of the constructor is to initialize an instance of the class so that it becomes a valid state.
Whenever an object is created, the constructor calls the other constructors in the same class, using the This statement (which must be the first sentence in the construct statement).
For example: public person (int Stunum,char name) {
}
Public person (Int. Stunum,char name,string age) {

This (stunum,name);

}
Compare the size of a string
Ignores case. Equalsignorecase ();

Java Learning Summary 1

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.