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 8194385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:48:21+00:00 2026-06-07T04:48:21+00:00

Object[] array = new Object[]{}; System.out.println((array instanceof Serializable));//passed System.out.println((array instanceof Cloneable));//passed This code compiles

  • 0
Object[] array = new Object[]{};
System.out.println((array instanceof Serializable));//passed
System.out.println((array instanceof Cloneable));//passed

This code compiles and runs. The output is:

true
true

However, this code doesn’t compile:

System.out.println((array instanceof Iterable));//not passed

The Eclipse compiler reports:

Incompatible conditional operand types Object[] and Iterable

I found that arrays can only be compared between interfaceSerializable and Cloneable when using operation instanceof. Can someone tell me why?

  • 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-07T04:48:22+00:00Added an answer on June 7, 2026 at 4:48 am

    According to the JLS, Java SE 7 edition, §15.20.2 (Type Comparison Operator instanceof):

    If a cast of the RelationalExpression to the ReferenceType would be rejected as a
    compile-time error, then the instanceof relational expression likewise produces
    a compile-time error. In such a situation, the result of the instanceof expression
    could never be true.

    And §15.16 (Cast Expressions) states:

    It is a compile-time error if the compile-time type of the operand may never be
    cast to the type specified by the cast operator according to the rules of casting
    conversion (§5.5).

    Finally, §5.5.1 (Reference Type Casting) states:

    Given a compile-time reference type S (source) and a compile-time reference type
    T (target), a casting conversion exists from S to T if no compile-time errors occur
    due to the following rules.

    […]

    If S is an array type SC[], that is, an array of components of type SC:

    • If T is an interface type, then a compile-time error occurs unless T is the type
      java.io.Serializable or the type Cloneable (the only interfaces implemented
      by arrays).

    Therefore, Java requires that your test to see if an array type is an instance of java.lang.Iterable results in a compile-time error.

    If you want to try and make it work (always return false) anyway, you can cast the array to Object first, like so:

    System.out.println((((Object)array) instanceof Iterable));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

String a []= {null,null,null,null,null}; //add array to arraylist ArrayList<Object> choice = new ArrayList<Object>(Arrays.asList(a)); System.out.println(choice.size());
Hello i'm using map() jquery to create a new object array DEMO but this
In my code an array is declared as follows private Object[,] cellInfos = new
This is my code snippet.I want to return 2D Object type array from the
Given var myHash = new Array(); myHash['key1'] = { Name: 'Object 1' }; myHash['key2']
object[] objs = new object[]{one,two,three}; Are the strings stored in the array as references
I have an array an a timer that adds a new object to my
I have converted an array to object data like this: <?php $myobject->data = (object)Array('zero','one','two');
I get this exception when i try to cast an Object array to a
I'm trying to output a string from an Object that has been passed into

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.