An array is a group of variables of same types containing some values.In Java Arrays are objects so they are considered reference types.The elements of array can be primitive or reference types.If we want to access any element in the array, then we use the position number of that element in the array.This position number in the array is called the index or subscript of the element.
Declaration of array:
datatype[] arrayReference; //preferred way
or
datatype arrayReference[];
For ex: int[] arrayOfInts;
is an array which is pointing to the elements of type integer.
Defining array:
arrayReference=new datatype[arraySize];
For ex: arrayOfInts=new int[10];
We can combine the above two steps in one step as
int[] arrayOfInts=new int[10];
So in this way we can define an array.
If the size of array is 10 then its index would be 0-9 i.e. 0 to (size-1).
For ex int[] arr=new int[5];
then its elements would be arr[0],arr[1],arr[2],arr[3],arr[4] and the array reference will be pointing to the first element that is arr[0].
The index of array can be a expression but it can't be negative.
For ex arr[a+b]=23;
How to pass an array to method?
Suppose we have an array int[] arr=new int[10];
and method declaration getArrayMethod(int[] a);
then the method call getArrayMethod(arr);
passes the reference of array arr to method getArrayMethod().
The argument to a method can be passed as an entire array or only a single element.When a argument to the method is entire array, then it receives a copy of reference and when argument to method is an individual array element of primitive type then the method receives the value of that element.
Pass by value and pass by reference:
There are two ways to pass the arguments to the method:
1)Pass by value
2)Pass by reference
When an argument is passed by value,a copy of argument's value is passed to the called method.So if we made any changes to that argument then that changes will only be valid inside that called function since method contains the copy of that argument.So that updated value will not affect the original value of that argument outside that method.
If argument is passed by reference then called method can access the argument's value in caller directly and can modify its value.Pass by reference is used to improves the performance because in this case we don't have any need to copy the data,we only have to copy the reference of that data.
In Java,we cannot choose to pass by value or pass by reference, all arguments are passed by value.If argument is primitive type then copy of that primitive value is passed and if argument is reference type then copy of that reference is passed.
Declaration of array:
datatype[] arrayReference; //preferred way
or
datatype arrayReference[];
For ex: int[] arrayOfInts;
is an array which is pointing to the elements of type integer.
Defining array:
arrayReference=new datatype[arraySize];
For ex: arrayOfInts=new int[10];
We can combine the above two steps in one step as
int[] arrayOfInts=new int[10];
So in this way we can define an array.
If the size of array is 10 then its index would be 0-9 i.e. 0 to (size-1).
For ex int[] arr=new int[5];
then its elements would be arr[0],arr[1],arr[2],arr[3],arr[4] and the array reference will be pointing to the first element that is arr[0].
The index of array can be a expression but it can't be negative.
For ex arr[a+b]=23;
How to pass an array to method?
Suppose we have an array int[] arr=new int[10];
and method declaration getArrayMethod(int[] a);
then the method call getArrayMethod(arr);
passes the reference of array arr to method getArrayMethod().
The argument to a method can be passed as an entire array or only a single element.When a argument to the method is entire array, then it receives a copy of reference and when argument to method is an individual array element of primitive type then the method receives the value of that element.
Pass by value and pass by reference:
There are two ways to pass the arguments to the method:
1)Pass by value
2)Pass by reference
When an argument is passed by value,a copy of argument's value is passed to the called method.So if we made any changes to that argument then that changes will only be valid inside that called function since method contains the copy of that argument.So that updated value will not affect the original value of that argument outside that method.
If argument is passed by reference then called method can access the argument's value in caller directly and can modify its value.Pass by reference is used to improves the performance because in this case we don't have any need to copy the data,we only have to copy the reference of that data.
In Java,we cannot choose to pass by value or pass by reference, all arguments are passed by value.If argument is primitive type then copy of that primitive value is passed and if argument is reference type then copy of that reference is passed.
0 comments:
Post a Comment