Java Basics
“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.
1. Basic definition of methods
Limitations: Methods described in this article are defined in a main class and called directly by the main method.
A method is a block of code that can be called repeatedly. If you want to define a method in Java, you can use the following method to do so.
public staticMethod return value Method name ([parameter type variable,....] ) {method body code;return[return value]; }Copy the code
When a method is defined, the return value of the method is divided into two classes: void There is no return value or data type (base type, reference type).
public class TestDemo{
public static void main(String args[]){
If the method is to be called inside the main method, it must be decorated with static
print(); // call directly from the main method
print(); // call directly from the main method
print(); // call directly from the main method
}
public static void print(a){
System.out.println("Hello,World!"); }}Copy the code
It is important to note, however, that when the return value is void, the call can be terminated by a return. Use in general and if judgments.
public class TestDemo{
public static void main(String args[]){
If the method is to be called inside the main method, it must be decorated with static
print1(10); // call directly from the main method
print1(20); // call directly from the main method
print1(30); // call directly from the main method
}
public static void print(a){
System.out.println("Hello,World!");
}
public static void print1(int x){
if(x == 20) {// indicates the end of the method
return ; // The code after this statement is not executing
}
System.out.println("x = "+ x); }}Copy the code
2. Method overload
Method overloading is when methods have the same name but different types or numbers of parameters.
public class TestDemo2{
public static void main(String args[]){
If the method is to be called inside the main method, it must be decorated with static
System.out.println(add(10.20)); // call directly from the main method
System.out.println(add(10.20.30)); // call directly from the main method
System.out.println(add(10.1.20.1)); // call directly from the main method
}
public static int add(int a,int b){
return a + b;
}
public static int add(int a,int b,int c){
return a + b + c;
}
public static double add(double a, double b){
returna + b; }}Copy the code
There’s an important rule to follow when overloading methodsThe return type of the method must be the same.
By using system.out.println () to find that all types can be printed, we can see that this method is overloaded.
3. Recursive calls to methods
Recursive invocation of a method refers to a method calling its own form. Recursive operations of methods often have the following characteristics
- A method must have a recursive termination condition
- Methods must change each time they recurse
Calculate 60!
public class TestDemo4{
public static void main(String args[]){
System.out.println(mul(60));
}
public static double mul(double num){
if (num == 1) {return 1;
}
return num * mul(num - 1); }} Calculation results:8.320987112741392 e81
Copy the code
In fact, we can use recursion for most of the while loop operations, and recursion is because the main method can perform many operations, and the structure is simple and good.
Object oriented predecessor is process oriented
The difference between the two: general will be the best example of process oriented is problem solving, object-oriented is modular design. For the current process is like car assembly, different factories produce different parts, these parts can be put together to form a car, when our parts break down can be fitted.
Object orientation actually has many other features
- Encapsulation: Internal operations are invisible to the outside.
- Inheritance: to build on the previous generation.
- Polymorphism: this is the most important part of our design.
Three stages: OOA(Object-oriented analysis), OOD(Object-oriented Design), OOP(object-oriented programming) terminology
5. Classes and objects
Classes and objects are at the heart of object orientation and the foundation of all concepts. Classes belong to our reference type, so their use has implications for our memory allocation
A class is a universal concept, while an object is a concrete thing that can be used.
Class composition: methods (action behavior), attributes (variables, describing the specific characteristics of each object).
The definition of a class is usually declared by class
classThe class name{properties1; // Attributes can be multipleattribute2; }Copy the code
Instead of being called by the main class, the method is called by the object.
Declare the instantiated object
class Person{ // To define a class, begin by capitalizing each initial of the class name
public void info(a){
System.out.println("name = "+ name + "\nage = "+ age); }}public class TestDemo5{
public static void main(String args[]){
// Instantiate objects in the first way
Person person = new Person();
person.name = "Zhang"; // Sets the value of the property in the object
person.age = 13; // Sets the value of the property in the object
person.info();
// Instantiate objects in the second way
Person person1 = null;
person1 = newPerson(); person1.info(); }}Copy the code
The biggest feature of reference data types is the allocation of memory. Whenever the keyword new appears, there is only one explanation: open up new memory (memory cannot be opened up indefinitely, so the so-called performance tuning in this case is memory).
Memory analysis
The memory space we use is divided into two parts: heap memory space (where the real data is stored, where the attributes of the object are stored) and stack memory space (where the address of the heap memory is stored, the operation rights of the heap memory, which is simply called the name of the save object). All data types must be used after the space is opened. NullPointerException is generated only when a reference data type (array, class, interface) is used. If a NullPointerException occurs, the object is instantiated based on the error position.
reference
The essence of reference passing is the alias, and the alias is just in our stack memory. One heap memory can be pointed to by multiple stacks.
class Person{ // To define a class, begin by capitalizing each initial of the class name
String name;
int age;
public void info(a){
System.out.println("name = "+ name + "\nage = "+ age); }}public class TestDemo5{
public static void main(String args[]){
// Instantiate objects in the first way
Person per = new Person();
per.name = "Zhang"; // Sets the value of the property in the object
per.age = 13; // Sets the value of the property in the object
per.info();
// Instantiate objects in the second way
Person per1 = null;
per1 = new Person();
per1.name = Less than sub;
per1.age = 30;
per1.info();
// This step is the operation passing by reference
Person per2 = per1;
per2.name = "The dog left"; per1.info(); }}Copy the code