0

I am trying to remember what happens when various mathematical operations are applied to Java Primitives e.g. int + int = int

But would would happen in this case:

float/short=?

Is there any easy way to remember what happens when any operation is applied to any of the Java primitives? Or is it just something that I will need to commit to memory?

--EDIT--

The float/short was an example of the kind of thing im looking for. Im wondering if there is a pattern on the answers when any operation is applied to any amount of primitives e.g.

double*double=?
short/byte+long=?
etc.....
Srb1313711
  • 2,017
  • 5
  • 24
  • 35
  • Im not sure this is a duplicate? Im not looking for a specific operation or primitive I am looking for an answer for all operations on all primitive combinations – Srb1313711 Jun 27 '16 at 15:02
  • 1
    `short` will be promoted to `int` in math operations, the result is `float` according to widening conversions (`int` -> `float`) – Andrew Tobilko Jun 27 '16 at 15:02
  • The general rule is the one of binary numeric promotion, as answered in the linked question: when doing operations on primitive types: if one of them is `double` (resp. `float`, resp. `long`), the other is converted to `double` (resp. `float`, resp. `long`); otherwise they are all converted to `int`. So the `short` in `float / short` is widened to a `float`. See also http://stackoverflow.com/a/1660864/1743880 – Tunaki Jun 27 '16 at 15:04
  • If it is a duplicate, include the reference to the original. – Grayson Jun 27 '16 at 15:04
  • Im not sure its duplicate but there is a reference at the top of the question – Srb1313711 Jun 27 '16 at 15:05
  • Please see edit and consider reopening question – Srb1313711 Jun 27 '16 at 15:06
  • @Srb1313711 The general rule for all primitive types, which is your question, is mentioned the linked question. It is also mentioned here http://stackoverflow.com/a/1660864/1743880. – Tunaki Jun 27 '16 at 15:08
  • Ok thanks, might be worth making the link in your comment the result of the duplicate instead of the current one – Srb1313711 Jun 27 '16 at 15:14
  • The links of “duplicate” messages always point to questions. So in either case, you have to scroll down to the first answer to find out that it explains the general rule. – Holger Jun 28 '16 at 14:06

0 Answers0