-1

I just can't understand the difference between this:

short d = 0; //some code node.accessible = d + 1;

and this

short d = 0 //same code here node.accessible = d; node.accessible += 1;

the second thing is working, but the 1st one is't inteliji showes "incompatiable types" error.

p.s. node class:

public class Node {
int n;
short accessible;
Node(int n){
    this.n = n;
    this.accessible = -1;
}
}   
ibanezn04
  • 478
  • 5
  • 12
  • 1
    Did you mean to type `+=` instead of `=+`? – yshavit Mar 11 '18 at 20:46
  • Why do you even use a `short` in the first place? In my long Java carrier I rarely found a scenario where using `short` instead of `int` or `boolean` would pay out in a full scale test. There are even cases where `short` is slower than `int` due to internal optimizations of `int`. Also, the potential of bugs due to unwanted overflows (without exception) is often too high compared to a possible micro-perfomance-improvement. Of course, it depends on your application, that's why I ask. – Zabuzard Mar 11 '18 at 20:49
  • 1
    @Zabuza thanks, i'll note this, but now i am just curious. – ibanezn04 Mar 11 '18 at 20:53

3 Answers3

2

In the first version :

node.accessible = d + 1;

d + 1 produces a int as summing an int and a short produces an int.
The JLS states indeed that (look at the last case, emphasis is mine) :

5.6.2. Binary Numeric Promotion

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

  1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

  2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

    • If either operand is of type double, the other is converted to double.

    • Otherwise, if either operand is of type float, the other is converted to float.

    • Otherwise, if either operand is of type long, the other is converted to long.

    • Otherwise, both operands are converted to type int.

But you cannot assign a int to the accessible field that is a short without explicit cast as int has a broader range than short.


While in the second version, a Compound Assignment Operators is used (+=):

node.accessible += 1;

As a consequence, in your case the result of the operation is converted to short : the type of the left-hand variable as the JLS states :

15.26.2. Compound Assignment Operators

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is Evaluations only once.

And more specifically in your case :

Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • The question is why `+=` does an automatic down-cast whereas `d + 1` does not. A JLS reference to this would be nice. – Zabuzard Mar 11 '18 at 20:53
  • @Zabuza finally I cannot delete it (accepted). I think that the question is not only the Compound assignment matter or the result type from `int`+`short` matter but both of them. So addressing both makes more sense. – davidxxx Mar 11 '18 at 21:13
2

That's because 1 in your + 1 expression is of type int. Adding short to int results in int which can't be assigned without a narrowing cast back to node.accessible.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
2

In the second sample,

node.accessible += 1;

is actually

node.accessible = (short)(node.accessible + 1);

so it works without problem. But in the first one node.accessible = d + 1; is actually node.accessible = d + 1; and it doesn't automatically cast as short and thus gives error as (d + 1) is of type int

Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • The question has since been updated, but just fyi... the reason `=+ 1` would work is actually that it's `= +1` -- that is, you're assigning to an int literal of positive 1. But since that literal is also a constant, and the compiler can confirm that it fits in a short, by JLS 5.2 the narrowing conversion (from int to short) succeeds. – yshavit Mar 11 '18 at 20:59
  • 1
    I thought `=+ 1` is a typo which actually is `+= 1` and wrote my answer accordingly. But thanks for it ;) – Onur A. Mar 11 '18 at 21:11