[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

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?
1 Solution
Pass it in as a String rather than a character - s.push(new Double(""+postfix.charAt(i)));
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now