Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
The expression
127
is not of type
short
- rather it is of type
int
.
If you want to have an expression of type
short
, use
(short) 127
, as in:
Overloaded o = new Overloaded((short)127);
Then your example will work.
–
All numeric constants are by default of integer type, and when you want to save integer to byte/short field you need to do a narrowing conversion using cast:
(byte)127
(short)127
Or in your case:
Overloaded o = new Overloaded((byte)127);
Or if you want to short:
Overloaded o = new Overloaded((short)127);
The above code wont produce an error.
It would rather show an output- con 3 is called.
Short type can be called in a constructor.
Typecast it to short
Overloaded o = new Overloaded((short)127);
–