if(GOLDEN_PICKAXE == GOLDEN_PICKAXE) returns False? whyyyyy?

Discussion in 'Plugin Development' started by lecreep, Nov 29, 2019.

  1. Offline

    lecreep

    I got a class
    Code:
    public class GoldPortal {
        public String center;
    } 
    And a headache
    Code:
    public void searchTeleport(PlayerInteractEvent event,String frameObjectName2) {
      GoldPortal portal=new GoldPortal();
      portal.center=frameObjectName2;
       //stuff in middle
       Object obj = new JSONParser().parse(line);
       JSONObject jo = (JSONObject) obj;
       GoldPortal possible=new GoldPortal();
       possible.center = (String) jo.get("center");
       if(possible.center==portal.center) {
    
       }else{
          //i come always here
       }
    }
    console output
    Code:
    [01:14:46] [Server thread/INFO]: [pruebaPlugin] possible.center:GOLDEN_PICKAXE Portal.center:GOLDEN_PICKAXE
    should work that and the "//stuff in middle" is cracking me? or...
     
    Last edited: Nov 29, 2019
  2. Offline

    Strahan

    Don't compare String with ==, use .equals or .equalsIgnoreCase
     
    KarimAKL likes this.
  3. Offline

    lecreep

    perfect! Thanks. ;)
     
  4. Offline

    Banjer_HD

    We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects. (source)

    Please mark this as 'Solved' :)
     
  5. Offline

    KarimAKL

    @Banjer_HD I'm pretty sure Object#equals(Object) looks something like this:
    Code:Java
    1. public boolean equals(Object obj) {
    2. return obj == this;
    3. }
     
  6. Offline

    knokko

    Yes, but subclasses of Object are supposed to override equals if they need a content comparison. For instance, String.equals(Object) does a proper content comparison.
     
    Strahan likes this.
  7. Offline

    KarimAKL

    @knokko I know that. :7 Anyway, this is off topic, and the problem is already solved.
     
  8. Offline

    Strahan

    Out of curiosity then, what were you trying to say by posting the equals method code? I'm not sure I understand what you were getting at if you already knew that subclasses override...?
     
  9. Offline

    KarimAKL

    @Strahan I meant that this:
    was wrong unless the method was overriden. :7
     
  10. Offline

    Strahan

    Ahh gotcha, thx.
     
    KarimAKL likes this.

Share This Page