JAVA function part

I. Definition Function

函数就是定义在类中的具有特定功能的一段独立小程序(主要解决的是那些重复的且具有独立功能的代码块)函数也称为方法。

 The meaning of existence functions : to reduce the redundant code, a multiplexing function to reduce the amount of codes of the main function, the main function appropriately split, so that the memory optimization.

II. Format function

    Access type function return value type function name (parameter list) {

       Function body

       return Return value;

     }

 

Access : refers to the use of functions (internal and external).

  • public
  • protected
  • Default does not write
  • private

Function types : type classification refers to the function, that is the specific usage scenarios and applications functions.

Function name : This is the section of code a programmer to customize the name (identifier).

Parameter List : list of parameters with a plurality of parameter type, parameter names ..., mainly for transmitting data to a number of receiving external function (parameter). Optional parameters may, for example, selected from: MMath.random () method is no parameter.

Function body : an independent function block.

return : merely indicates the end of the current function, if the return value (calculation result code block), before the end of the function return value to the caller. If the return is not found, because the function does not return a value, so the return it can be omitted.

Advertisement glance : In the first method, the need for a separate data type declaration for each parameter. For example: max (int numl, 1nt num2 ) is positive shoulder, and max (int numl, mim2) is wrong.

III. Classification function

  • There are return value parameter
  • There is no return value parameter
  • No return value parameter
  • Parameters None None Return Value

Note : The function returns a value of participating in operation, the output assignment. No return value of the function call only, and can not return the results of additional operations.

 

IV. Function parameter passing

The actual parameters (arguments) : is the data transfer function (constants, variables) is called the argument when calling the function.

In the form of parameters (parameters) : that is, when defining the function, among the list of data parameters, called parameter.

Note : the argument is passed to the constant parameter in the constant pool address and address of the object in the heap memory.

 

 Explain : program run from the main function, a, b which are stored in the constant pool 4 and 6 address encountered double c = pow (a, b ), the first execution pow (a, b), obtained results sum, then the sum of the stored address is transmitted to c, then the program to continue.

 

V. function stack

 Running the function based on (a container structure after a stack is advanced out) stack memory.

Whenever you call a method, the system creates an activity record (also called the active frame), parameters and variables for the preservation methods. Activity records placed in a memory area, called the call stack (call stack). Call stack also called the execution stack, the stack is running, or a stack machine, often referred to as the "stack"

 

 

1 is a function of stack operating procedures:

 

 

FIG recording stack 2 for method invocation

 

Sixth, program title

1.

Analysis: palindromic prime palindrome inside recall function and prime function corresponding functions are implemented.

class Demo04_06{
    public static void main(String[] args){
        int count=0;    //表示回文素数的个数
        int num=2;      //表示可能的回文素数的数字 从2开始~?
        while(true){
            //如果num是 回文素数
            if(isHuiWenAndSuShu(num)){
                count++;
                System.out.print(num+" ");
                if(count%10==0){               //每十个数换一行
                    System.out.println();
                }
            }
            if(count==100){
                break;
            }
            num++;
        }
    }
    public static boolean isHuiWenAndSuShu(int num){
        return isHuiWen(num)&&isSuShu(num);
    }
    public static boolean isHuiWen(int num){
        return reverse(num)==num;           
    }
    public static int reverse(int num){        //实现反序
        int sum=0;
        while(true){
            sum=sum*10+num%10;
            num/=10;
            if(num==0){
                return sum;
            }
        }
    }
    public static boolean isSuShu(int num){//判断素数
        for(int i=2;i<=num/2;i++){
            if(num%i==0){
                return false;
            }
        }
        return true;
    }
}

 

 

2.

Analysis: // itself is a prime number
         after // reverse is also prime
         // can not be a palindrome

class Demo04_07{
    public static void main(String[] args){
        int count=0;
        int num=2;
        while(true){
            if(isFanZhuanSuShu(num)){
                count++;
                System.out.print(num+" ");
                if(count%10==0){
                    System.out.println();
                }
            }
            if(count==100){
                return; //结束当前函数
            }
            num++;
        }
    }
    //1.本身是素数
    //2.反转之后也是素数
    //3.不能是回文
    //素数()  反转() 回文功能()
    public static boolean isFanZhuanSuShu(int num){
        return isSuShu(num)&&isSuShu(reverse(num))&&!isHuiWen(num);
    }
    //回文功能
    public static boolean isHuiWen(int num){
        return reverse(num)==num;
    }
    //素数功能
    public static boolean isSuShu(int num){
        for(int i=2;i<=num/2;i++){
            if(num%i==0){
                return false;
            }
        }
        return true;
    }
    //反转功能
    public static int reverse(int num){
        int sum=0;
        while(true){
            sum=sum*10+num%10;
            num/=10;
            if(num==0){
                return sum;
            }
        }
    }
}

 

 

 

3.

import java.util.Scanner;
class Demo04_02{
    public static void main(String[] args){
        //1.提示用户输入一个数字
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num=scanner.nextInt();
        //2.判断该数字是否是一个回文
        if(isPalindrome(num)){  // pause1
            System.out.println("是回文");
        }else{
            System.out.println("不是回文");
        }
        //return ;
    }
    public static boolean isPalindrome(int num){
        //1.先计算该数字的反序
        //2.对比反序的数字和数字本身
               
        return reverse(num)==num; 
    }
    public static int reverse(int num){
        int sum=0;
        while(true){
            sum=sum*10+num%10;
            num/=10;
            if(num==0){
                return sum;
            }
        }
    }
}

 

Published 11 original articles · won praise 1 · views 375

Guess you like

Origin blog.csdn.net/qq_45824565/article/details/104260030