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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:58:09+00:00 2026-06-12T04:58:09+00:00

According to JLS 7, 5.1.6 Narrowing Reference Conversion • From any array type SC[]

  • 0

According to JLS 7, 5.1.6 Narrowing Reference Conversion

• From any array type SC[] to any array type TC[], provided that SC
and TC are reference types and there is a narrowing reference
conversion from SC to TC.

Object[] objArr = {"a","b","c"};
String[] strArr = (String[])objArr; // ClassCastException

in the above example, both objArr and strArr are reference types and there’s a narrow reference conversion from Object to String.

Object obj = "a";
String str = (String)obj;

but the following codes works fine:

Object[] objArr = (Object[])java.lang.reflect.Array.newInstance(String.class, 3);
String[] strArr = (String[])objArr;

I want to ask the rules java uses to do the casting.
As far as I can tell the differences between the two examples is that, in the first one, objArr is an Object array with component of type Object. the second is an Object array with component of type String.

Please note that, I am not asking HOW to do the conversion, don’t show me how to do this using Arrays.copyOf or other libraries.

  • 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-12T04:58:10+00:00Added an answer on June 12, 2026 at 4:58 am
    Object[] objArr = {"a","b","c"};
    String[] strArr = (String[])objArr;
    

    With that the problem is {"a","b","c"} which is an array of Objects not Strings.

    It’s like doing something as follows –

    Object obj = new Object();
    String str = (String) obj; //ClassCastException
    

    No exception with the following –

    Object[] objArr = new String[] {"a","b","c"}; //Which is the case when you are using reflection
    String[] strArr = (String[])objArr; //No exception
    

    And it’s like doing something as follows –

    Object obj = new String();
    String str = (String) obj;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to: http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html 4.5.2 Variables of Reference Type A reference type can hold a
According to select name from system_privilege_map System has been granted: SELECT ANY TABLE ...and
According to the Java Language Specification : If there are any enclosing try statements
According MSDN, FxCop is an application that analyzes managed code assemblies (code that targets
According this article , generic JPA DAO(Data Access Object) is a pretty nice pattern.
According to Hibernate documentation : After observing that arrays cannot be lazy , you
According to this post in Recursive Descent vs. LALR , any LALR(k) can be
According to this article , Enumerations don't count as single-constructor types as far as
According to the following URL, there are three versions of the Entity Framework Storage
According to a specific row value (Type), I must use a TextBox or a

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.