0

Here is some code

package deleteit;
import java.lang.*;
import java.io.*;
import java.util.*;

public class Deleteit {

public static void main(String[] args) throws IOException {

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    ArrayList<Integer> arr1=new ArrayList<Integer>();
    arr1.add(100000);
    arr1.add(1);
    arr1.add(100000);
    arr1.remove(1);
    if(arr1.get(0)==arr1.get(1))
    {
        System.out.println("GUd");
    }
    else
       System.out.println("damnit");

}

}

Now Gud should be printed as 100000 is equal to 100000 as 1 gets removed from the ArrayList so it is left with 100000 and 100000 . So why does damnit gets printed ?

Is that a BUG?

damnitman1
  • 17
  • 6
  • use `if (arr1.get(0).equals(arr1.get(1)))` – YCF_L Jan 06 '18 at 15:25
  • Because `==` rather than `.equals()`. Also not that `arr1.remove(1)` is tricky - does it remove the items at index `1` or an item that `.equals(1)`? – Boris the Spider Jan 06 '18 at 15:26
  • Why is that so ? any reason for that ? I always use arr1.get(0)==arr1.get(1) – damnitman1 Jan 06 '18 at 15:26
  • It removes Items at index 1 – damnitman1 Jan 06 '18 at 15:27
  • U can debug it using Netbeans , etc – damnitman1 Jan 06 '18 at 15:27
  • could someone pls explain why `unboxing` not working here?? – Shubhendu Pramanik Jan 06 '18 at 15:28
  • _Sure_? There are [two `remove` methods](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#remove(java.lang.Object)). Do you know why it calls one rather then the other? – Boris the Spider Jan 06 '18 at 15:28
  • Also related: [Properly removing an Integer from a List](https://stackoverflow.com/q/4534146) (it's probably coincidental that your code removes the element you were trying to remove - try changing 1 in add(1) and remove(1) to 3 and your code should throw an exception) – Bernhard Barker Jan 06 '18 at 15:28
  • What unboxing @ShubhenduPramanik? You have **boxing**. You have a `List` after all... Generics don't support primitives. – Boris the Spider Jan 06 '18 at 15:29
  • With `==` you compare two objects - not values. Two objects have different hash codes. To fix this use `.equals()` instead of `==` or `arr1.get(0).intValue()==arr1.get(1).intValue()` – Taras Sheremeta Jan 06 '18 at 15:30
  • @TarasSheremeta "_Two objects have different hash codes_", since when? There is no requirement for that; the only requirement is that if two objects are not `equals` then they must have different hash codes - beyond that, nothing is required. – Boris the Spider Jan 06 '18 at 15:30
  • @BoristheSpider Sir, I mean `if((int)arr1.get(0)==(int)arr1.get(1))` will result `true`. So, why is the condition `if(arr1.get(0)==arr1.get(1))` not getting converted to primitive `int` first ? – Shubhendu Pramanik Jan 06 '18 at 15:34
  • @ShubhenduPramanik why on earth would it? `Integer` has a method `equals` that takes `Object`. You are calling _that_ - no boxing is required so none is done. – Boris the Spider Jan 06 '18 at 15:36
  • IT was returning false thatz y it printed damnit instead of GUd see the if else condition @ShubhenduPramanik – damnitman1 Jan 06 '18 at 15:36
  • @BoristheSpider, yes, sorry. Different addresses in memory, of course. – Taras Sheremeta Jan 06 '18 at 15:36
  • @BoristheSpider sorry, I got confused with `int c = 9; Integer a = c; int b = a;` . – Shubhendu Pramanik Jan 06 '18 at 15:40

1 Answers1

2

You have to use equals not ==.

Here is some sample code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class DeleteIt {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        arr1.add(100000);
        arr1.add(1);
        arr1.add(100000);
        arr1.remove(1);
        if (arr1.get(0).equals(arr1.get(1))) {
            System.out.println("GUd");
        } else
            System.out.println("damnit");
    }
}

Number objects (Integer, Double, Short) should always be compared with .equals and not ==. The same applies to String, by the way.

If you use == you are comparing references and not the actual values.

By the way, regarding unboxing and using the == sign. If you apply intValue() on the first value of the comparison, you will be able to use the == sign. So this code will also work:

package stackoverflow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class DeleteIt {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        arr1.add(100000);
        arr1.add(1);
        arr1.add(100000);
        arr1.remove(1);
        if (arr1.get(0).intValue() == arr1.get(1)) {
            System.out.println("GUd");
        } else
            System.out.println("damnit");

    }

}

This latter code seems to force unboxing and then the == is not comparing references, but actual values.

gil.fernandes
  • 11,381
  • 5
  • 51
  • 69