Array in Java program bizarre behaviour
<p>I came across this Java program and its behaving in unexpected way. The following program computes the differences between pairs of elements in an int array.</p>

<pre><code>import java.util.*;

public class SetTest
{
       public static void main(String[] args)
       {
            int vals[] = {786,678,567,456,
                          345,234,123,012};

            Set&lt;Integer&gt; diffs = new HashSet&lt;Integer&gt;();

            for(int i=0; i &lt; vals.length ; i++)
                for(int j = i; j &lt; vals.length; j++)
                       diffs.add(vals[i] - vals[j]);

            System.out.print(diffs.size());
       }
}
</code></pre>

<p>If we analyze it seems set size should be 8 which is the size of the array. But if you ran this program it prints 14. What's going on? Any idea? </p>

<p>Thank you in advance.  </p>

<p>Answer: This strange behavior happens because 012 in the array becomes octal if we change it to 12 then it prints 8 as expected. </p>

<p>Lesson: Never pad an integer literal with zeros.</p>
