Question

Can anyone explain what the purpose of the ints are in Rectangle center and Rectangle obstacle? I already understand that an int is a positive or negative whole number. Also why don't I need any ints in Rectangle left and Rectangle right if they are needed in center and obstacle? I'm still new to this so if anyone thinks I should include more code just let me know.

Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);

Rectangle center = new Rectangle((int)((WIDTH/9)* 2.5),(int)((HEIGHT/9)*2.5),(int)  
((WIDTH/9)*5),(HEIGHT/9)*4);
Rectangle obstacle = new Rectangle(WIDTH/2,(int)((HEIGHT/9)*7),
WIDTH/10,HEIGHT/9);
Was it helpful?

Solution

Java is a strongly typed language, which means every variable has a data type. However, via polymorphism you can use the information from one data type and handle it as a different one. Examples of this in Java are interfaces & subclasses. You can do this conversion either by implicit or explicit casting. That is what is shown here.

When you use a primitive data type or object name in parentheses before another variable, it casts to another compatible type.

As long as the internal data is compatible, you can convert the type to something else. For example, if the Object in question here was a String:

if(s instanceof String) {
String t = (String)s;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top