0% found this document useful (0 votes)
10 views5 pages

Missing Topics in Textbook Java

java

Uploaded by

n3004dsouza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Missing Topics in Textbook Java

java

Uploaded by

n3004dsouza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Chapter’3 — DataTypes,Varlables, and Arrays 63 ren hae ths avoiding the ned to explly specify the rence offers a number of advantages. For example it can ting the need to redundantly specify a variables type when ican : I can simplify declarations in cases in which the ype name i quite lengthy, such as can be the is case with some class names, Itcan sso ‘ype is dificult to discern or cannot be denoted A Jecan also be helpful when a ted (An example of type that cannot be denoted the type ofan anonymous clas, dscunedn Chaper 25) Farnese varie tye inference as become common pata the contempoey posing eae ae lusion Bele ‘helps keep Java up-to-date with ‘evolving trends in age design. To support local variable type inference, the conter-sorate tone igus °° the context-sestive keyword ‘To use local variable type inference, the variable must be declared with var as the type ‘name and it must include an initializer. For example, inthe past you would declare a local double variable called avg that is initialized withthe value 100, es shown here: double ‘avg “20.0; Using type inference, this declaration can now also be writen like this var avg = 10.0; Inboth cases, avg will be of type double. Inthe first case its type is explicitly specified. In the second, its type is inferred as double because the initializer 100 is of type double. ‘As mentioned, var is context sensitive. When it is used as the type name in the context ‘ofa local variable declaration, i tells the compiler to use type inference to determine the type ‘on the type ofthe initializer. Thus, in alocal variable actual, inferred type. However, when used in most ntifier with no special meaning, For example, the 4s einply & user-defined sdentitier [as int and vars the name of the variable being there area few places in which the use of vars ig discussion into action: wariable type inference arse) ( 1e the type of the eMjouble i= inferred eaves defined identifier As not = pres Sabie name: PART! The Java Language 4 Interestingly, in the following sequence, var is used, J/ as both the type of the declaration and as a variable nane // An the initializer, var k= var; System.out.printin(*Value of ki" +k); ) 1 Here is the output: Value of avg: 100 Value of var: 2. Value of k: - ‘The preceding example uses var to declare only simple variables, but you can also ue ‘var to declare an array. For example: var myArray = new int (201; // Thie ie valid ‘Notice that neither var nor myAzray has brackets. Instead, the type of myArray i infeed to be int{ |, Furthermore, you cannot use brackets on the left side of a var declaration. Ths both of these declarations ae invalid: Sinew ine (10); // wrong ine (001; // weong is made to bracket va. In the second, am attempts made to cases, the use of the brackets is wrong because the type hasize that var can be used to declare a variable only when hat, example the following statement is incorrect: | initializer required be used only'to declare local variables. It cannot be used wb parameters, or return types, for example tt discussion and examples have introduced the bescs of 104 havent shown its full power. As yu will se in Chaps especialy effective in shortening declarations that involve OE ‘used with generic types (see Chapter 14), in try-with 3), and witha for loop (see Chapter 5). ons 0 receding discussion, several other restrictions 37° ‘declared ata time; a variable cannot use ‘eannot be used by the initializer expres°™ Chapter3 Data Types, Variables, and Arrays 65 use var with an array initializer. lass. It also cannot be used as the eration, or annotation, or as the later in this book. Here are two ibsequent chapters but mentioned rence cannot be used to declare lambda expressions nor Local Variable Type Inference in a for Loop Asexplained in Chapter 3, JDK 10 introduced a feature called local variable type inference, ‘which allows the type ofa local variable to be inferred from the type of its initialize. To use Toca variable type inference the ype ofthe variable i speciied as var and the variable mag be initialized, Local variable type inference can be used in a for loop when declaring and initializing the loop control variable insicea traditional for loop, or when specifying the iteration variable in a for-each for, The following program shows an example of each case J/ use type inference in a for loop. class Typetnferencernror { public static void main(string!l args) { // Vee type inference with the loop control variable. system.out print ("Values of x: ")7 for (var x = 2.5; x © 100.0; x = x * 2) System.out print (x + system cut.printin() ; {/ tse type inference with the iteration variable. inet) numa = (1, 2,3, 6, 5, 6 1.0 40.0 99.0 a6. le xin this line: wx +2) «that i the type of its initializer. Iteration variable vis ‘ype of the array nums. be working in environments that beused by most of the for loops i lof course, consider it for new cod®

You might also like