Use the following problems to test your type inference programming prowess. I strongly encourage you to give each problem a try before you turn to the solutions and download the example programs:
- Simple var example: Write a program that exemplifies the correct usage of type inference (var) with respect to the code's readability.
- Using var with primitive types: Write a program that exemplifies the usage of var with Java primitive types (int, long, float, and double).
- Using var and implicit type casting to sustain the code's maintainability: Write a program that exemplifies how var and implicit type casting can sustain the code's maintainability.
- Explicit downcast or better avoid var: Write a program that exemplifies the combination of var and explicit downcast and explain why var should be avoided.
- Avoid using var if the called names don't contain enough type information for humans: Provide examples where var should be avoided because its combination with called names causes loss of information for humans.
- Combining LVTI and programming to the interface technique: Write a program that exemplifies the usage of var via the programming to the interface technique.
- Combining LVTI and the diamond operator: Write a program that exemplifies the usage of var with the diamond operator.
- Assigning an array to var: Write a program that assigns an array to var.
- Using LVTI in compound declarations: Explain and exemplify the usage of LVTI with compound declarations.
- LVTI and variable scope: Explain and exemplify why LVTI should minimize the variable's scope as much as possible.
- LVTI and the ternary operator: Write several snippets of code that exemplify the advantages of combining LVTI and the ternary operator.
- LVTI and for loops: Write several examples that exemplify the usage of LVTI in for loops.
- LVTI and streams: Write several snippets of code that exemplify the usage of LVTI and Java streams.
- Using LVTI to break up nested/large chains of expressions: Write a program that exemplifies the usage of LVTI for breaking up a nested/large chain of expressions.
- LVTI and the method return and argument types: Write several snippets of code that exemplify the usage of LVTI and Java methods in terms of return and argument types.
- LVTI and anonymous classes: Write several snippets of code that exemplify the usage of LVTI in anonymous classes.
- LVTI can be final and effectively final: Write several snippets of code that exemplify how LVTI can be used for final and effectively final variables.
- LVTI and lambdas: Explain via several snippets of code how LVTI can be used in combination with lambda expressions.
- LVTI and null initializers, instance variables, and catch blocks variables: Explain with examples how LVTI can be used in combination with null initializers, instance variables, and catch blocks.
- LVTI and generic types, T: Write several snippets of code that exemplify how LVTI can be used in combination with generic types.
- LVTI, wildcards, covariants, and contravariants: Write several snippets of code that exemplify how LVTI can be used in combination with wildcards, covariants, and contravariants.