java - Reason for casting a static final short to a short? -


i came across didn't understand earlier , wondering if may able shed light on it.

there class file many static final variables, they're case same type in declaration , wonder why.

so example have:

public static final short a_const_value = (short)12; 

thanks.

there no problem in line public static final short a_const_value = (short)12; or public static final short a_const_value = 12; if literal specifying within range of short -32,768 32,767. if line public static final short a_const_value = 32768; , compile error "possible loss of precision found: int, required : short" instead if write public static final short a_const_value = (short)32768; value of a_const_value type casted short , it's value -32768 ie cycle back.

so long literal within range of short, don't need typecast.also literal assigning a_const_value not datatype numeric value , should lie within range -32,768 32,767 because a_const_value short dataype. nuts n bolts of primitive datatype in java


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -