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

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.
主站蜘蛛池模板: 宜黄县| 育儿| 普陀区| 麟游县| 抚州市| 专栏| 洛南县| 霍州市| 蚌埠市| 青海省| 中山市| 康平县| 大余县| 德格县| 大余县| 顺义区| 曲麻莱县| 高尔夫| 凤城市| 闽清县| 德州市| 阿拉尔市| 东安县| 三亚市| 廉江市| 尖扎县| 德惠市| 翁牛特旗| 垫江县| 浪卡子县| 广宁县| 修武县| 敦煌市| 泾源县| 三河市| 石台县| 南丰县| 湘潭县| 安陆市| 睢宁县| 什邡市|