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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:22:20+00:00 2026-06-05T10:22:20+00:00

In Java, how expensive is to do a cast: (MyObject) IObject; versus: i++; assuming

  • 0

In Java, how expensive is to do a cast:

(MyObject) IObject;

versus:

i++;

assuming “int i = N” preceding it.

EDIT:

Disregard my direct comparison with (i++) for a second, if you would please. Let me rephrase this in more general terms: how expensive is casting in general? Choose your reference operation better than my naive “i++” example, take a simple variable declaration and assignment for instance, how does performing a cast compare to that in terms of VM bytecode/operations?

I used i++ as a measuring stick. That was a very bad idea. When they chose “meter” as a baseline they did much better. Is there a base measuring unit for operation cost in Java? A JVM op? I suppose this will depend to the native code the Java op compiles down to… anwyay, how expensive is casting? (you pick the measuring stick for me, I am bad at that)

  • 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-05T10:22:22+00:00Added an answer on June 5, 2026 at 10:22 am

    I will do a short answer on the 2nd part (how expensive is casting):

    casting is very cheap and heavily optimized where it counts.
    if (o instanceof Clazz) ((Clazz)o) is absolutely free (the cast, not the check itself).

    Overall if the cast can be proven by the compiler and constant folded it would cost nothing. Otherwise a load in the header of the object to determine the class is needed. Some JVM can use some bit of the pointer to store the class, it’s how important it is. Anyways, even a load’s likely to be cheap and hit the L1 cache. Virtually all casts branches are predicted correctly by the hardware – imagine how unlikely is getting ClassCastException (the slow path – and it doesn’t matter optimization wise in such a case).

    Arrays are generally significantly faster than collections framerwork and its generics for many a reason but casting plays a very small part, itself.

    Last: blatant ‘measure’, i.e. microbenchmark is often an appalling idea, measuring the application performance as whole is the right way to go. Microbenchmarks require deep understanding how JVM optimizes (or doesn’t) and you have to be sure you benchmark what you actually need. Overall it’s an art on its own.

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

Sidebar

Related Questions

Just a quick question what would be more expensive in Java? double test =
I am really new to Java and I read that synchronized is very expensive
Java Code : package Package; public class IntArray { private native int sumArray(int[] arr);
I have created a small game in Java and I would like to add
I'm puzzled by the return values on Java's Lists. I would have expected operations
In general, how expensive is locking in Java? Specifically in my case: I have
Greetings. I have a java method that I consider expensive, and I'm trying to
The Java tutorials say that creating a Thread is expensive. But why exactly is
I was wondering how expensive Java's string encoding conversion algorithms are, say, for a
How expensive is calling size() on List or Map in Java? or it is

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.