aspose file tools*
The moose likes Beginning Java and the fly likes Why primitive arguement's value won't be changed even it's changed in method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why primitive arguement Watch "Why primitive arguement New topic
Author

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

KenSpirit Chen
Greenhorn

Joined: Aug 25, 2005
Posts: 3
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?


----------------------------------<br />Ken ^_^
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

Read this and then (especially) this.


[Jess in Action][AskingGoodQuestions]
KenSpirit Chen
Greenhorn

Joined: Aug 25, 2005
Posts: 3
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

Joined: Nov 03, 2005
Posts: 135
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

Joined: May 05, 2005
Posts: 88
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

Joined: Jan 20, 2005
Posts: 1241
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.


There will be glitches in my transition from being a saloon bar sage to a world statesman. - Tony Banks
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5287
    
  10



Check out this article by Peter Haggar
[ December 16, 2005: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why primitive arguement's value won't be changed even it's changed in method