0
import java.util.Arrays;
public class Test {
    public static void main(String... args) {
        String[] strings = new String[] { "foo", "bar" };
        changeReference(strings);
        System.out.println(Arrays.toString(strings)); // still [foo, bar]
        changeValue(strings);
        System.out.println(Arrays.toString(strings)); // [foo, foo]
    }
    public static void changeReference(String[] strings) {
        strings = new String[] { "foo", "foo" };
    }
    public static void changeValue(String[] strings) {
        strings[1] = "foo";
    }
}

Can anyone explain these questions?

  1. What is Strings[]. Is it a String Object or String Object containing array of Objects.
  2. What does the changeReference() and changeValue() functions do and return?
  3. Does Java support Pass by Reference?
greydet
  • 5,509
  • 3
  • 31
  • 51
John Cooper
  • 7,343
  • 31
  • 80
  • 100
  • Have you actually tried to figure these out by yourself using Google and/or a Java book? Answers to these can be found quite easily if you're willing to put in the effort. – deltaforce2 Jun 08 '11 at 11:08

4 Answers4

4
  1. strings is an array of Strings. Arrays are objects for our purposes here, which means they are a reference type.

  2. changeReference does nothing useful. It receives a reference to strings, but it receives that reference by value. Reassigning strings within the function has no effect on the strings being passed in -- it just replaces the local copy of the reference, with a reference to a new array. changeValue, on the other hand, modifies the array object referred to by strings. Since it's a reference type, the variable refers to the same object.

  3. No, "pass by reference" is not supported. Java can pass references around, but it passes them by value. Summary being, you can change the object being passed in, but you can't replace the object in such a way that the caller will see it.

cHao
  • 84,970
  • 20
  • 145
  • 172
2

What is Strings[]. Is it a String Object or String Object containing array of Objects.

It’s neither. It’s an object (well, actually it’s a type) that references an array of strings.

What does the chanfeReference and changeValue function do and return?

Please try it yourself to see the effect.

Does Java support Pass by Reference?

No. Java is always pass by value.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Its always pass by value, but the value is a reference. See here http://stackoverflow.com/questions/40480/is-java-pass-by-reference/40523#40523 – Simeon Jun 08 '11 at 11:10
  • 2
    passed by value for primitive, but reference to objects (an array is also passed by reference) – Yanick Rochon Jun 08 '11 at 11:11
  • just for sake of clarification, there are no pointers (like in C) in Java,therefore a variable passes it's reference to an object; so assigning an **object** from `a` to `b` then changing `b` will change also `a`, however assigning a new `object` to `b` will *not* change `a` – Yanick Rochon Jun 08 '11 at 11:17
  • 2
    @YanickRochon Sorry, that’s wrong. References (and hence arrays) are passed by value too. This is a common confusion but to see that it’s true just look at the result of OP’s code (more specifically, the `changeReference` method). – Konrad Rudolph Jun 08 '11 at 11:21
  • 1
    oh, I do understand. I also understand why "'pass by reference` is not supported". And what I said *is* true. For example: `String[] a = new String[] {"foo"}, b = a;` then `b` points to the same object as `a`, while `int i = 1, j = i;` isn't (because it's a primitive type), and `b[0] = "bar";` will cause `a[0]` to print "bar" as well, however `b = new String[] {...};` will **not** change `a` at all. – Yanick Rochon Jun 08 '11 at 11:28
2

What is String[]. Is it a String Object or String Object containing array of Objects.

String[] is an array of String (and String is an Object).

What does the changeReference and changeValue function do and return?

In changeReference() java changes the reference of strings to an new string array. In changeValue(), java changes the value of the first element of strings array.

Does Java support Pass by Reference?

Java supports Pass by Value. As stated on JavaWorld:

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
1
  1. String[] is an array of String objects
  2. changeReference changes the refence to the array strings to a new refence to a new array which, in this case, contains the same thing, but the reference in the memory is in another place.
  3. Pass by Reference is not supported in Java
Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69