An overview and basic use of Java's basic knowledge approach

Source: Internet
Author: User
Tags modifier

1.1 Method definition Format and format interpretation
An overview of the 2.1.1.1 method
Suppose there is a game program in which the program is constantly firing shells (plants vs. Zombies) during operation. Firing the shells requires writing 100 lines of code, and repeating the 100 lines of code every time a projectile is implemented, so the program becomes bloated and poorly readable. In order to solve the problem of code duplication, you can extract the code of the firing shells into a {}, and give the code a name, so that the name of each firing cannon to invoke the code of the firing shells. In the above process, the extracted code can be considered as a method defined in the program, the program when the need to launch a shell call this method can be
Simply put: A method is a block of code that accomplishes a specific function
In many languages, functions are defined, and functions are called methods in Java.
1.1.2 Method Format
Modifier returns a value type method name (parameter type argument name 1, argument type parameter Name 2 ...) {
function body;
return value;
}
1.1.3 Method Format Interpretation
Modifier now remembers public static
The return value type is used to qualify the data type of the return value
Method name a name, in order to facilitate our call method
Parameter types are used to receive the type of data passed in when the method is called
Parameter names are used to receive variables that pass in data when the method is called
Code for method Body completion function
Return end method to bring the return value to the caller
1.1.4 Case Code One

package com.itheima_01;/* * 方法:就是完成特定功能的代码块。 * * 定义格式: * 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) { * 方法体; * return 返回值; * } * 格式解释: * A:修饰符 目前记住 public static * B:返回值类型 用于限定返回值的数据类型 * C:方法名 一个名字,为了方便我们调用方法 * D:参数类型 用于接收调用方法时传入的数据的类型 * E:参数名  用于接收调用方法时传入的数据的变量 * F:方法体 完成功能的代码 * G:return 结束方法,把返回值带给调用者 * * 方法的两个明确: * A:返回值类型 明确功能结果的数据类型 * B:参数列表 明确有几个参数,以及参数的类型 * * 案例: * 求两个数和的案例。 */public class MethodDemo {}

1.2 Writing the Summation method 1.2.1 Definition Method considerations
Writing a method first has two points that need to be clear
The data type that returns the value type explicit function result
The parameter list explicitly has several parameters, as well as the type of the parameter
According to the format and two clear to complete the following functions
1.2.2 Case Code II

/ * * 方法的两个明确: * A:返回值类型 明确功能结果的数据类型 * B:参数列表 明确有几个参数,以及参数的类型 * * 案例: * 求两个数和的案例。 */public class MethodDemo {/* * 写一个方法,用于求和。 两个明确: 返回值类型 int 参数列表 int a,int b */public static int sum(int a, int b) {// int c = a + b;// return c;return a + b;}public static void main(String[] args) {}}

1.3 Invocation of the summation method
Method calls with a definite return value:
Separate call, no meaning
Output call, meaningful, but not good enough, because I don't have to output the result
Assignment invocation, recommended way
1.3.1 Case Code Three

package com.itheima_01;/* * 有明确返回值的方法的调用: * A:单独调用,没有什么意义。 * B:输出调用,有意义,但是不够好,因为我可能需要对求和的结果进行进一步的操作 * C:赋值调用 */public class MethodDemo2 {// 求和的方法public static int sum(int a, int b) {return a + b;}public static void main(String[] args) {// 单独调用// sum(10,20);// 输出调用// System.out.println(sum(10,20));// 赋值调用int s = sum(10, 20);// s+=100;System.out.println("s:"+s);}}

1.4 Invocation plot of the summation method
1.4.1 Method Invocation Flowchart

An overview and basic use of Java's basic knowledge approach

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.