2

I wrote a java program "Test" which is:

class Get{
    static void fIntarray(int[] b){
        b = {10};
    }
}

class Test{
    public static void main(String[] args){
        int[] a;
        Get.fIntarray(a);
        System.out.println(a[0]);
    }
}

But when I compiled it, the compiler reported following faults:

Test.java:3: error: illegal start of expression
        b = {10};
Test.java:3: error:not a statement
        b = {10};
Test.java:3: error: ";" expected
        b = {10};
Test.java:5: error: class, interface, or enum expected
}

I want to create an integer array a and give value 10 to array a by passing a in the method fIntarray in Get class. I don't know where it's going wrong?

Victoria
  • 67
  • 5
  • `b = {10}` can only be used at declaration – Scary Wombat Mar 07 '18 at 07:45
  • Try `b = new int[]{10};` – Aniket Sahrawat Mar 07 '18 at 07:46
  • 6
    You could write `b = new int[] { 10 };` but that still wouldn't do what you want, because arguments are passed by value in Java - assigning a new value to `b` won't change `a`. – Jon Skeet Mar 07 '18 at 07:46
  • It works. But why it is not the case that I assign the pointer of a to fIntarray and in the method it changes where a points? – Victoria Mar 07 '18 at 07:54
  • 2
    @Victoria Read this: [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Erwin Bolwidt Mar 07 '18 at 07:56
  • @Victoria [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – BackSlash Mar 07 '18 at 07:56
  • Even if you fix "b" initialization, this code won't compile because of "int[] a" line - it causes "Variable a might not be initialized" exception. About following runtime exceptions see comments above. – amseager Mar 07 '18 at 08:00

4 Answers4

1
class Get{
    static int[] fIntarray(){ // you don't need to pass an array as argument here
        return new int[] {10};
    }
}

class Test{
    public static void main(String[] args){
        int[] a;
        a = Get.fIntarray(); // here you assign return value of Get.fIntarray() method to "a" array
        System.out.println(a[0]);
    }
}

You might want to see this question (as suggested in comments section) to understand why changes that you try to make to an array, doesn't take effect.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
  • I have written the same code like this. I want to also write a method with parameters but I haven't figured out yet. – Victoria Mar 07 '18 at 08:05
  • @Victoria When I was preparing my answer, I haven't been looking for answers of others. Anyway, I see some differences in our solutions. – Przemysław Moskal Mar 07 '18 at 08:08
  • I mean that I've written code in this way and I want to use another method in which the parameters are included in the method – Victoria Mar 07 '18 at 08:10
  • You don't even need to initialize the array with null. –  Mar 07 '18 at 08:11
  • @Victoria Parameter is not needed here. `fIntarray()` is here just to provide/return an array containg one element. You just need to assign this return value to an array. If you would create`fIntarray()` in the same class as array that you want to assign this return value to, it would make more sense to pass this array as argument. – Przemysław Moskal Mar 07 '18 at 08:16
0

is this solution okey for you ?

class Get{
static int[] fIntarray(int[] b){
    b = new int[]{10};
    return b;
}
}

class Test{
public static void main(String[] args){
    int[] a = null;
    a=Get.fIntarray(a);
    System.out.println(a[0]);
}
}
Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26
  • 2
    It works but produces warnings. You don't need to pass any parameters to fIntarray method in this case. – amseager Mar 07 '18 at 08:02
0

Java is pass-by-value, so if you reassign the array b inside your method, it would not change the passed array a outside.

Example:

class Get{
    static void fIntarray(int[] b){
        b = new int[]{10}; // This will NOT be reflected on 'a'
    }
}

class Test{
    public static void main(String[] args){
        int[] a = null; // 'a' needs to be initialized somehow, or else you get a compiler error.
        Get.fIntarray(a);
        System.out.println(a[0]); // Gives NullPointerException because 'a' was not affected by the method and is still null
    }
}

If you want your array to be filled inside your method, you need to instantiate it completely before passing it to that method.

class Get{
    static void fIntarray(int[] b){
        // Could potentially produce an ArrayIndexOutOfBoundsException if the array has length 0.
        b[0] = 10; 
    }
}

class Test{
    public static void main(String[] args){
        int[] a = new int[1]; // Create an empty array with length 1.
        Get.fIntarray(a);
        System.out.println(a[0]); // Prints 10
    }
}
QBrute
  • 4,405
  • 6
  • 34
  • 40
-1

Your error is on your array initialization, in java we initialize an array using constructor like that :

int[] tab = new int[]{10,-2,12};

And this the correct code for your case :

class Get{ static void fIntarray(int[] b){ b = new int[]{10};}} Hope that it gonna be helpful, good luck.

M.Lamine Lalmi
  • 78
  • 1
  • 2
  • 12