官术网_书友最值得收藏!

Using var with primitive data types

Using var with primitive data types seems to be the simplest scenario, but appearances can be deceptive. Try to execute the following code:

var counter = 9_009_998_992_887;       // code doesn't compile 

You might assume that an integer literal value (9_009_998_992_887, in this case) that doesn't fit into the range of primitive int types will be inferred to be a long type. However, this doesn't happen. Since the default type of an integer literal value is int, you'll have to append the preceding value with the suffix L or l, as follows:

var counter = 9_009_998_992_887L;       // code compiles 

Similarly, for an int literal value to be inferred as a char type, you must use an explicit cast, as follows:

var aChar = (char)91; 

What is the result when you divide 5 by 2? Did you think it's 2.5? This isn't how it (always) works in Java! When integer values are used as operands in the division, the result is not a decimal number, but an integer value. The fraction part is dropped, to get the result as an integer. Though this is normal, it might seem weird when you expect the compiler to infer the type of your variable. The following is an example of this:

// type of result inferred as int; 'result' stores 2 
var divResult = 5/2;

// result of (5/2), that is 2 casted to a double; divResult stores 2.0
var divResult = (double)(5/ 2);

// operation of a double and int results in a double; divResult stores
// 2.5
var divResult = (double)5/ 2;

Though these cases aren't specifically related to the var type, the developer's assumption that the compiler will infer a specific type results in a mismatch. Here's a quick diagram to help you remember this:

The default type of integer literals is int, and the default type of floating point numbers is double. Assigning 100 to a variable defined with var will infer its type as int, not byte or short.

In arithmetic operation, if either of the operands is char, byte, short, or int, the result is at least promoted to int:

byte b1 = 10; 
char c1 = 9; 
var sum = b1 + c1;        // inferred type of sum is int  

Similarly, for an arithmetic operation that includes at least one operand as a long, float, or double value, the result is promoted to the type long, float, or double, respectively:

byte cupsOfCoffee = 10; 
long population = 10L; 
float weight = 79.8f; 
double distance = 198654.77; 
 
var total1 = cupsOfCoffee + population;     // inferred type of total1 
// is long var total2 = distance + population; // inferred type of total2
// is double var total3 = weight + population; // inferred type of total3 is
// float
The rules of the implicit widening of primitive variables play an important role in understanding how the Java compiler infers variables with primitive values.
主站蜘蛛池模板: 陈巴尔虎旗| 马边| 会宁县| 罗源县| 额尔古纳市| 武山县| 友谊县| 衡南县| 丽水市| 浠水县| 溧阳市| 沁阳市| 安多县| 湘西| 龙南县| 喀什市| 全椒县| 岐山县| 平湖市| 贺兰县| 广东省| 富源县| 大英县| 庆阳市| 固原市| 长汀县| 广宁县| 神农架林区| 临漳县| 固安县| 从化市| 保康县| 庆阳市| 泰兴市| 夹江县| 嵊州市| 稷山县| 岳阳县| 巴彦淖尔市| 宜宾县| 东乡|