初学者到java并不确定这意味着什么

时间:2014-02-11 22:57:09

标签: java variables object

import java.util.*;

public class decimalToBinaryTest {
    public static void main(String[] args) {

        int number;
        Scanner in = new Scanner(System.in);

        System.out.println("Enter a positive interger");
        number = in.nextInt();

        if (number < 0) {
            System.out.println("Not a positive interger");
        }

        else {
            System.out.print("Convert to binary is: ");
            System.out.print(binaryform(number) + ".");
        }
    }

    private static Object binaryform(int number) {

        int remainder;

        if (number <= 1) {
            System.out.print(number);
            return null;
        }

        remainder = number % 2;
        binaryform(number >> 1);
        System.out.print(remainder);
        {
            return " ";
        }
    }
}

在程序的主要部分中创建了int variable。在下一部分中说private static Object binaryform ( int number )。变量I int number中的Objectrelating是主要方法吗?

1 个答案:

答案 0 :(得分:0)

是和否。变量名number与main方法无关。它是名为binaryform的方法的形式参数。参数number仅存在于方法本身中。但是,当调用binaryform时,调用中使用的变量(或常量)的实际值在方法执行时变为number的值。

public class Example {
    public static void main(String[] args) {
        int n = 3; // the name could be "number" and no behavior would change
        Object bf = binaryForm(n);
        // do something with bf
    }

    private static Object binaryform(int number) {
        // from the call above, number will have the value 3
        Object o = . . .;
        // generate or modify o from the value of number
        return o;
    }
}