简体   繁体   English

将char数组分配给int数组

[英]Assigning a char array to an int array

Can someone explain why the last assignment in the code below is not valid 有人可以解释为什么以下代码中的最后一个分配无效

class ScjpTest extends BounceObject implements Bouncable{ 类ScjpTest扩展了BounceObject实现Bouncable {

static int ac = 5;
static char ab = 'd';

static int[] a = new int[]{1,2,3};
static int[] b = new int[]{1,2,3};
static char[] c = new char[]{'a','b'};

public static void main(String[] args){  
    a = b;
    b = a;

    ac = ab;  //This is accepted
    a = c;    //This is rejected
}

} }

The compiler complains with the following error 编译器抱怨以下错误

ScjpTest.java:10: incompatible types
found   : char[]
required: int[]
                a = c;
                    ^
1 error

The following is also accepted 也接受以下

class Animal{}
class Horse extends Animal{]

Animal[] animals = new Animal[2];
Horse[] horses = new Horses[2];

animals = horses;

Why can i then not assign a char array to an int array? 为什么不能将char数组分配给int数组?

Because the language specification forbids it. 因为语言规范禁止这样做。 It works at runtime to view a String[] as an Object[] because both have the same representation in memory. 它在运行时可以将String []视为Object [],因为两者在内存中具有相同的表示形式。 But char[] and int[] have different memory representations, and it would be unacceptably slow if every operation on an int[] had to check whether it was really a char[] in disguise. 但是char []和int []具有不同的内存表示形式,并且如果int []上的每个操作都必须变相检查它是否真的是char [],那将是令人无法接受的缓慢。

Arrays of reference type do have to do runtime checks for assignments in order to know whether to throw ArrayStoreException, but at least reads from them is independent of the element type. 引用类型的数组必须对分配进行运行时检查,以便知道是否抛出ArrayStoreException,但至少从它们中读取的内容与元素类型无关。

The array is a type itself, and casting doesn't work the same way as it primitives. 数组本身就是一个类型,并且强制转换与原始类型的工作方式不同。 So you have to loop all elements and assign them to the respective element of the other array. 因此,您必须循环所有元素并将它们分配给另一个数组的相应元素。

You can assign a char variable to an int one, because the compiler applies a broadening conversion. 您可以将一个char变量分配给一个int变量,因为编译器会应用扩展的转换。 The basis being that an int is larger than a char and as such there is no chance of loss of information in the conversion. 其基础是int大于char,因此在转换中不会丢失信息。 If you tried to assign your int to your char though, you would notice the compiler rejects your code until you put in a specific typecast. 如果您尝试将int分配给char,则会注意到编译器会拒绝您的代码,直到您进行特定的类型转换为止。 This is because narrowing conversions almost always involve a loss of data, and as such the compiler requires you, the programmer, to explicitely indicate this is what you intend to do. 这是因为缩小转换几乎总是会导致数据丢失,因此编译器要求您(程序员)明确表示这是您要执行的操作。

The same principle applies to objects. 相同的原理适用于对象。 A broadening conversion is allowed implicitely, a narrowing conversion requires a typecast. 隐式允许扩大转换,缩小转换需要类型转换。 Note that at no time can you convert objects that aren't related to each other by super-class/sub-class hierarchy. 请注意,您绝不能通过超类/子类层次结构转换彼此不相关的对象。

Number n = null;
Integer i = null;

n = i ;         // Allowed without casting, Number is superclass of Integer
i = n;          // Compiler error, Integer is sub-class of Number
i= (Integer)n;  // Allowed due to the type-cast

In the example above, Number is a superclass of Integer. 在上面的示例中,Number是Integer的超类。 Assigning an instance of Integer to instance of Number is allowed without typecast, since this is a 'broadening' conversion. 无需类型转换即可将Integer实例分配给Number实例,因为这是一个“广泛的”转换。 Assigning an instance of Number to instance of Integer requires a specific cast since this is a narrowing conversion (Number could have represented a Float, or some other subclass). 将Number的实例分配给Integer的实例需要特定的转换,因为这是一个狭窄的转换(Number可能表示Float或其他子类)。 These rules carry over to arrays. 这些规则会延续到数组中。 So, you can do the following: 因此,您可以执行以下操作:

Number[] numArr = null;
Integer[] intArr = null;

numArr = intArr;
intArr = numArr;              //Compile error
intArr = (Integer[]) numArr;

For primitive arrays, there is no broadening conversion done on the array elements, even in cases where you think that it might make sense (ie, char to int, or byte to char etc). 对于原始数组,即使在您认为可行的情况下(例如,从char到int或从byte到char等),也没有对数组元素进行任何扩展的转换。 While this might not seem to make sense, if you look at the object analogue it becomes clearer why: 尽管这似乎没有意义,但如果您看一下对象类似物,就会清楚为什么:

Double[] doubleArr = null;
Integer[] intArr = null;

doubleArr = intArr ;      // Compile error, Double,Integer are sibling classes
doubleArr = (Double[]) intArr;  // Compile error, same reason
intArr = doubleArr;             // Compile error, same reason
intArr = (Integer[]) doubleArr; // Compile error, same reason

You can not cast a Double to an Integer, or vice-versa, they are completely different classes from the point of view of the compiler (as different as String and Float would be, for example). 您不能将Double转换为Integer,反之亦然,从编译器的角度来看,它们是完全不同的类(例如,与String和Float有所不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM