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
var and 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
var when declaring the loop control variable.
Practice 6
Practice 7
- Using
var, add another Dog object named finn and assign it to null.
Practice 8
- Modify the
Dog class by adding a bark method.
- While declaring
sound of type var works, it isn't recommended. Explain why?
Practice 9
- This code segment contains one improper use of
var. On which line is var being used improperly?
- Modify the code by replacing one case of
var with a data type.
Practice 10
- This code segment contains one improper use of
var. On which line is var being used improperly?
- Modify the code by replacing one case of
var with a data type.
Practice 11
- This code segment contains one improper use of
var. On which line is var being used improperly?
- Modify the code by replacing one case of
var with a data type.
Practice 12
- This code segment contains one improper use of
var. On which line is var being used improperly?
- Modify the code by replacing one case of
var with a data type.
Pracitce 13
- On which line is
var being used improperly?
- Modify the code so
numbers is declared var rather than var[] and assigned an int array of size 3.
- Modify the code to initialize
numbers to 1, 2, 3 using a for each loop.