Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Difference in passing Array and Integer to a method in java?
I have started doing some programming in java and had the following question.
First, I am passing an integer to a method in java
public class testClass {
public static void main(String args[]){
int myInteger = 4; //Declaring a new integer and initializing to 4
change(myInteger); //Passing the integer to a method named change
System.out.println(myInteger); //Checking to see if the integer changed
}
public static void change(int x){
x+=5; //Adding 5 to the passed integer
}
}
When I run the above program, it prints out 4, which means the variable myInteger was not affected
Now I try to run the following code
public class testClass {
public static void main(String args[]){
int myArray[] = {4}; //Declaring a new Array and initializing to 4
change(myArray); //Passing the Array to a method named change
System.out.println(myArray[0]); //Checking to see if the Array changed
}
public static void change(int x[]){
x[0]+=5; //Adding 5 to the passed Array
}
}
When I run this code, it prints out 9. I am wondering why?
When I pass an integer to a method and returns nothing, the original integer variable is not affected. But when I do the same for an Array, the original array is affected. Can someone explain this?
I understand that for an integer, java uses a copy and for an array, it references the original array. I am wondering why java does it? Why wouldnt java use a copy for the array as well? Also, are there any other variable that java reference the original that I need to be aware of? Where can I find more info on these?
3 Answers
- Neeraj KumarLv 51 decade agoFavorite Answer
Why Java does that?
Java does this is for efficiency concerns. Passing a very large array or object by copy will consume double memory, and if the called function is not going to modify data then it is a sheer wastage of system resources. Also passing by copy requires stack memory which is faster but very limited in size. Passing very large objects by copy may cause stack overflow.
Are there any other variable that java reference the original?
Actually except the basic data types like int, long, double etc. Java passes all objects and arrays by reference only. Which means you can modify them if they have public data members or public methods which change private data members.
You can learn about it in more detail at the following URL -
- NeunerballLv 41 decade ago
In case of the integer, a copy of myInteger is used in the method, whereas passing an array, a copy of a reference to the object is used in the method. Therefore, since the reference (memories address) is pointing to the same reference/address, the array changes.
- CynthiaLv 45 years ago
You know for a fact that your getting the right value from findAverage(theArray);