Cannot convert from char [] to int []

I can assign char int as follows.

char c = 'a'; int a = 0; a = c; 

Then why can't I assign char [] to int [] ?

 int[] ints= new int[4]; char[] chars= new char[4]; ints = chars; // Cannot convert from char[] to int[] ?? But why? 
+4
source share
6 answers

char to int promotion is a special provision for primitive types.

Regarding arrays, the Java Language Specification says the following:

If an array variable v is of type A [], where A is a reference type, then v may contain a reference to an instance of any type of array B [] provided by B, A. It may be thrown at runtime; see Β§ 10.10 for a discussion.

This only works "if A is a reference type." Even if char can be assigned to int , this rule does not apply, since it does not apply to primitive types.

Thus, without a special provision for assigning incompatible types, assignment is not performed.

If this were allowed, you would present the possibility of creating an ArrayStoreException in stores for primitive arrays (as is currently the ArrayStoreException with arrays of reference types):

 ints[0] = Integer.MAX_VALUE; /* Exception! */ 

This is because ints is an alias of char[] that cannot accommodate a 32-bit value. I'm not sure why this is acceptable for reference types and not for primitives, but this is probably due to all the special treatment that is already required for primitives.

+4
source

Not sure if I can provide more for you, besides the concept of casting from char [] to int [], there is no language specification.

At a low level, this is probably due to iteration through the array. If I consider char [] as int [], indexing through it in memory at a low level will not work. The 4 byte step (processing as int []) will actually be index 2 in char [].

+2
source

Then why can't I assign char [] int []?

The first answer is that the Java language specification prohibits this. Key Section 4.10.3. , which defines subtyping rules for array types. Rules mean that a primitive array type is not a subtype of another (different) primitive array type. One consequence of this is the appointment is prohibited.

The second answer (and the reason JLS forbids it) is that it would break the type system if it were allowed. Consider this example:

 int[] ints= new int[4]; char[] chars= new char[4]; ints = chars; 

Destination is a reference job. This means that ints now points to the array object that was created by new char[4] on the previous line. (This is NOT equivalent to a loop that assigns chars ' values ​​to ints elements.)

Now add a method:

 public void paranoidIncrement(int[] ints) { for (int i = 0; i < ints.length; i++) { int tmp = int[i]; ints[i] = ints[i] + 1; assert tmp + 1 == ints[i]; } } 

and combine it with the previous code:

 int[] ints= new int[4]; char[] chars= new char[4]; ints = chars; // assume this is legal ... paranoidIncrement(ints); 

So what does that mean? Well paranoidIncrement will consider the argument (which really is char[] ), as if it were int[] . But what happens when we do ints[i] = ints[i] + 1; ? If the cells of the actual array are really char , then the JVM must truncate the values ​​so that they match or throw an exception.

  • If it truncates the value, the assert will fail. And it is so contradictory that it just broke through.

  • If it throws an exception, then we suddenly have a whole class of new errors (in accordance with the JLS rules) that are currently detected at compile time.

The way Java avoids this malfunction is that you cannot assign char[] int[] .

+2
source
 int[] a = new int[4]; int[] b = a; 

Both a and b will refer to the same array - copying is not involved.

If ints=chars enabled, you will have an int[] variable referencing an array of 16-bit char s. An assignment such as ints[0] = 0x12345678 will be valid, but what should be the result?

+1
source

You cannot assign in this way because it is a type mismatch. The int [] and char [] types are incompatible.

0
source

It does not work as you expect, because (in the first case) the language hides from you some details of type conversion. That is, when you assign char to an int value, it is sometimes called an implicit product. This is equivalent to this:

 char c = 'a'; int a = 0; a = (int) c; 

... which will work fine, but the language does not force you to write casting (int) every time.

In the second example, the cast will not work. If you try to add an explicit cast, you will get an error, for example:

 Foo.java:9: inconvertible types found : char[] required: int[] int[] ints = (int[]) chars; 
0
source

Source: https://habr.com/ru/post/1393073/


All Articles