Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot compare two objects using instanceof

Tags:

java

oop

When trying to compare two objects passed as arguments: first as object, second as array of objects (the same kind). Getting error Incompatible conditional operand types Room and allRooms[]. I am trying to iterate in while cycle until thisRoomType is not equals to some object from rooms. How to fix it?

public abstract class Room { ...}
public class LightRoom extends Room  {...}
public class DarkRoom extends Room {...}

Caller:

release(thisRoomType, rooms)

with parameters

private Room thisRoomType = this; // is defined in DarkRoom and LightRoom 
private Room[] rooms; // is defined in DarkRoom and LightRoom 

in method:

public synchronized void release( Room thisRoom,  Room[] allRooms) {

        try {
            int j = 0;
            while(thisRoom instanceof allRooms[j]){     
                jj++;
            }
                        int nextRoom = jj;
...
    }
}
like image 678
J.Olufsen Avatar asked Jun 03 '26 01:06

J.Olufsen


1 Answers

Try this instead:

while (j < allRooms.length && thisRoom.getClass().equals(allRooms[j].getClass()))
like image 193
Óscar López Avatar answered Jun 06 '26 01:06

Óscar López