• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why primitive arguement's value won't be changed even it's changed in method

 
KenSpirit Chen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below is the sample code:

private static void changePrimitive(int i) {
i = 99;
}

public static void main(String[] args) {
int j = 0;
changePrimitive(j);
System.out.println("j = " + j);
}

Why the result would be: "j = 0"? There is no final specifier in the arguement?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Read this and then (especially) this.
 
KenSpirit Chen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

I got that later after reading the Appendix A in Thinking in Java. Don't know why it's placed at the end of the book. Anyway, think you for your reply.
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For others who may read the thread:

The answer is what's being changed inside the method is only a local variable (i.e., a copy of the variable passed in). Changing the local variable's value does not affect the value of the variable that was passed to the method.
 
Pratik Lohia
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
primitive datatypes such as int, boolean, double, long etc. are all pass by values during method calls. Objects except Strings are pass by reference.
Hence the value of j does not change
 
Dave Lenton
Ranch Hand
Posts: 1241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by PRATIK LOHIA:
Objects except Strings are pass by reference.


I think that everything is passed by value, never by reference. In the case of objects its the value of the address of the object which is passed.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Check out this article by Peter Haggar
[ December 16, 2005: Message edited by: Junilu Lacar ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by PRATIK LOHIA:
primitive datatypes such as int, boolean, double, long etc. are all pass by values during method calls. Objects except Strings are pass by reference.
Hence the value of j does not change


There is absolutely nothing whatsoever that differs between how Strings are passed as parameters and how any other kind of object is passed. Strings receive no special treatment of any kind.

Further (as has already been stated above) everything is passed by value, but in the case of objects, the value that is passed is a pointer (a reference) to the object, since that is what variables of reference type contain.

If you read the two stories I link to in my first post, you'll get a good explanation of this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic