Syntax
type variableName = value;Examples
Declaring Primitive Variables
Creating variables of each common primitive type.
int age = 21;
double gpa = 3.8;
char grade = 'A';
boolean isStudent = true;
long population = 8000000000L; // L suffix for long literals
float price = 19.99f; // f suffix for float literals
System.out.println(age + " " + gpa + " " + grade + " " + isStudent);Constants with final
Using the 'final' keyword to declare a variable that cannot be reassigned.
final double PI = 3.14159;
final int MAX_USERS = 100;
// PI = 3.14; // This would cause a compile error
System.out.println("Max users allowed: " + MAX_USERS);Variable Scope
Understanding how a variable's visibility is limited to the block it's declared in.
public class ScopeExample {
static int classField = 10; // accessible throughout the class
public static void main(String[] args) {
int localVar = 5; // only accessible inside main()
if (localVar > 0) {
int blockVar = 20; // only accessible inside this if block
System.out.println(blockVar);
}
System.out.println(classField + localVar);
// System.out.println(blockVar); // Error: out of scope here
}
}Default Values
Instance and static fields get default values automatically if not explicitly initialized (local variables do not).
public class Defaults {
int number; // defaults to 0
boolean flag; // defaults to false
String text; // defaults to null
public static void main(String[] args) {
Defaults d = new Defaults();
System.out.println(d.number); // 0
System.out.println(d.flag); // false
System.out.println(d.text); // null
}
}Best practices
- Use camelCase for variable names (studentAge, not StudentAge or student_age) to follow Java conventions
- Choose the smallest primitive type that safely fits your data - use int for whole numbers unless you specifically need long's larger range
- Use 'final' for values that should never change after initialization, both for safety and to communicate intent to other developers
- Prefer double over float for decimal values unless memory is a serious constraint - float has noticeably less precision
- Always initialize local variables explicitly - unlike fields, Java will not compile code that reads an uninitialized local variable
At a glance
- Purpose
- General-purpose application development
- File extension
- .java
- Runs in
- Java Virtual Machine
- Usually used with
- JDK and Java libraries
Specifications & further reading
Related Java documentation
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.Comparison & Logical Operators
Comparison operators (==, !=, <, >, <=, >=) compare two values and evaluate to a boolean. Logical operators (&&, ||, !) combine or invert boolean expressions, with && and || short-circuiting - meaning the second operand is skipped entirely if the first already determines the result. Importantly, == compares object references for non-primitive types like String, so .equals() should be used to compare their actual content.