-5

I have the following code.

import java.util.Arrays;

public class Practice {  

    public static void main(String[] args) {
        System.out.println(checkEquality({1,2,3},{1,2,3}));
    }

    public static boolean checkEquality(int[] a, int[] b) {
        return  Arrays.equals(a,b);
    }
}

I am trying to write a method that checks for equality between two arrays of type int. The code as written doesn't compile, giving me an illegal start of expression error for line 11 (the one which has the print statement). However if I first define two arrays in the main method

int[] a= {1,2,3};
int[] b= {1,2,3};

and then change

System.out.println(checkEquality({1,2,3},{1,2,3}));

to System.out.println(checkEquality(a,b));

the code compiles and works as it should, giving me true.

My question: why can't I input the two arrays that I want to check for equality directly into the print statement? This has worked with all other methods that didn't have array, ex expressions involving integers and strings. Am I doing something wrong here or is this generally not possible? If so, why?

David Artmann
  • 4,272
  • 1
  • 16
  • 24
user140161
  • 125
  • 6
  • ```System.out.println(checkEquality((1),2,3),(1),2,3);));``` => Seriously, you've not tried to compile your code. – Mik378 Nov 09 '17 at 22:37
  • 2
    You can't just make up some syntax and ask why the compiler doesn't understand it. – shmosel Nov 09 '17 at 22:39
  • 2
    *"Am I doing something wrong here ..."* - Yes. You are trying to program Java without doing a tutorial first. Any half-decent Java tutorial or text book will explain the syntax for creating and initializing array objects. – Stephen C Nov 09 '17 at 22:42
  • @Mik378 Sorry that was a typo copy and pasting the code to the website! I had initially used System.out.println(checkEquality({1,2,3},{1,2,3})); but that wasn't compiling so I just went along with the error messages that DrJava was giving me, adding the additional parenthesis. I have edited the question to correct this – user140161 Nov 09 '17 at 22:48
  • Stackoverflow is not a mean to free you to search on the net yourself before for such simple things. Just typing "java array inline" on Google should have done the trick. Test it. – Mik378 Nov 09 '17 at 22:50
  • Not to be rude but sometimes the comments here baffle me. Had I known that was the correct syntax, why would I have written this question to begin with? I am not trying to code Java without a tutorial, I looked through the material that my instructor gave me, in fact I have it it front of me right now, and wasn't able to find what was wrong. And yes, I also searched on google and on this site for similar questions – user140161 Nov 09 '17 at 23:03
  • Maybe I was looking in the wrong place or maybe I didn't use the right search terms or missed a similar question but I made an effort. I was clear in what I was asking and I posted my code. It's hard to understand to see why someone would take the time to write something condescending when they could've merely pointed in the right direction to look at in half the time and half the words. – user140161 Nov 09 '17 at 23:03

2 Answers2

2

Array can be initialized in following ways ...

int[] a = new int[10];
int[] a = new int[]{1,2,3,4,5,6,7,8,9,0}
int[] a = {1,2,3,4,5,6,7,8,9,0}; // anonymous array

Last one is called anonymous array as it doesn't let you know the size and the type of elements it have. Hence anonymous arrays directly can't be passed as a method parameter. First two approaches can be used to pass an array as a method parameter directly.

Here is what Java Specification have to say

https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.6

An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values.

An array initializer is written as a comma-separated list of expressions, enclosed by braces { and }.

A trailing comma may appear after the last expression in an array initializer and is ignored.

Each variable initializer must be assignment-compatible (§5.2) with the array's component type, or a compile-time error occurs.

It is a compile-time error if the component type of the array being initialized is not reifiable (§4.7).

Here is what reifiable means https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.7

Because some type information is erased during compilation, not all types are available at run time. Types that are completely available at run time are known as reifiable types.

Hence for your case, you can go with following 2 approaches ...

System.out.println(checkEquality(new int[]{1,2,3},new int[]{1,2,3}));

int[] a = {1,2,3};
int[] b = {1,2,3};
System.out.println(checkEquality(a,b));
Community
  • 1
  • 1
JRG
  • 4,037
  • 3
  • 23
  • 34
1

You can, it's just that your integer array constructors are incorrect. Try:

System.out.println(checkEquality(new int[]{1,2,3},new int[]{1,2,3}));
Catchwa
  • 5,845
  • 4
  • 31
  • 57