How do i pass Integer by reference? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How do i pass Integer by reference?

antsmanantsman Posts: 4Member
Could someone please tell me how to change the value of an Integer by reference? Whats wrong with my code below? why isnt after y 5

public class Test
{
Test(){}

public void change(Integer x)
{
x = new Integer(5);
}

public static void main(String[] args)
{
Test app = new Test();

Integer y = new Integer(100);

System.out.println("Before Y = " + y);

app.change(y);
System.out.println("After Y = " + y);
}


}

Output
Before Y = 100
After Y = 100

Comments

  • VilanyeVilanye Posts: 684Member
    Java passes by value.

    After your code calls change(), you have x and y pointing to the same object. This line: x = new Integer(5);, creates a new object that x references to, but y still references the original object. To change y change this line: app.change(y);, to y = app.change();. You don't have to pass y to change() since Integer is immutable. I wish they would add a set method to wrapper classes like this, it would make life a bit easier.


    : Could someone please tell me how to change the value of an Integer by reference? Whats wrong with my code below? why isnt after y 5
    :
    : public class Test
    : {
    : Test(){}
    :
    : public void change(Integer x)
    : {
    : x = new Integer(5);
    : }
    :
    : public static void main(String[] args)
    : {
    : Test app = new Test();
    :
    : Integer y = new Integer(100);
    :
    : System.out.println("Before Y = " + y);
    :
    : app.change(y);
    : System.out.println("After Y = " + y);
    : }
    :
    :
    : }
    :
    : Output
    : Before Y = 100
    : After Y = 100
    :

  • antsmanantsman Posts: 4Member
    : Java passes by value.
    :
    : After your code calls change(), you have x and y pointing to the same object. This line: x = new Integer(5);, creates a new object that x references to, but y still references the original object. To change y change this line: app.change(y);, to y = app.change();. You don't have to pass y to change() since Integer is immutable. I wish they would add a set method to wrapper classes like this, it would make life a bit easier.
    :
    :
    : : Could someone please tell me how to change the value of an Integer by reference? Whats wrong with my code below? why isnt after y 5
    : :
    : : public class Test
    : : {
    : : Test(){}
    : :
    : : public void change(Integer x)
    : : {
    : : x = new Integer(5);
    : : }
    : :
    : : public static void main(String[] args)
    : : {
    : : Test app = new Test();
    : :
    : : Integer y = new Integer(100);
    : :
    : : System.out.println("Before Y = " + y);
    : :
    : : app.change(y);
    : : System.out.println("After Y = " + y);
    : : }
    : :
    : :
    : : }
    : :
    : : Output
    : : Before Y = 100
    : : After Y = 100
    : :
    :
    :
    Thanks. After reconstructing my code it works. However, it there any way to change the value of y using the reference to y, which is x, in the change() method?
  • gregbkgregbk Posts: 6Member
    Here is the code to change y from 100 to 5, here change() returns an integer value 5.

    public class Test {

    Test(){}

    public int change() {
    return 5;
    }

    public static void main(String[] args) {
    int y = 100;
    Test app = new Test();

    System.out.println("Before Y = " + y);

    y = app.change();
    System.out.println("After Y = " + y);
    }
    }

    : Java passes by value.
    :
    : After your code calls change(), you have x and y pointing to the same object. This line: x = new Integer(5);, creates a new object that x references to, but y still references the original object. To change y change this line: app.change(y);, to y = app.change();. You don't have to pass y to change() since Integer is immutable. I wish they would add a set method to wrapper classes like this, it would make life a bit easier.
    :
    :
    : : Could someone please tell me how to change the value of an Integer by reference? Whats wrong with my code below? why isnt after y 5
    : :
    : : public class Test
    : : {
    : : Test(){}
    : :
    : : public void change(Integer x)
    : : {
    : : x = new Integer(5);
    : : }
    : :
    : : public static void main(String[] args)
    : : {
    : : Test app = new Test();
    : :
    : : Integer y = new Integer(100);
    : :
    : : System.out.println("Before Y = " + y);
    : :
    : : app.change(y);
    : : System.out.println("After Y = " + y);
    : : }
    : :
    : :
    : : }
    : :
    : : Output
    : : Before Y = 100
    : : After Y = 100
    : :
    :
    :

  • arb123arb123 Posts: 696Member
    [red]: Thanks. After reconstructing my code it works. However, it there any way to change the value of y using the reference to y, which is x, in the change() method?
    : [/red]

    There isn't a way to de exactly what you want (in the C/C++ sense of changing a value at a memory address). However, you can use a member on an object to achieve a similar effect.

    [code]
    public class Ref {

    public void change(Data data) {
    data.i = 123;
    }

    public static void main(String[] args) {
    Data d = new Data();
    d.i = 4;
    System.out.println(d.i);
    new Ref().change(d);
    System.out.println(d.i);
    }

    }

    class Data {
    public int i;
    }
    [/code]

    ---------------------------------
    [size=1]HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

Sign In or Register to comment.