Java basic grammar: day04 [Idea, method]

First, development tools, IntelliJ IDEA

1, create a package and class

Tips: The so-called package is a folder for the class file management

 

2, IDEA common shortcuts

3, IDEA modify keyboard shortcuts

In the IDEA tool, Ctrl + Space shortcut keys that can help us complement the code, but the shortcuts and Windows shortcut key to switch input method conflict, need to modify the IDEA shortcut keys.
File -> Settings-> keymap-> Main menu-> code-> Completion-> Basic

Second, the procedure is reviewed on the basis of prior learning to get started

Previous lessons, outputs a rectangular nested loops used, the console can print out a rectangle, so the method is defined as the void, no return value. Directly in the main method is called in the main.

Sample Code

package day04;

public class Demo01Method {
    public static void main(String[] args) {
        printMethod();
    }

    public static void printMethod() {
        for (int j = 0; j < 5; j++) {
            for (int i = 0; i < 20; i++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

operation result

"C:\Program Files\Java\jdk-13.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=58114:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath C:\java\JavaApps\out\production\JavaApps day04.Demo01Method
********************
********************
********************
********************
********************

Process finished with exit code 0

is directly outputted print method after the main method invocation result, the main process does not require the execution result of the print method, it is defined as void.

Precautions:

1. The method defined order does not matter.
2. The method must be defined in the next, another method not within the definition of a method.
3. After the method definition, it will not be executed; if you want to perform, be sure to call the method.

Third, the method is defined format Detailed

The method is actually a collection of functions of several statements.

1, the method is like a plant.

 

 

 2, format Detailed

 

 parameter list

  return result

3, three elements define the method

Demand: implemented method defined sum calculation of two integers.

4 three forms, call the method

day04 Package; 

public class Demo02MethodDefine { 
    public static void main (String [] args) { 
        // separate call 
        SUM (10, 20 is); 
        System.out.println ( "==========="); 

        // print call 
        System.out.println (SUM (10, 20)); // 30 
        System.out.println ( "==========="); 

        // assignment calls 
        int number = sum (15, 25); 
        Number + = 100; 
        System.out.println ( "variable values:" Number +); 140 // 
    } 

    public static int SUM (a int, int B) { 
        System.out.println ( " The method of performing it ");! 
        int Result = A + B; 
        return Result; 
    } 
}

operation result:

"C: \ Program Files \ Java \ jdk-13.0.2 \ bin \ java.exe" "-javaagent: C: \ Program Files \ JetBrains \ IntelliJ IDEA 2019.2 \ lib \ idea_rt.jar = 58501: C: \ Program Files \ JetBrains \ IntelliJ IDEA 2019.2 \ bin "-Dfile.encoding = UTF-8 -classpath C: \ java \ JavaApps \ out \ production \ JavaApps day04.Demo02MethodDefine 
method of performing it! 
=========== 
method of performing it! 
30 
=========== 
method of performing it! 
The value of the variable: 140 

Process Finished Exit with code 0

Note: Prior to learning method, fixed return type is written as void, this method can only be invoked separately, can not be printed or call assignment calls.

Fourth, the method call

1, method call flow diagram

2, with a contrast parameter and the parameter None

1, there is no difference between ginseng and ginseng

There are parameters: parentheses among the content, when a method requires some data conditions in order to complete the task, is to have parameters.

For example, to add two numbers, two numbers must know how much each can be added.

No argument: among the empty parentheses. A method does not require any data condition that he will be able to complete the task independently, it is no argument.

For example, a method is defined, 10 times the fixed print HelloWorld.

2, the sample code

day04 Package; 

public class Demo03MethodParam { 
    public static void main (String [] args) { 
        the method1 (10, 20 is); 
        System.out.println ( "=============="); 
        method2 (); 
    } 

    // multiplying two numbers, multiply, must know how much each of the two figures, or can not be calculated 
    // parameters have 
    public static void the method1 (A int, int B) { 
        int Result = A * B ; 
        System.out.println ( "The result is:" result +); 
    } 

    // e.g. printout text string 10 fixed 
    public static void method2 () { 
        for (int I = 0; I <10; I ++) { 
            the System .out.println ( "the Hello, World!" + I); 
        } 
    } 
}

Output

"C:\Program Files\Java\jdk-13.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=58803:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath C:\java\JavaApps\out\production\JavaApps day04.Demo03MethodParam
结果是:200
==============
Hello, World!0
Hello, World!1
Hello, World!2
Hello, World!3
Hello, World!4
Hello, World!5
Hello, World!6
Hello, World!7
Hello, World!8
Hello, World!9

Process finished with exit code 0

3, compare the return value and return value None

1, the subject of the request

Topics requirements: defining a method for obtaining [and] two numbers. (You help me count, after the completion of the count results tell me.)
Title deformation: the definition of a method for [print] the sum of the two numbers. (You calculated, then count yourself completely responsible for displaying the results, do not tell me.)

2, implementation code

day04 Package Penalty for; 

public class Demo04MethodReturn { 
    public static void main (String [] args) { 
        // I am the main method, I'll call you. 
        // I call you, you to help me calculate, after a goner, the results tell my num variable 
        int num = GetSum (10, 20); 
        System.out.println ( "return value is:" + num); 
        System.out.println ( "=============="); 

        printSum (100, 200 is); 
        System.out.println ( "============ == "); 

        System.out.println (GetSum (2,. 3)); // correct wording 
        getSum (3, 5); // correct wording, but did not use the return value 
        System.out.println (" == ============ "); 

        // void the method does not return a value, only a single, or not printed assignment 
        // System.out.println (printSum (2, 3 )); / / error writing! 
        // System.out.println (void);

        // int num2 = printSum (10, 20); // error writing! 
        Num3 void int = //; 
        // void Num4 = void; 
    } 

    // I'm a method, I am responsible for two numbers together. 
    // the return value of I int, who call me I'll tell who the results 
    public static int GetSum (A int, int B) { 
        int Result = A + B; 
        return Result; 
    } 

    // I is a method, I responsible for two numbers together. 
    // I do not have a return value, the result will not tell anyone, but I have printed out. 
    static void printSum public (A int, int B) { 
        int Result = A + B; 
        System.out.println ( "The result is:" Result +); 
    } 
}

operation result

"C:\Program Files\Java\jdk-13.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=59162:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath C:\java\JavaApps\out\production\JavaApps day04.Demo04MethodReturn
返回值是:30
==============
结果是:300
==============
5
==============

Process finished with exit code 0

Precautions:

  1. The method returns a value, calls may be used alone, or calling print call assignment.
  2. But for no return value method, you use only a single call can not be used to print call or assignment calls.

Fifth, the definition of training methods

1, define a method to determine two numbers are the same.

Three elements:

  1. Return Value Type: boolean
  2. Method name: isSame
  3. Parameter List: int a, int b

Implementation code:

public class Demo01MethodSame {

    public static void main(String[] args) {
        System.out.println(isSame(10, 20)); // false
        System.out.println(isSame(20, 20)); // true
    }

    /*
    三要素:
    返回值类型:boolean
    方法名称:isSame
    参数列表:int a, int b
     */
    public static boolean isSame(int a, int b) {
        /*boolean same;
        if (a == b) {
            same = true;
        } else {
            same = false;
        }*/

        // boolean same = a == b ? true : false;

        // boolean same = a == b;

        return a == b;
    }

  operation result

"C:\Program Files\Java\jdk-13.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=59718:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath C:\java\JavaApps\out\production\JavaApps day04.Demo01MethodSame
false
true

Process finished with exit code 0

2, define a method, and used to derive the values ​​of all numbers 1-100.

Three elements

  1. Return Value: returns a value, the calculation result is a digital int
  2. Method name: getSum
  3. Parameter List: data range has been determined, is fixed, so do not tell me any condition, no parameters

Implementation code

    static void main public (String [] args) { 
        System.out.println ( "The result is:" + GetSum ()); 
    } 

    / * 
    three elements 
    Return Value: returns a value, the calculation result is a digital int 
    Method Name: GetSum 
    list of parameters: data range has been determined, is fixed, there is no need to tell me any conditions, no parameters 
     * / 
    public static int GetSum () { 
        int SUM = 0; 
        for (int I =. 1; I <= 100; I ++ ) { 
            SUM = I +; 
        } 
        return SUM; 
    }

3, defines a method for printing a specified number of HelloWorld.

Three elements

Return Value Type: just be a bunch printing operation only, not calculated, nor the result of the call to tell
method name: printCount
list of parameters: in the end how many times you want to print? You have to tell me, or I do not know how many times, can not print. Number: int

Implementation code

    static void main public (String [] args) { 
        PrintCount (10); 
    } 

    / * 
    three elements 
    Return Value Type: lot just for a printing operation only, not calculated, there is no call to tell the results at a 
    method name: PrintCount 
    parameter list : in the end how many times you want to print? You have to tell me, or I do not know how many times, can not print. Times: int 
     * / 
    public static void PrintCount (int NUM) { 
        for (int I = 0; I <NUM; I ++) { 
            System.out.println; (+ (I +. 1) "the Hello, World!") 
        } 
    }

4, use of time, precautions

1. A method in a class which should be defined, but the method can not be defined in which method. You can not be nested.

2. The method defined before and after the order does not matter.

3. After you define the method does not perform, if you want to perform, be sure to call: call alone, printing calling, call assignment.

4. If the method returns a value, you must write "return return value;" can not do without.

The return value data behind the return, the return value must be the type of association.

6. For a void method does not return value, the return value can not write back return, only to write their own return.

7. A method for the void among the last return line can be omitted.

8. A method which can have multiple return statements, but must ensure that only one will be to perform, can not return two ligatures

Sample Code

day04 Package; 

public class Demo04MethodNotice { 
    public static int the method1 () { 
        return 10; 
    } 

    public static void method2 () { 
        // return 10; // error writing! The method does not return a value, you can not write back return return value. 
        return; // no return value, just the end of the execution method only. 
    } 

    Public static void the method3 () { 
        System.out.println ( "the AAA"); 
        System.out.println ( "the BBB"); 
    // return; // return the last row can be omitted. 
    } 

    Public static int getMax (A int, int B) { 
        / * int max; 
        IF (A> B) { 
            max = A; 
        } the else { 
            max = B; 
        } 
        return max; * /

        if (a > b) {
            return a;
        } else {
            return b;
        }
    }
}

Sixth, method overloading

For a similar function method, because the parameter list is not the same, but need to remember so many different ways to name, too much trouble.

Method overloading (Overload): multiple methods of the same name, but not the same parameter list. Benefits: You only need to remember only one method name, you can achieve a similar multiple functions.

1, method overloading associated with the following factors:

1. The number of different parameters
2. The parameters of different types of
3 different types of multi-order parameter

2, method overloading nothing to do with the following factors:

Independent of the name of the parameter 1.
2. regardless of the value and method return type

3, the sample code

day04 Package; 

public class Demo01MethodOverload { 
    public static void main (String [] args) { 
        /*System.out.println(sumTwo(10, 20 is)); 30 // 
        System.out.println (sumThree (10, 20 is, 30 )); 60 // 
        System.out.println (sumFour (10, 20 is, 30, 40)); // 100 * / 

        System.out.println (SUM (10, 20 is)); // two process parameters 
        System.out.println ((10, 20, 30 ) sum); // process the three parameters 
        System.out.println (sum (10, 20, 30, 40)); a method four parameters // 
// System.out.println (sum (10, 20, 30, 40, 50)); // could not find any way to match, so wrong! 

        SUM (10, 20 is); 
    } 

    public static int SUM (int A, Double B) { 
        return (int) (A + B); 
    }

    static int SUM public (Double A, int B) { 
        return (int) (A + B); 
    } 

    public static int SUM (A int, int B) { 
        System.out.println ( "There are two parameters of the method for performing! "); 
        return A + B; 
    } 

    // error writing! And the return value independent of the type 
    // public static Double SUM (A int, int B) { 
         // return A + B + 0.0; 
    //} 

    // Error writing! And the name of the parameter irrespective 
    // public static int SUM (X int, int Y) { 
         // return X + Y; 
    //} 

    public static int SUM (Double A, Double B) { 
        return (int) (A + B) ; 
    } 

    public static int SUM (a int, int B, int C) { 
        System.out.println ( "three parameters method for performing!");
        A + B + C return; 
    }

    static int SUM public (A int, int B, C int, int D) { 
        System.out.println ( "4 parameters method for performing!"); 
        return A + B + C + D; 
    } 
}

operation result

"C: \ Program Files \ Java \ jdk-13.0.2 \ bin \ java.exe" "-javaagent: C: \ Program Files \ JetBrains \ IntelliJ IDEA 2019.2 \ lib \ idea_rt.jar = 60007: C: \ Program Files \ JetBrains \ IntelliJ IDEA 2019.2 \ bin "-Dfile.encoding = UTF-8 -classpath C: \ java \ JavaApps \ out \ production \ JavaApps day04.Demo01MethodOverload 
there are two methods of performing parameter! 
30 
There are three parameters method execution! 
60 
There are four parameters method execution! 
100 
has two parameters perform a method! 

Process finished with exit code 0

4, heavy exercise (comparing two data are equal)

Topics requirements:

  1. Compares two data are equal.
  2. Parameter Type byte are two types, two types of short, int two types, two long type,
  3. And tested in the main method

Implementation code:

package day04;

public class Demo02MethodOverloadSame {
    public static void main(String[] args) {
        byte a = 10;
        byte b = 20;
        System.out.println(isSame(a, b));

        System.out.println(isSame((short) 20, (short) 20));

        System.out.println(isSame(11, 12));

        System.out.println(isSame(10L, 10L));
    }

    public static boolean isSame(byte a, byte b) {
        System.out.println("两个byte参数的方法执行!");
        boolean same;
        if (a == b) {
            same = true;
        } else {
            same = false;
        }
        return same;
    }

    static Boolean isSame public (A short, short B) { 
        System.out.println ( "Method two short execution parameters!"); 
        Boolean Same == A = B to true: to false;? 
        return Same; 
    } 

    public static Boolean isSame (a int, int B) { 
        System.out.println ( "method of performing two parameters int!"); 
        return a == B; 
    } 

    public static Boolean isSame (a Long, Long B) { 
        System.out.println ( "method two long execution parameters!"); 
        IF (a == B) { 
            return to true; 
        } the else { 
            return to false; 
        } 
    } 
}

operation result:

"C: \ Program Files \ Java \ jdk-13.0.2 \ bin \ java.exe" "-javaagent: C: \ Program Files \ JetBrains \ IntelliJ IDEA 2019.2 \ lib \ idea_rt.jar = 60124: C: \ Program Files \ JetBrains \ IntelliJ IDEA 2019.2 \ bin "-Dfile.encoding = UTF-8 -classpath C: \ java \ JavaApps \ out \ production \ JavaApps day04.Demo02MethodOverloadSame 
method of performing two byte parameters! 
false 
method two short arguments execution! 
true 
method of execution two int parameters! 
false 
method of implementation of two long parameters! 
to true 

Process Finished with Exit code 0

5, heavy exercise (to determine which method is overloaded Relations)

{class Demo03OverloadJudge public 

    / * 
    public static void Open () {} // correct overloaded 
    public static void open (int a) {} // properly reload 
    static void open (int a, int b) {} // Error Code : line 8 and conflict 
    public static void open (double a, int b) {} // correct overloaded 
    public static void open (int a, double b) {} // error Code: 6 and line conflict 
    public void open (int i, double d) { } // error Code: 5, line and conflict 
    public static void OPEN () {} // error code is not correct, but not the effective overload 
    public static void open (int i, int j) {} // error Code: row 3 and conflict 
    * / 

}

6, heavy exercise (the println method works analog output statement, what kind of data is transmitted on what type of data output, only allows the definition of a method name println.)

Long Short float byte int // Double char boolean 
// String 
// called when the output statement, println method is actually carried out a variety of data types overloads. 
{class Demo04OverloadPrint public 

    public static void main (String [] args) { 
        the myPrint (100); // int 
        the myPrint ( "the Hello"); // String 
    } 

    public static void the myPrint (NUM byte) { 
        System.out.println (NUM ); 
    } 

    public static void the myPrint (Short NUM) { 
        System.out.println (NUM); 
    } 

    public static void the myPrint (int NUM) { 
        System.out.println (NUM); 
    } 

    public static void the myPrint (Long NUM) { 
        System.out.println (NUM); 
    }

    public static void myPrint(float num) {
        System.out.println(num);
    }

    public static void myPrint(double num) {
        System.out.println(num);
    }

    public static void myPrint(char zifu) {
        System.out.println(zifu);
    }

    public static void myPrint(boolean is) {
        System.out.println(is);
    }

    public static void myPrint(String str) {
        System.out.println(str);
    }

}

operation result

    public static void main(String[] args) {
        myPrint(100); // int
        myPrint("Hello"); // String
    }

    public static void myPrint(byte num) {
        System.out.println(num);
    }

    public static void myPrint(short num) {
        System.out.println(num);
    }

    public static void myPrint(int num) {
        System.out.println(num);
    }

    public static void myPrint(long num) {
        System.out.println(num);
    }

    public static void myPrint(float num) {
        System.out.println(num);
    }

    public static void myPrint(double num) {
        System.out.println(num);
    }

    public static void myPrint(char zifu) {
        System.out.println(zifu);
    }

    public static void myPrint(boolean is) {
        System.out.println(is);
    }

    public static void myPrint(String str) {
        System.out.println(str);
    }

Guess you like

Origin www.cnblogs.com/luoahong/p/12598512.html