1

I really find the concept of conversion to be confusing.

short a = 34;
short b = (short)34;

What is the linguistic difference between these two statements? I think that, in the first statement the int literal 34 is directly stored in the short variable a, causing a conversion of int literal 34 to short literal 34. And in the second one, it seems that the int literal 34 is first converted to the short literal 34 due to the casting instruction and then stored in the short variable b. Am i correct or its one of the two cases?

user12208242
  • 315
  • 1
  • 11
  • Those two are equivalent. The type conversion is done automatically for you in the first line and therefore writing the second line is not useful. That syntax (called casting) is useful in other situations, where trying to store an int inside a short may fail to keep the same value as the int was because it was outside the range of short values. Like `int a = 34; short b = (short)a;` The compiler is not supposed to be aware that a will be of value 34, all it knows is you're putting an unknown int value inside a short and that could be a mistake, so you need a cast to enforce your choice. – kumesana Oct 30 '19 at 10:53
  • @kumesana thanks but still not clear about my query... – user12208242 Oct 30 '19 at 11:14
  • Well it would be smart to elaborate, because your query is what's the functional difference between the two statements and I explained the first implicitly is the same as the second, therefore no difference. – kumesana Oct 30 '19 at 11:15
  • @kumesana ok I'll try to be as simple as possible. Considering only the first statement, does Java directly store the int literal to a short variable or Java first converts it to the short literal and then stores in the short variable? – user12208242 Oct 30 '19 at 11:19
  • I think you don't understand what is a literal. A literal cannot be anything but something you write inside your source code. Compilers don't write source code and therefore nothing they do produces literals. A literal is a value, that you write literally. Like 32 is literally value 32. While a + b might be value 32 if it so happens that adding the values of variables a and b gives out 32. 32 is a literal and a + b isn't, despite both being the same values in cases where a + b gives value 32. Anyway, in the first case, Java first converts the int value to a short value then stores it. – kumesana Oct 30 '19 at 11:24

1 Answers1

1

What is the functional difference between these two statements?

There is no functional difference in this example. In this example, the end result is the same.

From a (Java) linguistic perspective, these are two distinct cases:

short b = (short) 34;

The number 34 is an int literal. The (short) is a type cast that performs an explicit narrowing primitive conversion. This will truncate the value if necessary, though it is not necessary here. You then have an short value that is assigned to the variable with no further conversion.

short b = 34;

The number 34 is an int literal. In this case there is an implicit narrowing primitive conversion. This "special case" happens in an assignment context when:

  1. the expression being evaluated is a compile time constant expression, AND
  2. the type of the variable is byte, char or short, AND
  3. the value of the expression is representable in the type of the variable.

This will NOT truncate the value.


If you change the code to the following, then the difference between the two contexts becomes apparent:

short a = 100000;          // Compilation error because 100,000 is too large
short b = (short) 100000;  // OK - 100,000 is truncated to give -31,072

Alternatively:

int x = 34;
short y = x;               // Compilation error because 'x' is NOT
                           // a constant expression

The most relevant sections of the JLS are 5.2 Assignment Contexts and 5.4 Casting Contexts.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216