java - dealing with a variable of short data type -
i have method in parameter receiving "rowno" of type short. code shown below.
private int abcd (int , short rownum) { // method body }
now query this. inside method have perform logic when value of rownum 5 , trying this
if (rowno == 5) { //perform operation }
please advise me whether correct approach short type.
that should work fine.
when rowno
short
, expression rowno == 5
equivalent ((int) rowno) == 5
since java implicitly promotes primitive operands.
the java language specification says
5.6.2. binary numeric promotion
when operator applies binary numeric promotion pair of operands, each of must denote value convertible numeric type, following rules apply, in order:
2 widening primitive conversion (§5.1.2) applied convert either or both operands specified following rules:
- if either operand of type double, other converted double.
- otherwise, if either operand of type float, other converted float.
- otherwise, if either operand of type long, other converted long.
- otherwise, both operands converted type int.
binary numeric promotion performed on operands of operators:
...
- the numerical equality operators
==
,!=
and kind of conversion happens here safe according to
5.1.2. widening primitive conversion
19 specific conversions on primitive types called widening primitive conversions:
- ...
- short int, long, float, or double
- ...
a widening primitive conversion integral type integral type, or float double in strictfp expression (§15.4), not lose information @ all; numeric value preserved exactly.
Comments
Post a Comment