How can I simulate passing by reference in Java?

I am a complete Java noob. I know that Java considers all parameters to be passed by value, and there are several others to where people explain this.

For example, in C ++, I can do:

void makeAThree(int &n)
{
   n = 3;
}
int main()
{
   int myInt = 4;
   makeAThree(myInt);
   cout << myInt;
}

What will be output 3. I know that in Java all parameters are passed by value, and therefore you cannot manipulate the passed parameter. Is there a standard way to simulate passing by reference in Java? Is it impossible to call a function that controls the passed variable? It’s hard for me to wrap up the idea that there is no way to do this.

+9
source share
8 answers

, , - , .

static void makeAThree(Reference<Integer> ref)
{
   ref.set(3);
}

public static void main(String[] args)
{
  Reference<Integer> myInt = new Reference<>(4);
  makeAThree(myInt);
  System.out.println(myInt.get());
}

Java , ( ), ref 3 makeAThree , myInt main().

: Reference - , Java. . :

public class Reference<T> {
    private T referent;

    public Reference(T initialValue) {
       referent = initialValue;
    }

    public void set(T newVal) {
       referent = newVal;
    }

    public T get() {
       return referent;
    }
}

, . . this ( ). - "" - .

+15

1

+7

Java , , , , . ,

void someMethod()
{
   int value = 4;
   changeInt(value);
   System.out.printlin(value); 
}

public void changeInt(int x)
{
   x = x + 1;
}

4,

class SomeClass
    {
       int x;
    }

void someMethod()
    {
       SomeClass value = new SomeClass();
       value.x = 4;
       changeCls(value);
       System.out.printlin(value.x); 
    }

    public void changeCls(SomeClass cls)
    {
        cls = new SomeClass();
        cls.x = 5;
    }

- 4, , , 'someMethod'.

class SomeClass
{
   int x;
}

void someMethod()
    {
       SomeClass value = new SomeClass();
       value.x = 4;
       changeCls(value);
       System.out.printlin(value.x); 
    }

    public void changeCls(SomeClass cls)
    {
        cls.x = cls.x + 1;
    }

, . , - , , . , 5. , , , . , .

+2

.

, , , . , Java, Array - . , "" println(), , - toString(), . , ( ). , Java , - , . Java. , , , . Java , . , . , 1999 . -, , . 1999 64 256 800   2 8 200-700 , Java Android, (iOS , Objective C -, , , , ).

int[] int , 5 someMethod().


public void changeInt(int x)
{
   x = x + 1;
} 

public void changeInt(int[] x)
{
   x[0] += 1; 

}

. , , . OFCOURSE , , .


  public void changeCls(SomeClass cls)
   {
       cls = new SomeClass();
       cls.x = 5;
   }

4, HIDDEN FROM SCOPE . , , , .


, , .

+2

, :

1) , "java bean", .

2) AtomicInteger/AtomicLong, concurrency, .... . .

: , / .

+1

Java - pass-by-value, pass-by-copy. , ++. - Java C/++. , :

public static void main (String [] args) {
    int myInt = 4;
    myInt = makeAThree(myInt);

}
static int makeAThree(int n)
{
   return n = 3;
}

P.S. static, . .;)

This will make you understand better.

+1

Java pass by value .

, , , .

, , . , . , , .

, java value.period.

0

- - .

, , . , , , , Java C++ ( ), . , , , C++, , , , . , - . .

0
source

All Articles