• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mixed Mathematical Expression Error  RSS feed

 
Bill Wakayama
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand how mixing expressions of different data types can result in an error if the assigned variable is not the same data type. But I don't understand how the below causes an error:

short totalPay, basePay = 500, bonus = 1000;
totalPay = basePay + bonus; // This causes the error

500 + 1000 = 1500. 1500 falls within the short parameters. If basePay, bonus, and totalPay are all short, as well as the resulting equation, how is this erroring?
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

It is fundamental to Java that in the addition of byte, char and short they are first promoted to int so that a+b results in an int which cannot be guaranteed to fit into short.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

To a human that may be obvious, but to a compiler, much less so. By the time it gets to "basePay + bonus" it has forgotten all about the actual values. Note that "totalPay = 500 + 1000" compiles fine because the actual values are right there.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use shorts for arithmetic; use ints. Unless you mark the variables final, the compiler has no way of “knowing” that there has been no change in the values. You might therefore (as far as the compiler can tell) have the sort of values which will produce overflow when you try to fit them into a short.
Actually, if you look in the Java Language Specification, you find that the shorts are expanded to ints whenever you do arithmetic on them, so they will implicitly not fit into the short datatype, so you will usually have to use an explicit cast. About the only sort of arithmetic you can do with shorts uses the ++/-- operators or compound assignment operators. You can get weird results with compound assignment (e.g. += *=), so it is probably a good idea only to use such compound assignment when you have the same datatype on both sides of it and they occupy not less than 32 bits.
 
Bill Wakayama
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, what an overwhelming response in such a short time!

Thank you for the very warm welcomes. This really is a friendly place. All those responses and not a single troll! What's wrong with this place?

Now I completely understand why it was erroring. Understand the conversion to INT. It all make sense. I'll try to stay away from using char / short but it was a problem in my book and I don't have the answer key.

I'm just beginning this Java quest and can already tell I'll be back here frequently.

Bookmarking the site and thanks again!
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Wakayama wrote: . . . Thank you . . .
it was a problem in my book . . .
You're welcome
Which book? Was it a “why won't this code compile?” sort of question>
 
Bill Wakayama
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Book is "Starting Out With Java - From Control Structures through Objects" Tony Gaddis. It's a school book, covering the first of two java classes.

Yes, that was exactly the question. Why it won't compile and how to fix it.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!