This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 5 years ago.
Why is the output for the following code 11,21,31 11,21,31 and not 10,20,30 10,20,30?
public class Tp {
public static void doChange(int a[])
{
for(int pos=0;pos<a.length;pos++)
{
a[pos]+=1;
}
}
public static void main(String args[])
{
int arr[]= {10,20,30};
doChange(arr);
for(int x:arr)
{
System.out.print(x+",");
}
System.out.println(arr[0]+" "+arr[1]+" "+arr[2]);
}
}
When you do
public static void doChange(int a[])
{
for(int pos=0;pos<a.length;pos++)
{
a[pos]+=1;
}
}
This line a[pos]+=1;, adds +1 to each value on the array, so the expect output is 11,21,31 11,21,31, if you want 10,20,30 10,20,30, delete or comment this line doChange(arr);
Because in java arrays are objects, when you pass the arr array to the doChange() method as argument, it actually copies the reference id of that array (not the elements).
In a simple way, you are updating the same array that you passed in the doChange() method, that's why it reflects to the original array.
Obviously it prints "11, 21, 31, 11,21,31", with the method doChange you add 1 to every element of the array.
Writing:
a[pos]+=1;
is the same as writing:
a[pos]=a[pos]+1;
int arr[]= {10,20,30}; Initializes array [0]=10, [1]=20, [2]=30
doChange called with a[] which is a reference of arr[]
for-loop adds +1 to every value in arr[] ( += is equal to x = x + ? )
Now you have [0]=11, [1]=21, [2]=31
Related
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 2 months ago.
public class lalak
{
public static void m1(int[] array)
{
array= new int[]{1,2,3,4,5};
System.out.println(array[2]);
}
public static void main(String[] args)
{
int[] array = {1,1,1,1,1};
m1(array);
System.out.println(array[2]);
}
}
why answer is 1, not 3?
i expected the program to print 3 but i got 1 as output.
i thought method would change my original array but it turned out to be false. does anyone know the reason for this?
You're creating a new array which makes the reference to your array in main redundant.
You could either, replace the first line like Jon Skeet said
Or another option would be, to have m1 return an array and assign it to your array in main
public class lalak
{
public static int[] m1(int[] array)
{
array= new int[]{1,2,3,4,5};
System.out.println(array[2]);
}
public static void main(String[] args)
{
int[] array = {1,1,1,1,1};
array = m1(array);
System.out.println(array[2]);
}
}
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 2 years ago.
The value of array elements are getting changed while calling a static method with the parameter passing the same array as argument but primitive value does not get changed
Is it work differently in the case of while we pass data structure as method argument ?
Want to know why array element get changed after the method call with the array ..
I was expecting value 0 in the last syso statement instead of 999
public class TestStatic {
public static int square (int x)
{
x = x + 1;
return x * x;
}
public static int[] changeArr(int a[])
{
a[0] = 999;
return a;
}
public static void main(String[] args)
{
int a = 10;
System.out.println((square(a)));
System.out.println(a);
int arr[] = {0,0};
changeArr(arr);
System.out.println(arr[0]);
}
}
Actual Output:
121
10
999
I was expecting
121
10
0
In Java, method parameters are by reference, not by value, which means you pass the reference to the object, not a copy of the object. Note that primitives are passed by value.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I'm new to Java and is trying to learn the concept of constructor. I tried to print out the value of arrayOfInts in the main method using (to test whether the constructor was initialised the way I expected)
System.out.println(ds.arrayOfInts);
However, instead of printing out the values, the output is:
[I#15db9742
Why am I getting the wrong result and how I can print out the correct result? (i.e. the value stored in arrayOfInts).
public class DataStructure {
public static void main(String[] args) {
DataStructure ds = new DataStructure();
//System.out.println(ds.arrayOfInts); Doesnt work as expected
}
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() {
int arrayValue = 0;
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = ++arrayValue;
}
}
}
You are trying to print an array. An array is an object.
In order to display it correctly, you can loop through it, or use the Arrays.toString() method:
System.out.println(Arrays.toString(ds.arrayOfInts));
which returns a string representation of the specified array.
Arrays are objects in java. You need to iterate over elements and print them. Here is a sample implementation using the for-each loop.
public void print() {
for (int x : arrayOfInts) {
System.out.println(x);
}
}
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
I am trying to print the elements of this array, but it is printing junk characters. What is the problem in this program?
class Demo{
public static void main(String[] args){
int[] x= new int []{5,6,7,8,9,10,11};
{
System.out.println(x);
}
}
}
You need to use the Arrays.toString() method:
System.out.println(Arrays.toString(x));
This gives you a meaningful representation of the data inside your array. The default toString of Java array objects is not a meaningful representation of the data.
public static void main(String args[]) {
int[] x= new int[]{5,6,7,8,9,10,11};
{
System.out.println(Arrays.toString(x));
}
}
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 3 years ago.
This is my first post here, so please be friendly! :)
Let's say I have this code:
public static void reassign (int[] nums) {
int[] A = {10,11,22};
A = nums;
}
public static void main(String[] args) {
int[] nums = {0,2};
reassign(nums);
System.out.println(nums[1]);
}
Why is my answer 2, and not 11? Does it have something to do with the relative sizes of the arrays?
When you do this,
public static void reassign (int[] nums) {
int[] A = {10,11,22};
A = nums;
}
you make A as a refference of nums, and the nums you are refering to is the one from parameter, not the one from main method. its two different variable
This is how you suppose to do it:
static int[] nums = {0,2}; //initial value of nums
public static void reassign (int[] arr) {
nums=arr;
}
public static void main(String[] args) {
int[] A = {10,11,22};
System.out.println("before reassign:"+nums[1]);
reassign(A);
System.out.println("after reassign:"+nums[1]);
}
Output:
before reassign:2
after reassign:11
Why is my answer 2, and not 11?
For one, you wanted to write
public static void reassign (int[] nums) {
int[] A = {10,11,22};
nums = A;
}
But than won't help you either: you cannot assign to one method's local variable from another method. The value of the variable (a reference to the array) is passed to reassign, not the address of the variable itself.
In reassign you merely create a new array and then assign its reference to the local variable nums (in my corrected code).
Does it have something to do with the relative sizes of the arrays?
No, the reason behind it is fully general and applies to all of Java.