(I'm a beginner) some questions about the Java Beginner function method are validation

Source: Internet
Author: User

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

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.