4

I meet a method invocation which the parameter type is short, such as

foo (short s){...}

When I call it, I think of the two solution:

one is:

foo((short) 1);

and another:

short s = 1;
foo(s);

What's the difference between them, and which is better?

TryinHard
  • 4,078
  • 3
  • 28
  • 54
zhouji
  • 5,056
  • 1
  • 26
  • 26
  • 1
    I think the method `foo()` will receive the same input in both cases. I personally prefer the second non-casted version because it is easier to read. May we know why you have this question? – Tim Biegeleisen Jul 27 '15 at 05:24
  • 4
    Both forms are equivalent (and *in general* you should not use `short`, see http://stackoverflow.com/questions/1615419/what-is-the-reason-for-these-pmd-rules for example) –  Jul 27 '15 at 05:26
  • @RC. Thanks. Didn't know that info. – hmc_jake Jul 27 '15 at 05:33
  • I want some more information deep in to the difference , such as the jvm level, so I'm still hesitating accept the answer. – zhouji Jul 29 '15 at 01:39

4 Answers4

1

1 will be by default treated as an int in Java.

So, when you're doing ((short)1), the you're passing 1 as a short parameter through the function. The receiving argument should be of a short type.

Whereas, when you have short s =1, then it is obviously a short integer.

Remember that in both the case, shorts will be converted to int(this is called type promotion) while performing operations. And, if operated with double operands, those int's will get promoted to double.

What's the difference between them, and which is better?

Both are doing the same operations finally(passing a short variable as an argument), and both of them are of equally better. But, you should prevent using short integers, unless extremely required as it has some shortcomings(causes compile-time errors when you accidentally try to store an int/long to a short).

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • I understand it is called a type promotion, but why would it be promoted if the method takes in a short? – hmc_jake Jul 27 '15 at 05:27
  • @hmc_jake-because, when operations are performed in this case, the type promotion takes place and all short/byte/char are promoted to int. – Am_I_Helpful Jul 27 '15 at 05:28
  • @hmc_jake-Without any operation, it'll be obviously stored as a short,but,just,when any operation is performed, it'll be converted to an int. This is what I mentioned in my answer, **EMPHASIS MINE** --- `...which will be converted to int(this is called type promotion) while performing operations`. – Am_I_Helpful Jul 27 '15 at 05:30
  • Right. I didn't know that all variables of type `short` are promoted to `int`. Learn something new every day! – hmc_jake Jul 27 '15 at 05:31
  • while performing operations the result will be in highest type ie operation with variable in short , byte , double gave result in double . @shekhar suman – sujith s Jul 27 '15 at 05:37
  • @sujiths-Well, I have used only Integer terms, a short will be converted to an int when being operated over Integer types. Finally, at the last stage, all the ints will be converted to doubles,directly shorts aren't converted to doubles. It's a step by step hierarchy. – Am_I_Helpful Jul 27 '15 at 05:39
  • 1
    You make it sound as if what you are describing in your second paragraph won't happen with `foo((short)1)`. The type promotion is irrelevant here. – Sotirios Delimanolis Jul 27 '15 at 05:50
  • @SotiriosDelimanolis-Yeah, I agree. It seems I forget to make the last line as the 3rd paragraph to remove all confusions. Improving it... Thanks. – Am_I_Helpful Jul 27 '15 at 05:51
  • I'm still not satisfied with this answer. The only difference in this example is the extra variable for storage. Both code snippets will pass a `short` value as an argument. It's also unclear what you mean by operations. Do you mean when a `short` value is used with any mathematical or binary operators? – Sotirios Delimanolis Jul 27 '15 at 05:56
  • @SotiriosDelimanolis-Well,it's your problem that you aren't understanding the latter case. if you perform any operations with this variable being one of the operand, then this operated will be automatically promoted to `int`. i didn't see the last part of the question,making necessary edit. – Am_I_Helpful Jul 27 '15 at 05:59
  • @SotiriosDelimanolis-Improved, please remove your -1 now OR ask for further clarification. – Am_I_Helpful Jul 27 '15 at 06:04
1

short s = 1 is defining a variable as a short with a value of 1. (short) 1 is casting an int value of 1 to the primitive datatype short.

There is no difference. They both create a short with a value of 1.

See Java: Primitive Data Types for information about a short and its value range, though, as setting a short to a value that is above or below its prescribed range will throw an exception.

hmc_jake
  • 372
  • 1
  • 17
1

Both solutions will work, and performance differences between the two will be hardly noticable.

(short) 1; uses a literal, which gets casted to a short that is passed in as a parameter for the function foo.

short s = 1; actually creates a variable in memory prior to passing the value as a parameter, in case you want to also use the variable for other things.

Daniel Kao
  • 11
  • 2
1

In java up cast from short to int automatically happens. But for down casting you have to manually cast it.

short(integer_value)
(short) 1

If you call using casting you can send integer parameter too.

Bruce
  • 8,609
  • 8
  • 54
  • 83