Home › Java Tutorial › All commands
All commands
Every command of this course: name, detailed description, syntax and example code.
Every command used in this course, with its name, a detailed description, the syntax and an example.
public class
Definition: Declares a class. In a file called Main.java the public class must be named Main.
Syntax:
public class Name {
...
}Example:
public class Main {
}main
Definition: The method where every Java program starts running. args holds command-line arguments.
Syntax:
public static void main(String[] args) {
...
}Example:
public static void main(String[] args) {
System.out.println("Hi");
}System.out.println
Definition: Prints a value and moves to a new line. Use print to stay on the same line and printf to format.
Syntax:
System.out.println(value);
Example:
System.out.println("Hello, Java!");int, double, String
Definition: Common types: int stores whole numbers, double stores decimals and String stores text.
Syntax:
type name = value;
Example:
int a = 5; double b = 2.5; String s = "Hi";
final
Definition: Makes a variable a constant: it can be assigned only once.
Syntax:
final type NAME = value;
Example:
final int MAX = 10;
if / else
Definition: Runs a block only when its condition is true. else if adds more choices and else catches the rest.
Syntax:
if (condition) {
...
} else {
...
}Example:
if (score >= 60) {
System.out.println("pass");
}switch
Definition: Compares one value against many case labels. The arrow form returns a value and needs no break.
Syntax:
switch (value) {
case 1 -> ...;
default -> ...;
}Example:
String s = switch (d) {
case 1 -> "Mon";
default -> "Other";
};for
Definition: Repeats a block using a start value, a condition and a step. The for-each form visits every item.
Syntax:
for (int i = 0; i < n; i++) {
...
}Example:
for (int i = 0; i < 3; i++) {
System.out.println(i);
}while
Definition: Repeats a block for as long as its condition stays true.
Syntax:
while (condition) {
...
}Example:
int i = 0;
while (i < 3) {
i++;
}static
Definition: Makes a field or method belong to the class itself instead of to each object.
Syntax:
static type name;
Example:
static int count = 0;
new
Definition: Creates a new object (or array) and calls its constructor.
Syntax:
new ClassName(arguments)
Example:
Dog d = new Dog("Rex", 3);class
Definition: Defines your own type that bundles fields (data) with methods (actions).
Syntax:
class Name {
fields;
methods;
}Example:
class Car {
int speed;
}extends
Definition: Makes a class inherit from a parent class and reuse its members.
Syntax:
class Child extends Parent { ... }Example:
class Cat extends Animal {
}interface
Definition: Lists methods that any implementing class must provide.
Syntax:
interface Name {
void method();
}Example:
interface Greeter {
void greet();
}ArrayList
Definition: A resizable list from java.util. Use add, get, remove and size.
Syntax:
ArrayList<Type> name = new ArrayList<>();
Example:
ArrayList<String> a = new ArrayList<>();
a.add("pear");try / catch
Definition: Runs risky code in try and handles an exception in catch. finally always runs.
Syntax:
try {
...
} catch (Exception e) {
...
}Example:
try {
Integer.parseInt("x");
} catch (NumberFormatException e) {
System.out.println("bad");
}import
Definition: Makes classes from another package available by their short name.
Syntax:
import package.Class;
Example:
import java.util.ArrayList;
return
Definition: Ends a method immediately and sends a value back to the caller.
Syntax:
return value;
Example:
static int twice(int n) {
return n * 2;
}Main.java
Definition: Save your code as Main.java , compile with javac Main.java and run with java Main .
Syntax:
Main.java
Example:
javac Main.java
System.out.print
Definition: System.out.println prints and moves to a new line, System.out.print stays on the same line, and System.out.printf formats values: %d int, %.2f decimals, %s text, %n new line.
Syntax:
System.out.print
Example:
System.out.print("No newline, ");
System.out.print("same line\n");System.out.printf
Definition: System.out.println prints and moves to a new line, System.out.print stays on the same line, and System.out.printf formats values: %d int, %.2f decimals, %s text, %n new line.
Syntax:
System.out.printf
Example:
System.out.printf("Age %d, pi %.2f, name %s%n", 20, 3.14159, "Ada");int
Definition: Declare a variable with its type: int , double , char , boolean or String .
Syntax:
int
Example:
int age = 20; final int MAX = 10;
double
Definition: Declare a variable with its type: int , double , char , boolean or String .
Syntax:
double
Example:
double price = 4.5;
char
Definition: Declare a variable with its type: int , double , char , boolean or String .
Syntax:
char
Example:
char grade = 'A';
boolean
Definition: Declare a variable with its type: int , double , char , boolean or String .
Syntax:
boolean
Example:
boolean ok = true;
String
Definition: Declare a variable with its type: int , double , char , boolean or String .
Syntax:
String
Example:
public static void main(String[] args) {
String name = "Ada";var
Definition: Declare a variable with its type: int , double , char , boolean or String .
Syntax:
var
Example:
var city = "Delhi";
byte
Definition: Java has eight primitive types: byte , short , int , long , float , double , char and boolean .
Syntax:
byte
Example:
byte b = 100;
long
Definition: Java has eight primitive types: byte , short , int , long , float , double , char and boolean .
Syntax:
long
Example:
long big = 9_000_000_000L;
float
Definition: Java has eight primitive types: byte , short , int , long , float , double , char and boolean .
Syntax:
float
Example:
float f = 1.5f;
f
Definition: Java has eight primitive types: byte , short , int , long , float , double , char and boolean .
Syntax:
f
Example:
float f = 1.5f; System.out.println(f);
Integer.parseInt
Definition: Integer.parseInt turns text into an int and String.valueOf turns a value into text.
Syntax:
Integer.parseInt
Example:
int n = Integer.parseInt("42");String.valueOf
Definition: Integer.parseInt turns text into an int and String.valueOf turns a value into text.
Syntax:
String.valueOf
Example:
String s = String.valueOf(3.5);
length()
Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .
Syntax:
length()
Example:
System.out.println(s.length());
toUpperCase()
Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .
Syntax:
toUpperCase()
Example:
System.out.println(s.toUpperCase());
substring(start, end)
Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .
Syntax:
substring(start, end)
Example:
System.out.println(s.substring(0, 5));
indexOf
Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .
Syntax:
indexOf
Example:
System.out.println(s.indexOf("World"));replace
Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .
Syntax:
replace
Example:
System.out.println(s.replace("World", "Java"));charAt
Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .
Syntax:
charAt
Example:
System.out.println(s.charAt(1));
equals()
Definition: Compare text with equals() , never with == .
Syntax:
equals()
Example:
System.out.println("a".equals("a"));String.join
Definition: Join strings with + or String.join .
Syntax:
String.join
Example:
System.out.println(String.join("-", "a", "b", "c"));Math
Definition: The Math class has ready-made helpers: max , min , sqrt , pow , abs , round , ceil and floor .
Syntax:
Math
Example:
System.out.println(Math.max(3, 9)); System.out.println(Math.min(3, 9)); System.out.println(Math.sqrt(16));
max
Definition: The Math class has ready-made helpers: max , min , sqrt , pow , abs , round , ceil and floor .
Syntax:
max
Example:
System.out.println(Math.max(3, 9));
min
Definition: The Math class has ready-made helpers: max , min , sqrt , pow , abs , round , ceil and floor .
Syntax:
min
Example:
System.out.println(Math.min(3, 9));
sqrt
Definition: The Math class has ready-made helpers: max , min , sqrt , pow , abs , round , ceil and floor .
Syntax:
sqrt
Example:
System.out.println(Math.sqrt(16));
pow
Definition: The Math class has ready-made helpers: max , min , sqrt , pow , abs , round , ceil and floor .
Syntax:
pow
Example:
System.out.println(Math.pow(2, 10));
abs
Definition: The Math class has ready-made helpers: max , min , sqrt , pow , abs , round , ceil and floor .
Syntax:
abs
Example:
System.out.println(Math.abs(-5));
round
Definition: The Math class has ready-made helpers: max , min , sqrt , pow , abs , round , ceil and floor .
Syntax:
round
Example:
System.out.println(Math.round(2.6));
ceil
Definition: The Math class has ready-made helpers: max , min , sqrt , pow , abs , round , ceil and floor .
Syntax:
ceil
Example:
System.out.println(Math.ceil(2.1));
if
Definition: Use if , else if and else .
Syntax:
if
Example:
if (score >= 90) {
} else if (score >= 70) {else
Definition: Use if , else if and else .
Syntax:
else
Example:
} else if (score >= 70) {
} else {break
Definition: The classic switch needs break after each case , or execution falls through to the next one.
Syntax:
break
Example:
case 1: System.out.println("Mon"); break;
case 2: System.out.println("Tue"); break;
case 3: System.out.println("Wed"); break;case
Definition: The classic switch needs break after each case , or execution falls through to the next one.
Syntax:
case
Example:
case 1: System.out.println("Mon"); break;
case 2: System.out.println("Tue"); break;
case 3: System.out.println("Wed"); break;continue
Definition: continue skips the rest of the current round and goes to the next one.
Syntax:
continue
Example:
if (i == 2) continue;
Arrays
Definition: The Arrays class ( import java.util.Arrays ) offers sort and toString .
Syntax:
Arrays
Example:
import java.util.Arrays; Arrays.sort(nums); System.out.println(Arrays.toString(nums));
sort
Definition: The Arrays class ( import java.util.Arrays ) offers sort and toString .
Syntax:
sort
Example:
Arrays.sort(nums);
toString
Definition: The Arrays class ( import java.util.Arrays ) offers sort and toString .
Syntax:
toString
Example:
System.out.println(Arrays.toString(nums));
void
Definition: Use void when it returns nothing.
Syntax:
void
Example:
static void greet(String name) {
public static void main(String[] args) {this
Definition: this refers to the current object.
Syntax:
this
Example:
this.name = name; this.age = age;
Counter.count
Definition: Access it with the class name, like Counter.count .
Syntax:
Counter.count
Example:
System.out.println(Counter.count);
private
Definition: Mark fields private so other classes cannot change them directly, then offer public getters and setters .
Syntax:
private
Example:
private double balance;
public
Definition: Mark fields private so other classes cannot change them directly, then offer public getters and setters .
Syntax:
public
Example:
public void deposit(double amount) {
public double getBalance() {
public class Main {super
Definition: A child can override a method (mark it @Override ) and call the parent's version with super .
Syntax:
super
Example:
super.speak();
enum
Definition: An enum defines constants such as days or levels.
Syntax:
enum
Example:
enum Level { LOW, MEDIUM, HIGH }values()
Definition: values() returns all of them and ordinal() gives the position.
Syntax:
values()
Example:
for (Level x : Level.values()) {ordinal()
Definition: values() returns all of them and ordinal() gives the position.
Syntax:
ordinal()
Example:
System.out.println(l.ordinal());
java.util
Definition: ArrayList (from java.util ) is a resizable list.
Syntax:
java.util
Example:
import java.util.*;
add
Definition: Use add , get , remove , size and Collections.sort .
Syntax:
add
Example:
fruits.add("pear");
fruits.add("apple");
fruits.add(0, "fig");get
Definition: Use add , get , remove , size and Collections.sort .
Syntax:
get
Example:
System.out.println(fruits.get(1));
remove
Definition: Use add , get , remove , size and Collections.sort .
Syntax:
remove
Example:
fruits.remove("apple");size
Definition: Use add , get , remove , size and Collections.sort .
Syntax:
size
Example:
System.out.println(fruits.size());
Collections.sort
Definition: Use add , get , remove , size and Collections.sort .
Syntax:
Collections.sort
Example:
Collections.sort(fruits);
HashMap
Definition: A HashMap stores values by key: put , get , containsKey .
Syntax:
HashMap
Example:
HashMap<String, Integer> ages = new HashMap<>();
put
Definition: A HashMap stores values by key: put , get , containsKey .
Syntax:
put
Example:
ages.put("Ada", 36);
ages.put("Linus", 28);containsKey
Definition: A HashMap stores values by key: put , get , containsKey .
Syntax:
containsKey
Example:
System.out.println(ages.containsKey("Bob"));HashSet
Definition: A HashSet keeps only unique values.
Syntax:
HashSet
Example:
Set<Integer> s = new HashSet<>(List.of(1, 2, 2, 3));
TreeMap
Definition: A HashMap has no guaranteed order, so wrap it in a TreeMap to loop in sorted key order.
Syntax:
TreeMap
Example:
for (String k : new TreeMap<>(ages).keySet()) {try
Definition: Put risky code in try and handle the problem in catch .
Syntax:
try
Example:
try {catch
Definition: Put risky code in try and handle the problem in catch .
Syntax:
catch
Example:
} catch (ArithmeticException e) {
} catch (NumberFormatException e) {filter
Definition: A stream chains steps such as filter , map and sum over a collection.
Syntax:
filter
Example:
.filter(n -> n % 2 == 0)
map
Definition: A stream chains steps such as filter , map and sum over a collection.
Syntax:
map
Example:
.map(n -> n * n)
sum
Definition: A stream chains steps such as filter , map and sum over a collection.
Syntax:
sum
Example:
int total = nums.stream().mapToInt(Integer::intValue).sum();
System.out::println
Definition: System.out::println is a method reference.
Syntax:
System.out::println
Example:
.forEach(System.out::println);
Scanner
Definition: Create a Scanner on System.in to read what the user types: nextLine() , nextInt() , nextDouble() .
Syntax:
Scanner
Example:
import java.util.Scanner;
// Real program: new Scanner(System.in)
Scanner sc = new Scanner("Ada 36");System.in
Definition: Create a Scanner on System.in to read what the user types: nextLine() , nextInt() , nextDouble() .
Syntax:
System.in
Example:
// Real program: new Scanner(System.in)
nextInt()
Definition: Create a Scanner on System.in to read what the user types: nextLine() , nextInt() , nextDouble() .
Syntax:
nextInt()
Example:
int age = sc.nextInt();
BufferedReader
Definition: A BufferedReader reads line by line, and try-with-resources closes it automatically.
Syntax:
BufferedReader
Example:
// Real file: new BufferedReader(new FileReader("data.txt"))
try (BufferedReader br = new BufferedReader(new StringReader("line one\nline two"))) {java.time
Definition: Classes are grouped in packages such as java.util and java.time .
Syntax:
java.time
Example:
import java.time.LocalDate;
LocalDate
Definition: LocalDate handles dates without time zones.
Syntax:
LocalDate
Example:
import java.time.LocalDate; LocalDate d = LocalDate.of(2026, 1, 15);
Example code
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}