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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:48:58+00:00 2026-06-15T18:48:58+00:00

I have an Object obj that I know is actually a long . In

  • 0

I have an Object obj that I know is actually a long.
In some Math code I need it as double.

Is it safe to directly cast it to double?

double x = (double)obj;

Or should I rather cast it first to long and then to double.

double x = (double)(long)obj;

I also found another (less readable) alternative:

double x = new Long((long)obj).doubleValue();

What are the dangers/implications of doing either?

Solution Summary:

  • obj is a Number and not a long.
  • Java 6 requires explicit casting, e.g.: double x = ((Number)obj).doubleValue()
  • Java 7 has working cast magic: double x = (long)obj

For more details on the Java6/7 issue also read discussion of TJ’s answer.

Edit: I did some quick tests. Both ways of casting (explicit/magic) have the same performance.

  • 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-15T18:48:59+00:00Added an answer on June 15, 2026 at 6:48 pm

    As every primitive number in Java gets cast to its boxing type when an object is needed (in our case Long) and every boxed number is an instance of Number the safest way for doing so is:

    final Object object = 0xdeadbeefL;
    final double d = ((Number)object).doubleValue();
    

    The danger here is, as always, that the Object we want to cast is not of type Number in which case you will get a ClassCastException. You may check the type of the object like

    if(object instanceof Number) ...
    

    if you like to prevent class cast exceptions and instead supply a default value like 0.0. Also silently failing methods are not always a good idea.

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

Sidebar

Related Questions

I have an object obj that is passed into a helper method. public static
I have a byte[] that i obtained using Object ArrayList<Obj> Can anyone tell me
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
suppose I have a object: obj:{ child:{ x:12, y:50 }, key1:value1, key2:value2 } if
I have a couple functions like this: object obj.getChild(childIndex) int obj.numChildren() So I am
public void test(Object obj){ //Here i have to set the values of the obj
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have this interface: public interface IValidationCRUD { public ICRUDValidation IsValid(object obj); private void
I have a Java function called testForNull public static void testForNull(Object obj) { if
i have a method as follows. test(Object obj){ } now within this method i

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.