Question

Possible Duplicate:
Java, pass-by-value, reference variables

Consider the following simple java program

class main{
          public static void main(String args[]){
                int x = 5;
                change(x);
                System.out.println(x);
          }
          static void change(int x){
             x = 4;
          }
 }

Since java uses pass by value the value of x will not change in the main..To overcome this problem we have the concept of pass by reference in c..But i do not find any such concept in java..How can i really change the value of x..? If there is no way of changing x then is this not a disadvantage of java?

No correct solution

OTHER TIPS

You can't change the value of x in the calling code within change. (It would have been clearer if you'd used two different variable names.) I don't find it makes my life harder in Java - you can use pass by reference in C#, but it's rarely a good idea.

Pass by value is usually easier to understand, and leads to simpler code. Usually I either want a method to simply compute something - in which case that should be the return value - or I want to mutate an object; preferably the object I call the method on, but potentially an object which I indicate by passing a reference by value for one of the parameters. I very, very rarely wish I could change the value of a variable in the calling code other than by using simple assignment.

(Oh, and C doesn't have pass-by-reference either, by the way. It allows you to pass pointers by value, but that's not the same thing.)

if you need to change a value using a method then don't use primitive data types...alternatively since you are using an int you could do this:

 class main{
          public static void main(String args[]){
                int x = 5;
                x = change(x);
                System.out.println(x);
          }
          static int change(int x){
             x = 4;
             return x;
          }
 }

C also passes parameter by value. But if you pass a pointer (also by value of a pointer), you can change the value of a variable that the pointer points to.

You can check that C pass pointer by value by changing the value of a pointer in a function. When the function returns, the pointer still points to the same location (not the one that it points in the function).

Pass by a value is not a disadvantage. I feel safer if I'm sure that a function or a method cannot change the value of an argument.

If you want to change the value of x, use the following code:

x = change(x);

and change void change(...) to int change(...).

One way to do this is to give x class scope by moving the declaration outside the main function, that way it's scope is available to all members of the class, including the function change. You could also make x public, protected, or a member of a public class.

class main{
      int x = 5;
      public static void main(String args[]){
            change(x);
            System.out.println(x);
      }
      static void change(int x){
         x = 4;
      }
}

So the default private scope of all objects is not a disadvantage, quite the opposite. It minimizes side effect of unintended changes, unless the programmer explicitly allows it.

Basically primitives are passed by value, objects are passed by reference and there are no pointers. So make a wrapper class or return the new value.

More information can be found here: http://javadude.com/articles/passbyvalue.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top