Exploring var with the Java Playground
Your Turn: Exploring var with the Java Playground
- Run each example in the Java Playground.
- Record the result, if any and explain why this was the given result.
- If there is an error, explain the error and correct the code.
- If a result was provided, re-write the code segment with the proper data type instead of
varand re-run the code.
Practice 1
Practice 2
Practice 3
Practice 4
Practice 5
- Modify the enhanced-for loop to use a traditional for loop. Use
varwhen declaring the loop control variable.
Practice 6
Practice 7
- Using
var, add anotherDogobject namedfinnand assign it tonull.
Practice 8
- Modify the
Dogclass by adding abarkmethod. - While declaring
soundof typevarworks, it isn't recommended. Explain why?
Practice 9
- This code segment contains one improper use of
var. On which line isvarbeing used improperly? - Modify the code by replacing one case of
varwith a data type.
Practice 10
- This code segment contains one improper use of
var. On which line isvarbeing used improperly? - Modify the code by replacing one case of
varwith a data type.
Practice 11
- This code segment contains one improper use of
var. On which line isvarbeing used improperly? - Modify the code by replacing one case of
varwith a data type.
Practice 12
- This code segment contains one improper use of
var. On which line isvarbeing used improperly? - Modify the code by replacing one case of
varwith a data type.
Pracitce 13
- On which line is
varbeing used improperly? - Modify the code so
numbersis declaredvarrather thanvar[]and assigned anintarray of size 3. - Modify the code to initialize
numbersto 1, 2, 3 using a for each loop.