-1

I have a class Objekt in java in that class I wrote a function to check objects the serial number of objects from other classes and I keep getting .equals error. I was wondering is it because I wrote String function and the serial number(Evidenca) is int or I wrote something wrong. So any ideas are welcome.

boolean obstaja = false;
    for(PoslovniProstor pp : this.PoslovniProstori)
    {
        if(pp.getEvidenca().equals(poslovniProstor.getEvidenca()))
        {
            obstaja = true;
            break;
        }
    }

This colors .equals in red bare in mind that the class I am taking an object from is PoslovniProstor and I created an ArrayList in this class Objekt and is called PoslovniProstori

public ArrayList<PoslovniProstor> getPoslovniProstori(){
    return this.PoslovniProstori;
}

So yea any help is welcome. :D

Dustaboy3
  • 43
  • 8
  • 2
    And what's the type of the value returned by `getEvidenca()`? Probably some primitive type. Primitives are compared using `==`, objects are compared using `equals()`. – Robby Cornelissen Apr 10 '20 at 09:59
  • 2
    you wrote "serial number(Evidenca) is int". `int`doesn't have an `equals` method. Use `==` instead – jhamon Apr 10 '20 at 10:01
  • Every now and then we are reminded how odd and inconsistent some aspects of Java are! – Chris Apr 10 '20 at 10:16

2 Answers2

0

As int is a primitive type, it has to be compared using == rather than .equals().

Kali
  • 16
  • 1
0

equals method and == operator have separate use cases. In short, equals is used to check for equality of value whether == is used to check for same reference. In your case, as you have said Evidenca is int, then, to compare one int to another int use == operator.

For more details, you can check this thread How do I compare strings in Java?

user404
  • 1,934
  • 1
  • 16
  • 32