Link to home
Start Free TrialLog in
Avatar of Eindoofus
Eindoofus

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of a_b
a_b

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of colr__
colr__

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).