Why is this character being pushed into the stack as it's decimal value?

Posted on 2011-04-26
Last Modified: 2012-08-13
I'm attempting to push a character into:
Stack<Double> s = new Stack();

By using the following line:

s.push(new Double(postfix.charAt(i)));

However, when I push the character '7' for example and try to pop it using:

double a = ((Double) s.pop()).doubleValue();

I get the decimal's character value 55 rather than the number 7. What am I doing wrong? What code will fix this?
Question by:Eindoofus
    LVL 20

    Accepted Solution

    Pass it in as a String rather than a character - s.push(new Double(""+postfix.charAt(i)));
    LVL 8

    Expert Comment

    You are getting 55 as that is the character code (see an ascii table) for the character you are supplying to the Double constructor. Although char represents a character, it is also a type of number, or bit pattern. In this case the char you supply is '7' but it's numeric value is actually 55.

    Since Double doesnt accept a char in its constuctor, it implicitly casts the char to double, in doing so working with the numeric value (55).

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now