java如何通过引用传递? [英] How to pass by reference with java?

查看:118
本文介绍了java如何通过引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

这是我用Java编写的第一个程序.我首先在main中编写了该程序,然后使其正常运行.然后,我将现有程序分为模块.现在,每次运行它时,值都为0.我假设发生了这种情况,因为变量需要通过引用传递.唯一的事情是我不知道该怎么做.如果不是通过引用传递问题,请告知.感谢您的时间和精力.


Hello,

This is the first program I have ever written in Java. I first wrote the program in main, and I had it functioning correctly. Then I divided the existing program into modules. Now every time I run it the values are all 0. I am assuming this is happening because the variables need to be passed by reference. The only thing is I can''t figure out how to do it. If passing by reference is not the issue please advise. Thanks for your time, and effort.


public class SalesTracker
{

    public static void main(String[] args)
    {
        //variables
        double totalSales = 0;
        double averageSales = 0;
        double highestSales = 0;
        double lowestSales = 0;
        double highvalue = 0;
        double lowvalue = 999999999;
        String lowestMonth = "";
        String highestMonth = "";
        int i = 0;

        //arays
        String[] monthArray = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                               "Aug", "Sep", "Oct", "Nov", "Dec"};
        double[] monthlySales = new double[12];

        //greeting
        System.out.print("Welcome to the Annual Sales Program!!!\n\n\n");

        //Input
        getSales(monthlySales, monthArray);

        //Calculations I corrected the issue by adding

        totalSales = computeTotalSales(monthlySales, totalSales);

        averageSales = computeAverageSales(monthlySales, totalSales, averageSales);

        highestSales = computeHighestMonth(monthlySales, highestSales);

        lowestSales = computeLowestMonth(monthlySales, lowestSales);

        displayInfo(totalSales, averageSales, highestSales, lowestSales);
    }

    public static void getSales(double monthlySales[], String monthArray[] )
    {
        ////Get Input
        int i;
        Scanner input =  new Scanner(System.in);
        for (i = 0; i < monthlySales.length; i++)
        {
            System.out.print("Please enter total sales for " + monthArray[i] + ":");
            monthlySales[i] = input.nextInt();
        }
    }

    public static double computeTotalSales(double monthlySales[], double
                                            totalSales)
    {
        ////Total Value
        int i;
        for (i = 0; i < monthlySales.length; i++)
        {
            totalSales = totalSales + monthlySales[i];
        }
        return totalSales;
    }

    public static double computeAverageSales(double monthlySales[], double totalSales
                                            , double averageSales)
    {
     ////Average Value
        averageSales = totalSales/monthlySales.length;
        return averageSales;
    }

    public static double computeHighestMonth(double monthlySales[],double highestSales)
    {
        ////Highest Value
        int i;
        double highvalue = 0;
        for (i = 0; i < monthlySales.length; i++)
        {
                    if (monthlySales[i] > highvalue)
                    {
                        highvalue = monthlySales[i];
                    }
                    else
                    {
                        highvalue = highvalue;
                    }
                    highestSales = highvalue;
        }
        return highestSales;
    }

    public static double computeLowestMonth(double monthlySales[],double lowestSales)
    {
        ////Lowest Value
        int i;
        double lowvalue = 999999999;
        for (i = 0; i < monthlySales.length; i++)
        {
                    if (monthlySales[i] < lowvalue)
                    {
                        lowvalue = monthlySales[i];
                    }
                    else
                    {
                        lowvalue = lowvalue;
                    }
                    lowestSales = lowvalue;
        }
        return lowestSales;
    }

    public static void displayInfo(double totalSales, double averageSales,
                                   double highestSales, double lowestSales)
    {
        //Output
        System.out.print("Total: " + totalSales + "\n");
        System.out.print("Average: " + averageSales + "\n");
        System.out.print("Highest: " + highestSales + "\n");
        System.out.print("Lowest: " + lowestSales + "\n");
    }
}

推荐答案

这是Java的工作方式:所有参数均按值,时间段传递.

问题是:Java类型是引用类型值类型.引用类型的参数(通过值传递)可以通过引用进行修改,就像在其他情况下(例如,有两个引用类型的变量引用同一对象)一样.让我们看看:您通过值传递了引用类型的变量.此值的副本将复制到堆栈中.引用变量是两个对象:引用本身和它引用的对象.当按值传递引用类型变量时,您有两个引用对象,一个是原始对象,另一个是其在堆栈上的副本,但是它们都引用相同的对象.如果需要修改引用的对象,则可以通过调用前传递的变量对其进行访问,此变量引用的对象将在调用后进行修改.就这么简单.

请参阅:
https://en.wikipedia.org/wiki/Reference_type [ http://docstore.mik.ua/orelly/java-ent/jnut/ch02_10.htm [ ^ ].

当然,类和数组是引用类型.字符串是一种紧密模仿值语义的引用类型.在某种程度上,您的问题根本就不存在.

—SA
Here is how Java works: all parameters are passed by value, period.

The thing is: Java types are either reference types or value types. Parameters of reference types, passed by value, can be modified by their reference, as in any other situations when you have, say, two variable of reference types referencing the same object. Let''s see: you pass a variable of reference type by value. The copy of this value is copied on stack. Reference variable is two objects: the reference itself, and the object it references. When your passed reference-type variable by value, you have two reference objects, one is original, and another one is its copy on stack, but they both reference the same object. Should you need to modify the referenced object, it will be accessible through the variable you passed before the call, the the object referenced by this variable will come out modified after the call. As simple as that.

Please see:
https://en.wikipedia.org/wiki/Reference_type[^],
http://docstore.mik.ua/orelly/java-ent/jnut/ch02_10.htm[^].

Of course, classes and arrays are reference types. String is a reference type which tightly mimics value semantics. Your problem, in a way, simply does not exist.

—SA


这篇关于java如何通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆