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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:06:02+00:00 2026-05-13T14:06:02+00:00

Is there any overhead when we cast objects of one type to another? Or

  • 0

Is there any overhead when we cast objects of one type to another? Or the compiler just resolves everything and there is no cost at run time?

Is this a general things, or there are different cases?

For example, suppose we have an array of Object[], where each element might have a different type. But we always know for sure that, say, element 0 is a Double, element 1 is a String. (I know this is a wrong design, but let’s just assume I had to do this.)

Is Java’s type information still kept around at run time? Or everything is forgotten after compilation, and if we do (Double)elements[0], we’ll just follow the pointer and interpret those 8 bytes as a double, whatever that is?

I’m very unclear about how types are done in Java. If you have any reccommendation on books or article then thanks, too.

  • 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-05-13T14:06:02+00:00Added an answer on May 13, 2026 at 2:06 pm

    There are 2 types of casting:

    Implicit casting, when you cast from a type to a wider type, which is done automatically and there is no overhead:

    String s = "Cast";
    Object o = s; // implicit casting
    

    Explicit casting, when you go from a wider type to a more narrow one. For this case, you must explicitly use casting like that:

    Object o = someObject;
    String s = (String) o; // explicit casting
    

    In this second case, there is overhead in runtime, because the two types must be checked and in case that casting is not feasible, JVM must throw a ClassCastException.

    Taken from JavaWorld: The cost of casting

    Casting is used to convert between
    types — between reference types in
    particular, for the type of casting
    operation in which we’re interested
    here.

    Upcast operations (also called
    widening conversions in the Java
    Language Specification) convert a
    subclass reference to an ancestor
    class reference. This casting
    operation is normally automatic, since
    it’s always safe and can be
    implemented directly by the compiler.

    Downcast operations (also called
    narrowing conversions in the Java
    Language Specification) convert an
    ancestor class reference to a subclass
    reference. This casting operation
    creates execution overhead, since Java
    requires that the cast be checked at
    runtime to make sure that it’s valid.
    If the referenced object is not an
    instance of either the target type for
    the cast or a subclass of that type,
    the attempted cast is not permitted
    and must throw a
    java.lang.ClassCastException.

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

Sidebar

Related Questions

Using C#, is there any overhead in having one larger object containing a whole
Is there any overhead using partial classes in case of memory, performance etc? If
1.. Is there any performance overhead caused by the usage of attributes? Think for
Is there any overhead associated with using lambda expressions in C++0x (under VS2010)? I
Is there any performance overhead in using RaiseEvent in .NET ? I have a
Is there any performance overhead that you take on when using Structs (as compared
I would like to know if there is any overhead incurred through the use
Is there any performance or memory overhead using UIImage or UILabel or NSString in
Is there any difference in overhead between using Panel or HtmlContainerControl when needing to
is there any advantage of using your own type identifier over RTTI? e.g. class

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.