title: Arrays.sort throwing class cast exception
body: <p>The Arrays.sort function throws ClassCastException in the following scenario:</p>
  
  <pre><code>public static void main(String[] args) {
          Object[] obj = {
                  new String("string"),
                  new Integer(10)
          };
          Arrays.sort(obj);
      }
  </code></pre>
  
  <p>I would expect an exception here, and the error message says:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ClassCastException:
    java.lang.String cannot be cast to java.lang.Integer  at
    java.lang.Integer.compareTo(Integer.java:52)  at
    java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
      at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)     at
    java.util.ComparableTimSort.sort(ComparableTimSort.java:146)  at
    java.util.Arrays.sort(Arrays.java:472)</p>
  </blockquote>
  
  <p>Is the sort() function is trying to cast the first value in array to second value?<br/>
  I reversed the elements in Object array to:<br/></p>
  
  <pre><code>Object[] obj = {
                      new Integer(10),
                      new String("string")
              };
  </code></pre>
  
  <p>Now the Exception says that Integer cannot be cast to String:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ClassCastException:
    java.lang.Integer cannot be cast to java.lang.String  at
    java.lang.String.compareTo(String.java:108)   at
    java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
      at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)     at
    java.util.ComparableTimSort.sort(ComparableTimSort.java:146)  at
    java.util.Arrays.sort(Arrays.java:472)</p>
  </blockquote>
  
  <p>I know sorting in this scenario is not possible, but what exactly is happening internally?</p>