- We declare local variables in a method with its data type.
- From Java 10 or higher version onwards , we can use var keyword to declare local variables in a method. Same variables can be declared with var keyword like below.
- Java compiler takes the variables type from it's initializer. 20 is integer value, so age data type is int.
- You can use var keyword to declare reference variables.
- You can use var to declare variable inside for loop, forEach loop.
Restrictions :
- You can't use var to declare array initializer .
- You can't use var to declare class level instance , static variables.
Conclusion:
var keyword is good addition to Java programming language. It shortens the variables declaration. This is all for today. Happy Coding 😀
excellent blog, very clear explanation. keep updating regulary. Full Stack Course In Pune
ReplyDeleteexcellent informative. Python Course In Pune
ReplyDelete