Well-off will accompany you to learn JAVA -------- Method Introduction, well-off java --------
The method can simplify the program structure and save time for writing the same program code to achieve program modularization. In fact, the reader should be familiar with methods. The main () appearing in each class is a method. There are a lot of advantages in using methods to write program code. It can simplify program code, streamline repetitive program processes, and separate program code with specific functions, reduces Program maintenance costs.
The method can be defined using the following syntax:
Method parameters and return values
If the method returns a value,The data type of the returned value must be specified before the method is declared. If a parameter is to be passed to a method, the parameter and its type must be filled in brackets of the method. TestJava4_9 is an example used to describe the method. It can receive an integer parameter n. After outputting 2 * n asterisks, it returns an integer 2 * n.
Example: TestJava4_9
01 // The following Program is an example of an integer type returned by a method
02 public class TestJava4_9
03 {
04 public static void main (String args [])
05 {
06 int num;
07 num = star (7); // input 7 to star () and receive the returned value with num
08 System. out. println (num + "stars printed ");
09}
10
11 public static int star (int n) // star () method
12 {
13 for (int I = 1; I <= 2 * n; I ++)
14 System. out. print ("*"); // output 2 * n asterisks
15 System. out. print ("\ n"); // line feed
16 return 2 * n; // returns an integer of 2 * n.
17}
18}
Output result:
**************
14 stars printed
In TestJava4_9, because star () transmits an integer, the declaration of row 11th must add the int keyword before the star () method. In addition, an integer must be input to star (), therefore, the parameter name and data type should also be specified in the brackets of star:
If you want to pass a parameter,Enter the name and type of the parameter to be passed in the brackets of the method. TestJava4_10 is an example of calculating the diagonal length of a rectangle. The show_length () method can receive the width and height of a rectangle and return the diagonal length after calculation.
Example: TestJava4_10.java
01 // The following program describes the use of the method
02 public class TestJava4_10
03 {
04 public static void main (String args [])
05 {
06 double num;
07 num = show_length (); // enter the 22 and 19 parameters to show_length ().
08 System. out. println ("diagonal line length =" + num );
09}
10
11 public static double show_length (int m, int n)
12 {
13 return Math. sqrt (m * m + n * n); // return the diagonal line length.
14}
15}
Output result:
Diagonal Line Length = 29.068883707497267
The 7th line of TestJava4_10 calls show_length () and passes the integers 22 and 19 to the show_length () method. The second row uses the sqrt () method in the Math class to calculate the diagonal length. The function of sqrt (n) is to open the parameter n. Because the return value of sqrt () is of the double type, the return value of show_length () is also of the double type.
How to Learn JAVA
I recommend you a few books that I have used all the way.
1. Entry level: Zhang Xiaoxiang, java employment training materials (you can download a video)
2. j2ee preparation: "JavaScript web development-experiential learning tutorial" Zhang Xiaoxiang (supporting videos can be downloaded). You don't have to study it too seriously, understand the form, and perform basic javascript operations.
3. j2ee Introduction: <Tomcat and JavaWeb development> sun weiqin began to contact jsp and servers.
4. j2ee advanced: <proficient in Struts> <proficient in spring> <proficient in Hibernate> This part is the focus. You can also download a set of summer vacation notes, the introduction is not comprehensive, but as a quick start
Please study in sequence. Otherwise, it will be difficult. You 'd better find some videos for better understanding.
In addition, we recommend a better Website:
Bbs.langsin.com/index.php
Finally, we wish you an early success!
How to Learn java?
If there is no foundation, we recommend
<Head first java> I think this book is easy to understand and memorable. You can download it online or borrow it from the library.
<Java handout> Li Gang's lecture is detailed. You can create a tool book later.
If you want to learn more, we recommend
<Java core technology> I am reading it and think it is not bad.
<Java programming ideology> reference book. Search for it if necessary.