1

I have a weird weird weird weird error, first of all here the class which give me the error

public class SpeciePesci {


public static final SpeciePesce[] specie = (new SpeciePesce[]{
    new SpeciePesce(13,"Trota fario", R.drawable.specie_trota_fario, R.string.specie_descrizione_trota_fario),
    new SpeciePesce(14,"Trota iridea", R.drawable.specie_trota_iridea, R.string.specie_descrizione_trota_iridea),
    new SpeciePesce(4,"Trota marmorata", R.drawable.specie_trota_marmorata, R.string.specie_descrizione_trota_marmorata),
    new SpeciePesce(15,"Trota lacustre", R.drawable.specie_trota_lacustre, R.string.specie_descrizione_trota_lacustre),
    new SpeciePesce(195,"Salmerino alpino", R.drawable.specie_salmerino_alpino, R.string.specie_descrizione_salmerino_alpino),
    new SpeciePesce(18,"Salmerino di fonte", R.drawable.specie_salmerino_fonte, R.string.specie_descrizione_salmerino_fonte),
    new SpeciePesce(19,"Carpa", R.drawable.specie_carpa, R.string.specie_descrizione_carpa),
    new SpeciePesce(20,"Tinca", R.drawable.specie_tinca, R.string.specie_descrizione_tinca),
    new SpeciePesce(16,"Temolo", R.drawable.specie_temolo, R.string.specie_descrizione_temolo),
    new SpeciePesce(22,"Persico reale", R.drawable.specie_persico_reale, R.string.specie_descrizione_persico_reale),
    new SpeciePesce(21,"Lucio", R.drawable.specie_lucio, R.string.specie_descrizione_lucio)
});


public static Integer getPosition(Integer id){
    Integer pos=  -1;
    if(195==195){
        Log.d("INT==","195==195  ?? OK");

    }
    if(id==195){
        Log.d("INT==","ID==195  ?? OK");

    }
    for (int i = 0; i < specie.length; i++) {
        if(specie[i].getId()==195){
            Log.d("INT==","getID==195  ?? OK");

        }
        Log.d("INT==",""+specie[i].getId()+"=="+id);
        if (specie[i].getId()==id ) {
                Log.d("INT==","OK");
                pos = i;
                break;
        }
    }
    return pos;
}
}

and the other class

public class SpeciePesce {
private Integer id;
private String title;
private Integer resourceImage;
private Integer resourceDescription;

public SpeciePesce(Integer id, String title, Integer resourceImage, Integer resourceDescription) {
    this.id = id;
    this.title = title;
    this.resourceImage = resourceImage;
    this.resourceDescription = resourceDescription;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Integer getResourceImage() {
    return resourceImage;
}

public void setResourceImage(Integer resourceImage) {
    this.resourceImage = resourceImage;
}

public Integer getResourceDescription() {
    return resourceDescription;
}

public void setResourceDescription(Integer resourceDescription) {
    this.resourceDescription = resourceDescription;
}
}

When I call the method getPosition(X) is suppose I receive the position in the array of the item with such Id; until here it is simple and it works great with all ids except 195. When it compare 195==195 it gives me false.. what can be??

as see I did log various case ..here the result

05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 195==195  ?? OK
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ ID==195  ?? OK
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 13==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 14==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 4==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 15==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ getID==195  ?? OK
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 195==195  << suppose the loop stop here
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 18==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 19==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 20==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 16==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 22==195
05-23 14:30:28.665      560-560/com.evosw.altosarca D/INT==﹕ 21==195

here is an example of a working one

05-23 14:35:30.068    3153-3153/com.evosw.altosarca D/INT==﹕ 195==195  ?? OK
05-23 14:35:30.068    3153-3153/com.evosw.altosarca D/INT==﹕ 13==13
05-23 14:35:30.068    3153-3153/com.evosw.altosarca D/INT==﹕ OK
05-23 14:35:30.103    3153-3153/com.evosw.altosarca D/INT==﹕ 195==195  ?? OK
05-23 14:35:30.103    3153-3153/com.evosw.altosarca D/INT==﹕ 13==13
05-23 14:35:30.103    3153-3153/com.evosw.altosarca D/INT==﹕ OK
05-23 14:35:30.103    3153-3153/com.evosw.altosarca D/INT==﹕ 195==195  ?? OK
05-23 14:35:30.103    3153-3153/com.evosw.altosarca D/INT==﹕ 13==13
05-23 14:35:30.103    3153-3153/com.evosw.altosarca D/INT==﹕ OK

what can be? :S

Jonik
  • 80,077
  • 70
  • 264
  • 372
BuBy
  • 59
  • 5
  • well i did discover which i get this error with number greater than 127 ,, so from 128 and upper i have this error....uhmm – BuBy May 23 '15 at 19:11

1 Answers1

2

This is because you are not using the primitive int type. You are using the reference Integer type. Your getPosition() method returns Integer.

While comparing two reference with == operator it's actually compare it's reference not it's value. If you want to check whether two object are meaningfully equals then use equals() instead.

But note if the both Integer you are comparing were less than 128 then the comparison should returns true.
Note:

    Integer a = 101;
    Integer b = 101;
    Integer c = 1234;
    Integer d = 1234;
    Integer e = a;


    System.out.println(a==b); //true - same reference; for explanation read the attached link
    System.out.println(a==e); //true - same reference; 'e' referencing 'a'
    System.out.println(c==d); //false - diffrent reference
    System.out.println(c.equals(d)); //true - meaningfully equals 

You may read the answer for more details.

Community
  • 1
  • 1
Razib
  • 10,965
  • 11
  • 53
  • 80