3

I know here is no pointer in Java. But how do I change a value in the calling scope? For instance, I want to write a function that takes an integer num, set the integer to 0 if it's greater than 21, otherwise do nothing. In the main, my code is as follow:

int a=34;
KillOver21(a);
System.out.print(a);

I expect an 0.

4
  • 3
    Why not just return your value from the function? Commented Aug 13, 2013 at 5:15
  • You can only do this with mutable types. Commented Aug 13, 2013 at 5:17
  • 1
    The fact that Java doesn't have pointers is irrelevant. In the Pascal sense, it does have pointers. What matters is that it doesn't have pass-by-reference. Commented Aug 13, 2013 at 5:20
  • 1
    Chances are, if you need to do this, it's not idomiatic/java-like. every language lends itself to its own way of thinking and approaching problems and you can usually approach whatever problem you have in real life from a way that doesn't involve pass by reference. Commented Aug 13, 2013 at 5:24

4 Answers 4

4

Java is pass by value, so a copy of the parameter a is sent to the method, so modification to a in the method will not affect the original argument a in main

The max you can do is return int from KillOver21(a) method

int z = KillOver21(a); // This will return 0
System.out.print(z);

But you can achieve something like that with custom objects, say you have a class

class AHolder {

    public int a;

}

then you can expect AHolder instance to change

public static void main(String [] args) {
     AHolder a = new AHolder();
     a.a = 34;
     killOver21(a);
     System.out.println(a.a);
}

public static void killOver21(AHolder b) {
    if(b.a > 21) {
       b.a = 0;
    }
}

Since in the latter (even if its Pass by Value) , the reference is copied and both reference point to same object. So changes made inside the killOver21 method actually changes the object.

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Or, you could use an object, which would be pass by reference.
@Timothy Jones: No, be careful, even if you use a type that inherits from Object it is still passed by value.
Well, technically speaking, the Object is passed by the value of its reference handle. So, if you use a mutable type, and pass it in, you could change it using a setter, resulting in the desired behaviour.
3

It is simply not possible, Java supports pass by value. int a's value will be copied to the function.

You could use Object instead of primitive where the reference value will be copied to your function by which you can get the actual object and modify it.

Comments

1

Fundamentally impossible in Java, period. int are immutable, and passed by value. You would need to create a mutable int type:

class MutableInt {
    private int value;

    public MutableInt(int value) { this.value = value; }

    public getValue() { return this.value; }
    public setValue(int value) { this.value = value; }
}

Then:

void KillOver21(MutableInt m) {
    if(m.getValue() > 21) { m.setValue(0); }
}

However, be aware the mutable types that represent concepts that are defined by their value rather than their identity are generally an extremely bad idea. But, this is the only way to achieve what you're trying to achieve. Again, I caution you with the strongest words: what you're doing is a bad idea. You should find another way.

Doc, it hurts when I do this.

Then don't do that!

Comments

1

The simpliest way (quick&dirty) is to put value within an array

int holder[] = new int[]{ a};

KillOver21(holder)

System.out.printf( "value=[%d]", holder[0]  );

void KillOver21(int holder[] ) {
    holder[0] = 0;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.