The benefits of how methods are used and methods in Java

Source: Internet
Author: User

Scene One, there are 100 requirements, output 100 different string content.

=============== demand 1 Output "I'm Chinese" =================//
=============== Demand 2 Output "I love China" =================//
=============== Demand 3 Output "study hard" =================//
=============== Demand 4 output "Day up" =================//
=============== ... "=================//
=============== demand 99 Output "XXXX" =================//
=============== demand 100 Output "XXXX" =================//

"Example code 1:"

 Packagelesson15; Public classTest2 {/**     * @paramargs*/     Public Static voidMain (string[] args) {//=============== Demand 1 Output "I'm Chinese" =================//System.out.println ("I am Chinese"); //=============== Demand 2 Output "I love China" =================//System.out.println ("I love China"); //=============== Demand 3 output "study hard" =================//SYSTEM.OUT.PRINTLN ("Study hard"); //=============== Demand 4 output "day up" =================//System.out.println ("Day Up"); //=============== demand ... "=================//        //=============== demand 99 Output "XXXX" =================//        //=============== Demand 100 Output "XXXX" =================//                //"Note 1," We found each time is the output character demand, the output content is different, the output code is the same, each time to write once System.out.println ()//There is a lot of duplicate code System.out.println ()//"NOTE 2," If there are 100 demand output strings, we have to write 100 lines System.out.println (). //"Optimization" use method to optimize    }}

Use method Optimization:

"Sample Code 2"

 Packagelesson15; Public classTest3 {/**     * @paramargs*/     Public Static voidMain (string[] args) {//=============== Demand 1 Output "I'm Chinese" =================//println ("I am Chinese"); //=============== Demand 2 Output "I love China" =================//println ("I love China"); //=============== Demand 3 output "study hard" =================//println ("Study hard"); //=============== Demand 4 output "day up" =================//println ("Day Up"); }    //define a method to perform the operation of the output     Public Static voidprintln (String str) {System.out.println (str); }}

Scenario two, if we want to precede each line of the output of scene one, add the words "inside Empty is:". If it is implemented as "sample code 1", it will be 100 times more cumbersome.

And if we use the "example code 2" above to make changes, it is more convenient. As follows:

 Packagelesson15; Public classTest3 {/**     * @paramargs*/     Public Static voidMain (string[] args) {//=============== Demand 1 Output "I'm Chinese" =================//println ("I am Chinese"); //=============== Demand 2 Output "I love China" =================//println ("I love China"); //=============== Demand 3 output "study hard" =================//println ("Study hard"); //=============== Demand 4 output "day up" =================//println ("Day Up"); }    //define a method to perform the operation of the output     Public Static voidprintln (String str) {System.out.println ("The content is:"+str); Use method, easy maintenance}}

Summary: Using the method, reduce a lot of duplicate code, easy to maintain.

The benefits of how methods are used and methods in Java

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.