What is the difference between 'short s = 1' and '(short) 1' in Java?

I come across a method call that has a parameter type short, e.g.

foo (short s){...}

When I call it, I think of two solutions:

one:

foo((short) 1);

and the other:

short s = 1;
foo(s);

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

+4
source share
4 answers

1 will by default be considered as int in Java.

So, when you do ((short) 1), you pass 1 as a short parameter through a function. Reception argument should be short.

Whereas when you have short s = 1, then this is obviously an integer short.

, int ( ) . , , int .

, ?

( ), . short, , ( , int/long ).

+1

short s = 1 short 1. (short) 1 int 1 short.

. a short 1.

. Java: Primitive Data Types , , short, , .

+1

, .

(short) 1; , , foo.

short s = 1; , .

+1

java up cast short int . down casting .

short(integer_value)
(short) 1

, .

+1
source

Source: https://habr.com/ru/post/1599799/


All Articles