Definition
📌 Variables
Java is a statically typed language — every variable's type is declared and checked at compile time.
Syntax
syntax
type variableName = value;Example
java
public class Main {
public static void main(String[] args) {
int age = 20;
double price = 49.99;
String name = "CodeyHub";
System.out.println(name + " - " + age + " - " + price);
}
}Output
outputCodeyHub - 20 - 49.99
Important Points
- Primitive types:
int,double,char,boolean,long, and more. Stringis a reference type, not a primitive — but it behaves like text values in most expressions.- Local variables inside methods must be initialized before use, or the code won't compile.
- Java is case-sensitive, just like C and C++.
Common Mistakes
- Using a local variable in a method before assigning it a value — Java refuses to compile this.
- Confusing
String(capital S, a class) with a primitive type. - Forgetting semicolons at the end of statements.
Interview Tip
🎯 Before your interview
Know the difference between primitive types and reference types — a very common Java fundamentals interview question.
