7

While creating an array, we can pass short, char, byte, int. So, [why] is int[] a = new int['a'] valid? It doesn't throw a compile time error.

What does an array declaration in this form mean?

user2864740
  • 60,010
  • 15
  • 145
  • 220
kwishna
  • 412
  • 7
  • 14
  • `'a'` is a `char` literal. In Java, `char` is a numeric type, so expressions of that type can be used where a number is required, including to specify the size of an array. – John Bollinger Sep 14 '18 at 20:34
  • 1
    @Oleksandr I think this question is different enough from that. – Sweeper Sep 14 '18 at 20:36
  • @Sweeper And it's still effectively the same answer: https://stackoverflow.com/a/8549716/2864740 D: (Although the answers given here are more precise and better formatted.) – user2864740 Sep 14 '18 at 20:42
  • When I Tried To Find The Length Of This Array. I Got 97. I Means int[] a = new int['a']; Is Actually Creating An Array With Length 97. 97 Is ASCII Value Of 'a'. – kwishna Sep 14 '18 at 20:43
  • 1
    @ABD Please consider accepting the answer that has been the most helpful to you by clicking on that checkmark. – Sweeper Sep 14 '18 at 21:50

5 Answers5

8

From JLS Sec 15.10.1:

The type of each dimension expression within a DimExpr must be a type that is convertible (§5.1.8) to an integral type, or a compile-time error occurs.

Each dimension expression undergoes unary numeric promotion (§5.6.1). The promoted type must be int, or a compile-time error occurs.

And from JLS Sec 5.6.1:

If the operand is of compile-time type Byte, Short, Character, or Integer, it is subjected to unboxing conversion (§5.1.8). The result is then promoted to a value of type int by a widening primitive conversion (§5.1.2) or an identity conversion (§5.1.1).

and

Otherwise, if the operand is of compile-time type byte, short, or char, it is promoted to a value of type int by a widening primitive conversion (§5.1.2).

So, any of Byte, Short, Character, Integer, byte, short, char or int are acceptable.

Hence 'a', a char literal, is allowed, and is promoted to the int value 97.

Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
5

Like most times with this kind of questions, the answer lies in the Java Language Specification:

§ 5.1.2. Widening Primitive Conversion

19 specific conversions on primitive types are called the widening primitive conversions:

  • byte to short, int, long, float, or double
  • short to int, long, float, or double
  • char to int, long, float, or double
  • int to long, float, or double
  • long to float or double
  • float to double

(Emphasis mine)

It may or may not be intuitive, but a char is actually an integral type, and the Java rules specify that primitive integral types may be converted to an integral type with a lower or higher-capacity. In this case, it's called a widening primitive conversion.


Andy's answer provides more references to the Java Language Specification, and explicitly states that an expression as in your question is valid.

Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
2

According to the JLS §15.10.1 Array Creation Expressions,

The type of each dimension expression within a DimExpr must be a type that is convertible (§5.1.8) to an integral type, or a compile-time error occurs.

Each dimension expression undergoes unary numeric promotion (§5.6.1). The promoted type must be int, or a compile-time error occurs.

Here, unary numeric promotion occurs. Here is an excerpt from §5.6.1:

...if the operand is of compile-time type byte, short, or char, it is promoted to a value of type int by a widening primitive conversion (§5.1.2).

Therefore, new int['a'] is valid.

If you are wondering how exactly is a char converted to an int, here is the relevant bits from §5.1.2:

A widening conversion of a char to an integral type T zero-extends the representation of the char value to fill the wider format.

Also note that characters are all encoded as integers.

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

In java char is integer type (hello C++). This code do same int[] a = new int[97];

0

a is a char type and it is implicitly casted to an integer with value 97 (ASCII representation, see details here). Therefore you are creating an array of length 97.

Jezor
  • 3,253
  • 2
  • 19
  • 43