5

I am trying to pass an array without using a reference, but directly with the values :

    public static void main(String[] args){
            int[] result = insertionSort({10,3,4,12,2});
    }

    public static int[] insertionSort(int[] arr){
        return arr;
    }

but it returns the following exception :

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error on token(s), misplaced construct(s)
Syntax error on token ")", delete this token

When I try the following code , it works , can anybody please explain the reason ?

    public static void main(String[] args){
        int[] arr = {10,3,4,12,2};
        int[] result = insertionSort(arr);  
    }

    public static int[] insertionSort(int[] arr){
        return arr;
    }
3
  • 1
    An important note: Java never passes by reference, only by value. Commented Sep 30, 2012 at 18:16
  • What do you mean exactly by value not by reference ? because when I pass an array to a method which manipulates values of this array , values of the original array change too . Therefor , sometime I need to clone an array if I don't want the original array to be changed ! Commented Sep 30, 2012 at 19:57
  • Try to receive an array in a method and initialize the array inside the method (new int[20] or similar will do), then pass a null array to the method and print the array outside the method, it will still be null, that's because Java passes by value, not the reference of the object. Commented Sep 30, 2012 at 20:08

4 Answers 4

10

It has to be

int[] result = insertionSort(new int[]{10,3,4,12,2});

{10,3,4,12,2} is a syntactic sugar for array initialization, which must go with the declaration statement like the one in the following -

int[] arr = {10,3,4,12,2};

Something as follows is not allowed too -

int[] arr; // already declared here but not initialized yet
arr = {10,3,4,12,2}; // not a declaration statement so not allowed
Sign up to request clarification or add additional context in comments.

Comments

5
insertionSort({10,3,4,12,2})

is not valid java because you don't specify a type in your method call. The JVM does not know what type of array this is. Is it an array with double values or with int values?

What you can do is insertionSort(new int[]{10, 3 ,4 12, 2});

Comments

1

int[] array = { a, b, ...., n } is a shorthand initialization - you have to write:

int[] result = insertionSort(new int[]{10,3,4,12,2});

to initialize it anonymously.

Comments

1

Nothing much to add to what others said.

However I believe the reason why you should use new int[]{10,3,4,12,2} (like others stated) and Java doesn't let you use just {10,3,4,12,2} is that Java is strong typed.

If you just use {10,3,4,12,2} there is no clue of what the type of the array elements can be. They seem to be integers, but they can be int, long, float, double, etc...

Well, actually it might infer the type from the method signature, and fire a compile error if it doesn't fit, but it seems complicated.

1 Comment

+1 for pointing out that Java would have no clue on guessing the elements type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.