Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:07:31+00:00 2026-06-03T07:07:31+00:00

I am looking for advice, because I get an error when I run the

  • 0

I am looking for advice, because I get an error when I run the following code:

public class Test9 {


    public static void main(String[] args) {        

        Object [] myObjects = {
                 new Integer(12),
                 new String("foo"),
                 new Integer(5),
                 new Boolean(true)
                 };
                 Arrays.sort(myObjects);
                 for(int i=0; i<myObjects.length; i++) {
                 System.out.print(myObjects[i].toString());
                 //System.out.print(" ");
                 }

    }

}

The error I am getting is:

   Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    at java.lang.Integer.compareTo(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at Practicequestions.Test9.main(Test9.java:18)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:07:32+00:00Added an answer on June 3, 2026 at 7:07 am
     Object [] myObjects = {
                     new Integer(12),
                     new String("foo"),
                     new Integer(5),
                     new Boolean(true)
                     };
    

    This a generic type array. So when you trying to sort it with public static void sort(Object[] a) it populate runtime ClassCastException cause array contains elements that are not mutually comparable.

    Array.sort() specified array of objects into ascending order, according to the natural ordering of its elements.
    There is a another method you can use it sort(Object[] a,Comparator c) . Implement Comparator with your own logic and pass it.

    Object[] myObjects = { new Integer(12), new String("foo"),
                    new Integer(5), new Boolean(true) };
            Comparator<Object> comparator=new Comparator<Object>() {
                @Override
                public int compare(Object obj1, Object obj2) {
                    if(obj1 instanceof String && obj2 instanceof String){
                        return String.valueOf(obj1).compareTo(String.valueOf(obj2));
                    }else if(obj1 instanceof Integer && obj2 instanceof Integer){
                        return ((Integer) obj1).compareTo((Integer) obj2);
                    }else if(obj1 instanceof Boolean && obj2 instanceof Boolean){
                        return ((Boolean) obj1).compareTo((Boolean) obj2);
                    }else if(obj1 instanceof String && obj2 instanceof Integer){
                        return 1;
                    }else if(obj1 instanceof Integer && obj2 instanceof String){
                        return -1;
                    }else if(obj1 instanceof Boolean){
                        return -1;
                    }else if( obj2 instanceof Boolean){
                        return 1;
                    }
                    return 0;
                }
            }; 
            Arrays.sort(myObjects,comparator);
            for (int i = 0; i < myObjects.length; i++) {
                System.out.print(myObjects[i].toString());
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most advice on thread safety involves some variation of the following pattern: public class
All, I'm looking for advice over the following scenario: I have a component running
Looking for advice on framework and approach for building a modular web application. Primary
I am looking for advice about how to parse a single list, using two
I'm looking for advice, tutorials and links at how to set up a mid-sized
Overview I'm looking for advice / solutions on standardizing color profiles on comps so
Looking for some advice on the best way to implement localization along with client
I'm looking for some advice on the best way to provide a single place
I'm looking for some advice on whether or not I should use a separate
I'm looking for some advice on how to go about reading the online documentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.