0
import java.util.Scanner;
public class z {
public static void main(String[] args) {
String[] arr = {"me", "my", "mine", "i"};
    whatIsThis(arr);
    for (int k = 0; k < arr.length; k++) {
        System.out.print(arr[k] + " ");
    }
    System.out.println();
}

public static void whatIsThis(String[] arr) {
    arr = new String[5];
    arr[0] = "I"; 
    arr[2] = "loving";
}
}

Why does this code output "me my mine i" instead of "I my loving i" This is a question on my sample final exam and I want to make sure I understand.

Brooke
  • 13
  • 4
  • You're not modifying the original `arr` instance on `main()` because `whatIsThis()` has no return, you need to add a return type to that method. `String[]` in this case. And then add a `return arr;` statement at the end of the method. – Julian Aug 10 '16 at 18:45
  • Thanks Julian!! I understand now, you're a lifesaver! – Brooke Aug 10 '16 at 18:47

0 Answers0