Learn the function method, this can be in the future when we write the program, encountered to repeat the operation of the time, do not have to write each time, direct call on the line.
It reduces our workload and also reduces the error rate. But in contact, there are some doubts.
Public Static boolean Test (int a) { if(a>0) { returnTrue ; } Else { returnfalse; } }
The function method is to determine whether a is a positive number and return a Boolean value when we call Test, Boolean bl=test (a), and then we can use BL to make a judgment, or direct if (test (a)) such call.
And look at the second piece of code.
Public Static void test1 (int a) { if(a>0) { System.out.println (A +"is positive" ); } Else { System.out.println (A +"is not a positive number");} }
Test1 is similar to the previous paragraph, except that the return value is NULL, we do not think return, a>0, the output string "is not a positive number", otherwise the output is "not positive", this is also very well understood.
But I have a question, what will be the output of this situation?
1 Public Static voidMain (string[] args) {2 intA=5;3 if(Test (a));4 System.out.println (a);5 System.out.println (Test (a));6 }7 8 Public Static BooleanTestinta) {9 if(a>0){TenSystem.out.println (A + "is positive"); One return true; A } - Else - { theSystem.out.println (A + "not a positive number"); - return false; - } -}
Test is a function method that returns a Boolean value, so when called in the main function, SYSTEM.OUT.PRINTLN () in test will appear in the result? This is my question at the time, because the 3rd line call Testi is to determine its return value, that "is positive" will appear in the results?
Operation Result:
Analysis, the main function in the OUTPUT statement 2 times, the first 4th row, output A, that is the second row in the result, then the result of the 1th line "5 is positive" is the function method test output, similarly, 3rd line is also.
So we get the conclusion that whatever the return type of the function method is, all the contents of the function will run when called.
Although this question may seem very simple to many people, as long as a random question, but to self-test, this impression will be more profound
(I'm a beginner) some questions about the Java Beginner function method are validation