Java通过引用和编译器优化传递 [英] Java pass by reference and compiler optimization

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

问题描述

在函数中,fermatFactorization() a b 被传递为引用参数,因为我正在使用 Long Class。但是,当我传递 a b testFermatFactorization() c>到> fermatFactorization() a b 不要改变,所以 testFermatFactorization()打印(0)(0)。我在 fermatFactorization() a b c>,我得到了我期望的输出。



我忽略了什么?编译器能否在 fermatFactorization()中改变 a b > 因为它们只被分配给?(可疑)

  public static void fermatFactorization(Long n,Long a,Long b)
// PRE:n是要分解的整数
// POST:a和b将是因子n
{
Long v = 1L;
Long x =((Double)Math.ceil(Math.sqrt(n)))。longValue();
//System.out.println(\"x:+ x);
Long u = 2 * x + 1;
Long r = x * x - n; (r!= 0)//我们正在寻找条件x ^ 2 - y ^ 2 - n为零
{
while(r> 0)
{
r = r - v; //更新我们的条件
v = v + 2; (r <0)
{$ b $ //跟踪(y + 1)^ 2 - y ^ 2 = 2y + 1,增加y
}
br = r + u;
u = u + 2; //跟踪(x + 1)^ 2 - x ^ 2 = 2x + 1,增加x
}
}

a =(u + v - 2)/ 2; //记住你和v相等; - > (2x + 1 + 2y + 1-2)/ 2 = x + y
b =(u-v)/ 2; // - > (2x + 1-(2y + 1))/ 2 = x-y
}

public static void testFermatFactorization(Long number)
{
Long a = 0L;
长b = 0L;
fermatFactorization(number,a,b);
System.out.printf(Fermat分解(%d)=(%d)(%d)\\\
,数字,a,b);


解决方案

Java是按值传递的。如果为参数分配新值,则不会影响调用方法中的值。

您有两个选择:
$ b


  • 使您的方法返回 a b - 可以在 int [] 中或使用一个单独的 FactorizationRezult 类有两个字段。这样你将在被调用的方法中声明 a b 作为局部变量,而不是将它们作为参数。这是最可取的方法。

  • 另一种方法是使用 MutableLong 并使用 setValue(..)方法 - 这种方式的变化将影响调用方法中的对象。这是不明智的

In the function fermatFactorization(), a and b are being passed as reference parameters, since I am using the Long Class. However, in function testFermatFactorization() when I pass a and b to fermatFactorization(), the values of a and b do not get changed, and so testFermatFactorization() prints (0)(0). I tested this by printing out a and b in fermatFactorization(), and I got the output that I expected.

What am I overlooking? Could the compiler alter a and b in fermatFactorization() since they are only being assigned to?(doubtful)

public static void fermatFactorization(Long n, Long a, Long b)   
//PRE:  n is the integer to be factored
//POST: a and b will be the factors of n
{
    Long v = 1L;
    Long x = ((Double)Math.ceil(Math.sqrt(n))).longValue();
    //System.out.println("x: " + x);
    Long u = 2*x + 1;
    Long r = x*x - n;

    while(r != 0)                 //we are looking for the condition x^2 - y^2 - n to be zero
    {
        while(r>0)
        {
            r = r - v;            //update our condition
            v = v + 2;            //v keeps track of (y+1)^2 - y^2 = 2y+1, increase the "y"
        }
        while(r<0)
        {
            r = r + u;
            u = u + 2;            //keeps track of (x+1)^2 - x^2 = 2x+1, increases the "x"
        }
    }

    a = (u + v - 2)/2;            //remember what u and v equal; --> (2x+1 + 2y+1 - 2)/2 = x+y
    b = (u - v)/2;                //                             --> (2x+1 -(2y+1))/2 = x-y
}

public static void testFermatFactorization(Long number)
{
    Long a = 0L;
    Long b = 0L;
    fermatFactorization(number, a, b);
    System.out.printf("Fermat Factorization(%d) = (%d)(%d)\n", number, a, b);
}

解决方案

Java is pass by value. If you assign a new value to the argument, it won't affect the value in the caller method.

You have two options:

  • make your method return a and b - either in a int[] or using a separate FactorizationRezult class that has two fields. That way you will declare a and b as local variables in your called method, rather than taking them as parameters. This is the most advisable approach.

  • An alternative approach is to use a MutableLong and use a setValue(..) method - that way the changes will affect the object in the caller method. This is less advisable

这篇关于Java通过引用和编译器优化传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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