简体   繁体   English

整数数组如何成为引用类型?

[英]How can an integer array be a reference type?

BufferedImage image = new BufferedImage(Defaults.SCREEN_WIDTH, Defaults.SCREEN_HEIGHT, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();

I found this code in an open source project and I just want to figure out what's happening. 我在一个开源项目中发现了这个代码,我只想弄清楚发生了什么。 Whenever I change a value of 'pixels' it gets "written" into the buffered image. 每当我更改“像素”的值时,它就会“写入”缓冲图像。 I though that because integers in Java are primitive types, not reference types, naturally, arrays of integers would be too. 我认为,因为Java中的整数是原始类型,而不是引用类型,当然,整数数组也是如此。 So I guess my question is, are all arrays reference types in Java? 所以我想我的问题是,Java中的所有数组引用类型都是?

As Chapter 10 of the JLS specifies: 正如JLS的第10章规定:

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). 在Java编程语言中,数组是对象(§4.3.1),是动态创建的,可以分配给Object类型的变量(§4.3.2)。 All methods of class Object may be invoked on an array. 可以在数组上调用Object类的所有方法。

Yes, all arrays are actually objects. 是的,所有数组实际上都是对象。

From what I see, I assume the type "DataBufferInt" was defined in a way that it extends from "java.lang.Integer". 从我看到的,我假设“DataBufferInt”类型的定义方式是它从“java.lang.Integer”扩展。
Later, the values are saved into the int[] in-spite of that being primitive, because of the feature of "auto-boxing" in Java. 之后,由于Java中的“自动装箱”功能,这些值被保存到int []中,尽管它是原始的。

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

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