Jump to content
  • Advertisement
Sign in to follow this  
Sagar_Indurkhya

Java Double not working right

This topic is 4970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have:
public class Calculator
{
    public static void main(String args[])
    {

        Double c = null;
        test(c);

        System.out.println(c);
    }

    public static void test(Double b)
    {
        b = new Double("5");
        int i = 5;
    }
}

now the output is: null this is because apparently, java is not treating Double as a reference! Double is derived from Objects, so shouldn't it be treated as a reference?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
you should set Test to return something, or else it will always return null or whatever you assign it before passing it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Objects are passed in java by reference, by value. Meaning it passes a copy of the reference. You can modify whatever the object passed references, but if it pointed to nothing in the first place, pointing it at something in the test method is only going to point the copy of the reference at it and your original object is still going to be null. This is an example of a dangling pointer(at least it would be in c/c++, but same idea).

Share this post


Link to post
Share on other sites
Yep. The solutions for "out parameters" in Java are basically:

1) wrap the parameter in an object, and set the data member.
2) wrap the parameter in an array of size 1, and set the 0th element. (Arrays are objects, more or less anyway.) Or if there are several "out parameters" of the same type, pass them in a single array. This may well be good design; there could be a good reason to group those parameters in a particular order throughout the system.

The behaviour is actually quite nice and consistent, and IMHO coheres well with the Java object model; I feel rather like this is somewhere that the C# language developer(s) don't Get It. The Java behaviour upholds the Principle of Least Astonishment; if you pass something mutable to a function, it may be changed in some way, but you will get the same thing back.

It wouldn't make as much sense in C++ where there is less emphasis on "everything is an object" and more emphasis on being able to copy and destruct objects properly, do RAII etc., such that the C++ reference system makes more sense for C++, but would seem out of place in Java. (I would say it *does* seem out of place in C#, but I can't fairly assert that, since, well, I haven't used C#.)

OTOH, it's sometimes aggravating not to have something equivalent to the "copy constructor/assignment operator" concept.

OTGH, because of the garbage collection, Java's design inherently encourages aliasing objects and NOT copying them, whenever possible - this actually allows for some optimizations that people wouldn't bother with in C++ because the memory management would get too tricky.

(E.g. an object has a data member which is of a polymorphic, but immutable type; you don't really need to clone it in a C++ copy constructor, but if you don't, then you have a nasty object ownership problem on your hands and will probably have to resort to smart pointers.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!