0

I'm having an issue with trying to pass a double constant as a parameter into a hashmap.

public static final double Apple_Initial_Value = 30.0;
private static HashMap<String, Double> base_values = new HashMap<String, Double>() {{

    put("Apple", Apple_Initial_Value);
}};
System.out.println(base_values.get("Apple"));

Results in the following output:

0.0

However,

public static final double Apple_Initial_Value = 30.0;
private static HashMap<String, Double> base_values = new HashMap<String, Double>() {{

    put("Apple", 30.0);
}};
System.out.println(base_values.get("Apple"));

Results in the following output:

30.0

Having just finished a course in C, my mind jumped to it being some sort of pass by reference vs. pass by value issue, but as I understand it, java is only pass by value, so I'm at a loss.

Cheers, and thanks in advance!

Edit: My apologies, it looks like the code I posted above is not actually indicative of what is exactly happening in my code (as it's actually working!). To be more specific, the situation is broken into two classes, IR.java, and Main.java. The relevant parts of IR.java look like this:

public class IR {

    public static final double Apple_Initial_Value = 30;
    public static HashMap<String, Double> base_values = new HashMap<String, Double>() {{

        put("Apple", Apple_Initial_Value);

    }};

    public static double get(String item) {
        double value = (double) base_values.get(item);
        return value;
    }
}

And the relevant parts of Main.java look like this:

public class Main {

    public static void main(String[] args) {

        System.out.println(IR.get("Apple"));
    }
}
Overdogg
  • 9
  • 3
  • 3
    I get [30.0](http://ideone.com/x1QNpF) – SomeDude Jul 06 '16 at 02:31
  • If you delete the static identifier of the variable it's still happening? – Ami Hollander Jul 06 '16 at 02:38
  • Turns out the code fragment I posted above originally is fully functioning code! Whoops! I've edited the original post with the code fragments necessary to indicate that something isn't working properly. – Overdogg Jul 06 '16 at 02:52
  • You are using wrong constants. `Apple_Pie_Initial_Value` and `Apple_Initial_Value` – Vishal Vijayan Jul 06 '16 at 02:55
  • 2
    Good idea to write a [MCVE] first that you can check reproduces the behaviour you are asking about and that others can run. Avoids the typo problems. – pvg Jul 06 '16 at 02:57
  • Sorry, the apple_pie vs apple was a typo. The code is still however not producing the expected answers. And @VishalVijayan, you're right and I'll be sure to be more cautious next time I post a question. – Overdogg Jul 06 '16 at 03:00
  • Btw @Overdogg Java is pass by reference, not by value. – Samuel Toh Jul 06 '16 at 03:04
  • @SamuelToh Well, maybe depending on how you interpret the word "reference"; however, check out this: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Overdogg Jul 06 '16 at 03:07
  • @Overdogg still this code is giving no error for me its printing the given value for constant. – Vishal Vijayan Jul 06 '16 at 03:12
  • I would think that it is order of operation. It is unclear that Apple_Initial_Value has to be initialized before base_values. Try disassembling to determine the order. – iceraj Jul 06 '16 at 03:15
  • Ahah! I've found it. I miswrote public static final double Apple_Initial_Value = 30 as public static Apple_Initial_Value = 30. Completely omitting the "final". I'm sorry for leading you on such a wild goose chase, and immensely appreciate the effort and time you spent to help me look into this. That said, why does the value of "Apple" get set to zero if the variable being inputted is not a constant, but a mutable variable? @SamuelToh – Overdogg Jul 06 '16 at 03:17
  • @Overdogg thanks for the article hmm yep it really boils down to how u see it... Interesting article. Thanks for it I'll have a good read. – Samuel Toh Jul 06 '16 at 03:17

2 Answers2

0

The code above after the edit is completely valid, as I accidentally made it work by adding a typo that was not originally present in my source code when I posted the question (figure that). My source code declared Apple_Initial_Value as such:

public static Apple_Initial_Value = 50;

but the working code instead declares it as:

public static final Apple_Initial_Value = 50;

Sorry for leading you all on a wild goose chase, and thanks immensely for your time!

To make an example of myself, I advise any new posters to please read https://stackoverflow.com/help/mcve to learn how to make Minimal, Complete and Verifiable examples before posting questions that contain code.

Community
  • 1
  • 1
Overdogg
  • 9
  • 3
-1

You can try like this:

public static void main(String[] args) {
    final double Apple_Initial_Value = 30.0;
    HashMap<String, Double> base_values = new HashMap<String, Double>() {{

        put("Apple", Apple_Initial_Value);
    }};
    System.out.println(base_values.get("Apple"));

}

Update for your new code: The variable name is incorect. Just simple change it in the put statement:

put("Apple", Apple_Pie_Initial_Value);
Tan Mai Van
  • 657
  • 6
  • 8