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

  • Home
  • SEARCH
  • 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 86953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:19:37+00:00 2026-05-10T22:19:37+00:00

I think there must be something subtle going on here that I don’t know

  • 0

I think there must be something subtle going on here that I don’t know about. Consider the following:

public class Foo<T> {   private T[] a = (T[]) new Object[5];    public Foo() {     // Add some elements to a   }    public T[] getA() {     return a;   } } 

Suppose that your main method contains the following:

Foo<Double> f = new Foo<Double>(); Double[] d = f.getA(); 

You will get a CastClassException with the message java.lang.Object cannot be cast to java.lang.Double.

Can anyone tell me why? My understanding of ClassCastException is that it is thrown when you try to cast an object to a type that cannot be casted. That is, to a subclass of which it is not an instance (to quote the documentation). e.g.:

Object o = new Double(3.); Double d = (Double) o; // Working cast String s = (String) o; // ClassCastException 

And it seems I can do this. If a was just a T instead of an array T[], we can get a and cast it without a problem. Why do arrays break this?

Thanks.

  • 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. 2026-05-10T22:19:37+00:00Added an answer on May 10, 2026 at 10:19 pm
    Foo<Double> f = new Foo<Double>(); 

    When you use this version of the generic class Foo, then for the member variable a, the compiler is essentially taking this line:

    private T[] a = (T[]) new Object[5]; 

    and replacing T with Double to get this:

    private Double[] a = (Double[]) new Object[5]; 

    You cannot cast from Object to Double, hence the ClassCastException.

    Update and Clarification: Actually, after running some test code, the ClassCastException is more subtle than this. For example, this main method will work fine without any exception:

    public static void main(String[] args) {     Foo<Double> f = new Foo<Double>();     System.out.println(f.getA()); } 

    The problem occurs when you attempt to assign f.getA() to a reference of type Double[]:

    public static void main(String[] args) {     Foo<Double> f = new Foo<Double>();     Double[] a2 = f.getA(); // throws ClassCastException     System.out.println(a2); } 

    This is because the type-information about the member variable a is erased at runtime. Generics only provide type-safety at compile-time (I was somehow ignoring this in my initial post). So the problem is not

    private T[] a = (T[]) new Object[5]; 

    because at run-time this code is really

    private Object[] a = new Object[5]; 

    The problem occurs when the result of method getA(), which at runtime actually returns an Object[], is assigned to a reference of type Double[] – this statement throws the ClassCastException because Object cannot be cast to Double.

    Update 2: to answer your final question ‘why do arrays break this?’ The answer is because the language specification does not support generic array creation. See this forum post for more – in order to be backwards compatible, nothing is known about the type of T at runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 104k
  • Answers 104k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer try list-style-position: li { list-style-position: outside; } As ricebowl remembered… May 11, 2026 at 8:31 pm
  • Editorial Team
    Editorial Team added an answer I found my error. I had changed the value of… May 11, 2026 at 8:31 pm
  • Editorial Team
    Editorial Team added an answer The best way I know to do this is to… May 11, 2026 at 8:31 pm

Related Questions

I think there must be something subtle going on here that I don't know
I think it can be done by applying the transformation matrix of the scenegraph
I've seen different questions on SO about not being able to use parameterless constructors
I want to build an SQL string to do database manipulation (updates, deletes, inserts,
My winforms app isn't shutting down nicely when I log off/shutdown. I have a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.