How to Use cainiao notes in java, and cainiao notes in java
N !!! Use of methods without parameters and return values in java
1. Definition Method
Eg: public void show (){
System. out. println ("HelloWorld! ")
}
--- Methods must implement specific operations in a pair of braces
--- Naming Convention: the first letter is lowercase, and the first letter is uppercase.
--- Call A method. First, create an object, and then operate through the object name. Method Name;
Eg: public class HelloWorld {
Public static void main (String [] args ){
HelloWorld hello = new HelloWorld (); // create an object named hello
Hello. show (); // call object
}
Public void show () {// define a method
System. out. println ("HelloWorld! ");
}
}
N !!! Use of the return value method without parameters in java
Defines a method that has no parameters but returns the int type.
Public int add (){
Int a = 5; int B = 4;
Int sum = a + B;
Return sum;
} // The return value in the add method is of the int type. Therefore, you must use return to return an integer in the method body.
--- When a method with a returned value is called, a result is returned after the method is executed. Therefore, when a method with a returned value is called, the returned value is generally received and processed.
Pay attention to N in the following situations !!
--- If the return type of a method is void, return values cannot be used in the method.
--- A method can return a maximum of one value.
--- The method return value type must be compatible with eg. If the returned type is int, the String type cannot be returned.
N !!! Use of methods with no return value in java
Eg: public void show (String name ){
System. out. println ("Welcome" + name + "! ");
}
HelloWorld hello = new HelloWorld ();
Hello. show ("nsu ");
The output is Welcome nsu!
--- When calling the method with parameters, make sure that the quantity, type, and sequence of the real parameters are consistent with those of the form parameters!
--- When calling a method, you do not need to specify the data type for the real parameter eg: hello. show ("Welcome ");
--- The method can be a basic data type, such as eg: int double, or a String array of the referenced data type.
--- Multiple parameters are separated when there are multiple methods.
N !!! Use of the return value method with parameters in java
Eg: public String show (String name) {// defines a method with parameters and return values
Return "welcome" + name;
}
HelloWorld hello = new HelloWorld ();
String welcome = hello. show ("nsu"); // call the method with the return value of the parameter and save the return value in the welcome variable.
System. out. println (welcome );