Java Variables Explained: Types, var
Keyword & Interview Questions for Beginners
What is a Variable in Java?
To fully understand how variables work in Java, it’s helpful to first know how Java runs your programs. This involves understanding key Java components like the JDK, JRE, and JVM, which are responsible for compiling and executing your code.
A variable is a name or label that stores data in your program’s memory. Think of it as a container or box holding information such as numbers, text, or true/false values.
How to Declare Variables in Java
Variables must be declared before use, specifying their data type and name:
Example:
You can also declare without initialization:
Types of Variables:
![]() |
Types of variable. |
1. Local Variables
Declared inside methods and accessible only within that method. Must be initialized before use.
2. Instance Variables/ Global Variables
Declared inside a class but outside methods. Each object of the class has its own copy.
3. Static Variables
Declared with the static
keyword and shared across all objects of the class.
Primitive Data Types in Java
Type | Example |
---|---|
int | int age = 20; |
float | float price = 9.99f; |
double | double pi = 3.14; |
char | char grade = 'A'; |
boolean | boolean flag = true; |
Type Inference with var
Starting with Java 10, you can use var
to let Java automatically determine the variable’s type based on the assigned value.
Instead of:
You can write:
Important points about var
:
-
Must assign a value when declaring with
var
. -
Can only be used for local variables inside methods.
-
The inferred type is fixed and checked by Java at compile time.
Default Values of Variables
Variable Type | Default Value |
---|---|
int | 0 |
boolean | false |
char | '\u0000' |
Object types | null |
Note: Local variables do NOT have default values and must be initialized before use.
Simple Example Combining Variables
Best Practices for Using Variables
-
Use meaningful names like
userAge
instead ofx
. -
Follow camelCase naming convention.
-
Initialize variables before use.
-
Limit the variable scope to where it is needed.
Common Java Variables Interview Questions
-
What is a variable in Java?
-
What are the different types of variables?
-
What is the default value of a boolean variable?
-
Can local variables have default values?
-
What is the
var
keyword in Java? -
What is variable shadowing?
-
Where are local variables stored in memory?
-
Can you use static variables inside methods?
Conclusion
Variables are a fundamental part of Java programming. Understanding how to declare and use different types of variables — including the modern var
keyword — will improve your coding skills and prepare you for technical interviews. Practice writing code using variables to get comfortable and confident!
๐ฌ Join the Conversation!
Do you have any questions or want to share your experience with Java variables? Maybe you found a tricky interview question or a cool coding trick? Drop your thoughts below — I’d love to hear from you! ๐
Comments Section
Comments
Post a Comment