2

why is the output of array giving the modified value of b;

public static void main(String[] args)
{
    int i=0;
    int a[] = {1,2,3,4};

    int b[] = {5,6,7,8};
    a=b;
    b[2] = 9;
    while(i<4)
    {
        System.out.println(a[i]);
        i++;
    }
}
Ankit
  • 39
  • 3
  • 3
    Not related to your question but you shouldn't use harcoded values just as you did there in the `while` loop, prefer using `b.length`. – gcourtet Aug 14 '18 at 08:34
  • 1
    As for code style: always be consistent. Don't use `a=b` (without spaces) and `b[2] = 9` (with spaces). Generally we try and use more whitespace to enhance readability. If you don't want to type things like `while (i < 4)` then you can always use a pretty printer or formatter within a development environment. – Maarten Bodewes Aug 14 '18 at 08:37
  • 1
    @gcourtet That was the first thing I noticed too. Hardcoding values is very risky and can breed hidden errors in the future especially if the code is modified later and the length is changed. – Taslim Oseni Aug 14 '18 at 08:40
  • Also because array in Java is an object, unlike those primitive data type. When use assignment operator =, you assign the object reference of b to a – Mr. Brickowski Aug 14 '18 at 08:40
  • thanks @MaartenBodewes will practice that. – Ankit Aug 14 '18 at 08:44
  • @Taslim I don't see how this can be a dupe of that question. I am not doubting that there is a dupe somewhere, but that question is about *method arguments*. As we don't even call a method in the question (other than `println`, which is just used to check the output) I don't see how that's related. – Maarten Bodewes Aug 14 '18 at 08:54

5 Answers5

3

Because a and b have the same reference. And is basically the same data.

Is Java "pass-by-reference" or "pass-by-value"?

Simion
  • 353
  • 1
  • 9
  • 1
    Pass-by-value has very little to do with this question in my opinion; no method is called. – Maarten Bodewes Aug 14 '18 at 09:04
  • 1
    The article i specified is a general article about how variables and data works in Java. If you understand the principles, it is easy to understand also the question above. – Simion Aug 14 '18 at 09:10
  • @Simion the correct verbiage I think would be "because that array (and arrays are Object(s)) is referenced by two references"; otherwise I totally agree, this *is* the correct answer, although a duplicate... – Eugene Aug 16 '18 at 21:02
2

When you write a=b; then a and b pointing to same array i.e. {5,6,7,8};.

Then it doesn't matter whether you update it via a or b it will make changes to the same array.

Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23
0

In Java, byte arrays are a special type of objects. In this case you're using integer arrays that have type I[. In Java, object instances are always referenced rather than kept by value. You can nicely see what happens if you print them out:

int[] a = { 1, 2, 3, 4 };
int[] b = { 5, 6, 7, 8 };

System.out.printf("Before: a=%s b=%s%n", a, b);

a = b;

System.out.printf("After: a=%s b=%s%n", a, b);

b[2] = 9;

System.out.println(Arrays.toString(a));

will print out:

Before: a=[I@15db9742 b=[I@6d06d69c
After: a=[I@6d06d69c b=[I@6d06d69c
[5, 6, 9, 8]

As you can see it prints out the references as the type I[, then an @ and then the reference. First the reference points to two different object instances, but after a = b, both a and b reference the object instance that was first assigned to b. This means that any alteration of a and b will on the same object. Of course it then doesn't matter if you print out the values of a or b either.

If you want to make sure that a isn't altered when b is changed then you need to clone the object. Cloning is supported for arrays as they implement the Cloneable interface:

int[] a = { 1, 2, 3, 4 };
int[] b = { 5, 6, 7, 8 };

System.out.printf("Before: a=%s b=%s%n", a, b);

a = b.clone();

System.out.printf("After: a=%s b=%s%n", a, b);

b[2] = 9;

System.out.println(Arrays.toString(a));

will print out:

Before: a=[I@15db9742 b=[I@6d06d69c
After: a=[I@7852e922 b=[I@6d06d69c
[5, 6, 7, 8]

As you can see a now points to a brand new array instance with the original values first referenced by b.


Note that printing the object references works for arrays because arrays don't implement toString, which is called automatically when a String is expected. In that case it uses Object#toString which prints the references as shown here.

You cannot print out the references to object instances if toString is implemented. Instead you can use System.identityHashCode(variable) for an indication if the references are equal. Or you could simply use if (a == b) to test for reference equality of course.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • As you can see I did away with your while loop. If you're looking *very* carefully you will also see that I use `int[] a` instead of the equivalent `int a[]`. Usually the former is preferred as `int[]` clearly indicates the *type* of `a`. `int a[]` should be considered a left over of the C syntax in Java. – Maarten Bodewes Aug 14 '18 at 09:48
-1

When you are making your array declarations, only that which is in the curly braces are actually stored in memory. (Actually, more information is stored, but for the scope of your question, you can pretend it's just what's in the curly braces)

Your variables 'a' and 'b' are just references that point in memory to the location of the values in the curly braces.

So when you say a=b now your array reference 'a' no longer points to its original location with stored values, but rather to the location and stored values also pointed to by 'b'.

Aaron
  • 192
  • 6
-1

a=b
that is a shallow copy because a and b have the same reference , any change of array "a" will change of array "b"

if you want copy the value (Deep copy)

for(int i = 0 ; i<a.length() , i++) // assume the size of two array are equal 
{
    b[i] = a[i] ; 
}