bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Java/Java Tutorial
Java•Java Tutorial

Java var Keyword

The var Keyword

The var keyword was introduced in Java 10 (released in 2018).

The var keyword lets the compiler automatically detect the type of a variable based on the value you assign to it.

This helps you write cleaner code and avoid repeating types, especially for long or complex types.

For example , instead of writing int x = 5; , you can write:

Example

var x = 5;  // x is an int
System.out.println(x);

When using var , the compiler understands that 5 is an int .

Example with Different Types

Here are some examples showing how var can be used to create variables of different types, based on the values you assign:

var myNum = 5; // int
var myDouble = 9.98; // double
var myChar = 'D'; // char
var myBoolean = true; // boolean
var myString = "Hello"; // String

Important Notes

  1. var only works when you assign a value at the same time (you can't declare var x; without assigning a value):
var x; // Error
var x = 5; // OK
  1. Once the type is chosen, it stays the same. See example below:
var x = 5; // x is now an int
x = 10; // OK - still an int
x = 9.99; // Error - can't assign a double to an int

When to Use var

For simple variables, it's usually clearer to write the type directly ( int , double , char , etc.).

But for more complex types, such as ArrayList or HashMap , var can make the code shorter and easier to read:

// Without var ArrayList<String> cars = new ArrayList<String>();
// With var var cars = new ArrayList<String>();

Don't worry if the example above looks a bit advanced - you will learn more about these complex types later. For now, just remember that var was introduced in Java 10, and if you work with others, you might see it in their code - so it's good to know what it means.

Previous

Java Non-Primitive Data Types

Next

Java Type Casting

This chapter

Overview
49

Lessons

110m

Read time

1. Java Tutorial2. Java Introduction3. Java Getting Started4. Java Syntax5. Java Statements6. Java Output / Print7. Java Output Numbers8. Java Comments9. Java Variables10. Java Print Variables11. Java Declare Multiple Variables12. Java Identifiers13. Java Constants (final)14. Java Data Types15. Java Numbers16. Java Boolean Data Types17. Java Characters18. Java Data Types Example19. Java Non-Primitive Data Types20. Java var Keyword21. Java Type Casting22. Java Operators23. Java Arithmetic Operators24. Java Assignment Operators25. Java Comparison Operators26. Java Logical Operators27. Java Operator Precedence28. Java Strings29. Java String Concatenation30. Java Numbers and Strings31. Java Special Characters32. Java Math33. Java Booleans34. Java If ... Else35. Java Else36. Java Else If37. Java Short Hand If...Else (Ternary Operator)38. Java Nested If39. Java Logical Operators in Conditions40. Java Switch41. Java While Loop42. Java Do/While Loop43. Java For Loop44. Java Nested Loops45. Java For Each Loop46. Java Break and Continue47. Java Arrays48. Java Arrays Loop49. Java Multi-Dimensional Arrays

On this page

The var KeywordExample with Different TypesImportant NotesWhen to Use var