• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Pass by value

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example of pass by value:
public class Passbyvalue {

public static void main(String[] args) {

int a=15,b;

System.out.println("Value of a before call:"+a);
Value value = new Value();
value.meth(a);
System.out.println("Value of a after call:"+a);

}
}


public class Value {

void meth(int i){
i *= i;

}
}
Here the value of a remains as 15.
If i perform same operation with return value the value of a is changed.


public class Passbyvalue {

public static void main(String[] args) {

int a=15,b;

System.out.println("Value of a before call:"+a);
Value value = new Value();
value.meth(a);

b= value.meth(a);
System.out.println("Value of a after call:"+b);
}
}


public class Value {

int meth(int i){
i *= i;
return i;
}
}
here value of a is changed to 255.It is pass by value and it changes the value.
Can you explain it HOW?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nakuli Patidar wrote:

Here the value of a remains as 15.
If i perform same operation with return value the value of a is changed.


here value of a is changed to 255.It is pass by value and it changes the value.
Can you explain it HOW?


Where do you see that a has changed? the value of b gets set to 255, but if you actually printed a (you are printing b), you'll see it doesn't change.
 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even after printing value of a:

a= value.meth(a);
System.out.println("Value of a after call:"+a);

the value of a is changed.

I request you all to run this code.

Please let me know if you have any idea.
 
Sheriff
Posts: 28417
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course the value of a is changed. Your code assigns it the value returned by the method, so it is completely unsurprising that it gets that value. This has nothing to do with parameter passing at all.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nakuli



Try this instead.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nakuli Patidar wrote:


you've changed what you're doing here. "a" changes because you do some computation, and then reset "a" to that new value. if you had done this:

would you be surprised that "a" changed? No. What you should have done to see that "a" didn't change was this:

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic