-1

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?

Community
  • 1
  • 1
nikhil
  • 9,023
  • 22
  • 55
  • 81

5 Answers5

2

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • OKay..how would u go about to change value of x then? – nikhil Nov 06 '11 at 21:23
  • 3
    @nikhil: One option would be to return the new value from the method, and use assignment. But you've clearly written a meaningless example - it makes more sense to ask how to achieve something *useful*. – Jon Skeet Nov 06 '11 at 21:24
  • @nikhil I can think of three methods (wrapper object, return value, or instance variable) that will get the job done and are acceptable depending on the circumstances. But it all depends on the circumstances. I would recommend giving context on your production code and what your true goal is to determine which of the three (and perhaps some others I'm forgetting?) is the most appropriate to use. – corsiKa Nov 06 '11 at 21:32
0

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;
          }
 }
corsiKa
  • 81,495
  • 25
  • 153
  • 204
ac3hole
  • 123
  • 2
  • 10
0

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.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
0

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(...).

wannik
  • 12,212
  • 11
  • 46
  • 58
-2

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

NCode
  • 2,758
  • 1
  • 16
  • 25
  • 2
    No, objects are *not* passed by reference. References are passed by value... just as it says in the article you linked to: 'I'm really tired of hearing folks (incorrectly) state "primitives are passed by value, objects are passed by reference".' Why link to an article and at the same time spout exactly the mistake that it criticizes **in the second sentence**? – Jon Skeet Nov 06 '11 at 21:26
  • This is not true... you are misstating this which will confuse people. Objects are passed by value as well.... the value is an actual reference to the object. – Bryan Nov 06 '11 at 21:28
  • 4
    @Bryan: Objects aren't passed at all. The value of an expression is never an object. *References* are passed, and they're passed by value. – Jon Skeet Nov 06 '11 at 21:29