0

Why mistake?

public class Program { public static void main(String[] args) { int[5] intArr = new int[5]; System.out.println(intArr.length); } }

12th Jul 2018, 3:38 PM
MakarovDs
2 odpowiedzi
+ 1
error: ']' expected int[5] intArr = new int[5]; ^ // (under the 5) First, your variable type is "integer array" or "array of integers". "Integer array of length 5" is not a type, in the sense that "how long it is" is not likely to be a defining (object-like) feature of an array. Length is a property of the array, but it is not something you really use to "tell arrays apart" (separate arrays from each other), so length does not belong in the variable type. int[] intArr = new int[5]; // also works :) The next two errors are (as it is with many languages) the parser trying to recover / figure out if you meant something else: error: not a statement int[5] intArr = new int[5]; ^ // (under the [) error: illegal start of expression int[5] intArr = new int[5]; ^ // (under the ]) You can often ignore errors that cascade from an earlier one; they can still help though--here it may reinforce your belief that an earlier syntax issue has caused the compiler to stumble / lose its place.
12th Jul 2018, 4:25 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
When you run it, it even says where the error is. It's int intArr[] = new int[5];
12th Jul 2018, 3:47 PM
Matthias
Matthias - avatar