What is the equivalent of the C# 'var' keyword in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In many programming languages, dynamic or implicit typing has become a popular feature because it allows for code that is more general and easier to write, especially in scenarios where the exact type of variables cannot be determined in advance. C# and Java, two of the mainstays of modern programming languages, both have approaches to handle this, although their implementations and the features they support can differ significantly.
C# 'var' Keyword
In C#, the var keyword was introduced with C# 3.0 to support implicitly typed local variables. It tells the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The type is resolved at compile-time, and once set, the variable is strongly typed, and its type cannot change.
Here, the compiler infers that number is an int, name is a string, and list is a List of integers. The use of var can make the code cleaner and more readable, particularly when dealing with complex generic types.
Java Equivalent: var in Java 10+
Java introduced a similar feature, also named var, in Java 10 under JEP 286 (Local-Variable Type Inference). Like C#, Java’s var allows you to skip the type declaration when declaring local variables. However, unlike dynamically typed languages where type inference happens at runtime, Java's var is also a compile-time feature ensuring that the code is strongly typed. Once a variable is declared using var, its type is fixed, and trying to assign a value of another type will throw a compile-time error.
Key Differences and Similarities
Despite the similarities in concept and syntax, there are key differences between how C# and Java implement their type inference with var:
- Scope: In C#,
varcan be used in both local scope (within methods) and in script or interactive mode (like within C# Interactive or LINQPad). In Java,varcan only be used in a local variable declaration context (within methods or code blocks). - Properties and Fields: C# disallows the
varkeyword in class-level fields or properties. Java, similarly, does not allowvarto be used when declaring class fields, method parameters, or return types.
Table: Comparison of 'var' in C# and Java
| Feature | C# var | Java var |
| Scope | Local variables, scripts | Local variables only |
| Type Safety | Compile-time | Compile-time |
| Class Fields | Not allowed | Not allowed |
| Method Parameters | Not allowed | Not allowed |
| Return Types | Not allowed | Not allowed |
Usage and Best Practices
The usage of var should be governed by the clarity it brings to the code. One common best practice is to use var when the right-hand side of the assignment makes the type obvious, or when dealing with generics whose type parameters are verbose to write out.
However, var should not be overused as it could make the code harder to read and maintain, particularly when the type is not clearly evident from the right-hand side of the assignment, potentially leading to confusion or errors during maintenance phases.
Conclusion
Both C# and Java offer a var keyword for type inference, allowing developers to write cleaner and more maintainable code while retaining strong type safety. The key to using var effectively involves understanding not just how it works, but also when its usage enhances readability and maintainability. Each language's implementation of var shows slight variations, primarily in terms of the allowed contexts and scope, but the underlying principles and benefits are similar.
Related reading
- What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?
- What is the fastest way in Java to get the amount of factors a number has
- What is the 'instanceof' operator used for in Java?
- What is the Java ? operator called and what does it do?
- What is the F language created to accomplish?
- What is the fastest way to calculate frequency distribution for array in C?
- What is the Java equivalent for LINQ?
- What is the LIMIT clause alternative in JPQL?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.