cglotr
cglotr

Reputation: 916

What does (Object) do in java?

I watched a tutorial on 2D game engine design in YouTube and there is this line:

private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

I know that the pixels are supposed to contain integer list of colors for the image but I don't understand how the data of the pixels got into the image since in the tutorial this is the only line where pixels are used.

So is (DataBufferInt) means I am connecting pixels to the data of type DataBufferInt in the image?

Upvotes: 2

Views: 118

Answers (1)

gaborsch
gaborsch

Reputation: 15758

It is called casting. The object (which had a different runtime type) will be considered as the new given type.

Say, that image.getRaster().getDataBuffer() returns a type DataBuffer. But in reality (the runtime type) it is DataBufferInt.

Your DataBuffer type does not have a getData() method, that would return int[]. So, you need to tell the compiler that it is a DataBufferInt, so you could get the data as int[].

If the runtime data type is different, and you try to cast, you will get a ClassCastException.

Upvotes: 4

Related Questions