codegurukool.com · Free coding tutorials & online compiler

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

↗ Java get started

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");

↗ Java output

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");

↗ Java output

int

Definition: Declare a variable with its type: int , double , char , boolean or String .

Syntax:

int

Example:

int age = 20;
final int MAX = 10;

↗ Java variables

double

Definition: Declare a variable with its type: int , double , char , boolean or String .

Syntax:

double

Example:

double price = 4.5;

↗ Java variables

char

Definition: Declare a variable with its type: int , double , char , boolean or String .

Syntax:

char

Example:

char grade = 'A';

↗ Java variables

boolean

Definition: Declare a variable with its type: int , double , char , boolean or String .

Syntax:

boolean

Example:

boolean ok = true;

↗ Java variables

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";

↗ Java variables

var

Definition: Declare a variable with its type: int , double , char , boolean or String .

Syntax:

var

Example:

var city = "Delhi";

↗ Java variables

byte

Definition: Java has eight primitive types: byte , short , int , long , float , double , char and boolean .

Syntax:

byte

Example:

byte b = 100;

↗ Java data types

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;

↗ Java data types

float

Definition: Java has eight primitive types: byte , short , int , long , float , double , char and boolean .

Syntax:

float

Example:

float f = 1.5f;

↗ Java data types

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);

↗ Java data types

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");

↗ Java type casting

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);

↗ Java type casting

length()

Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .

Syntax:

length()

Example:

System.out.println(s.length());

↗ Java strings

toUpperCase()

Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .

Syntax:

toUpperCase()

Example:

System.out.println(s.toUpperCase());

↗ Java strings

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));

↗ Java strings

indexOf

Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .

Syntax:

indexOf

Example:

System.out.println(s.indexOf("World"));

↗ Java strings

replace

Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .

Syntax:

replace

Example:

System.out.println(s.replace("World", "Java"));

↗ Java strings

charAt

Definition: Useful methods: length() , toUpperCase() , substring(start, end) , indexOf , replace , charAt .

Syntax:

charAt

Example:

System.out.println(s.charAt(1));

↗ Java strings

equals()

Definition: Compare text with equals() , never with == .

Syntax:

equals()

Example:

System.out.println("a".equals("a"));

↗ Java strings

String.join

Definition: Join strings with + or String.join .

Syntax:

String.join

Example:

System.out.println(String.join("-", "a", "b", "c"));

↗ Java strings

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));

↗ Java math

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));

↗ Java math

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));

↗ Java math

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));

↗ Java math

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));

↗ Java math

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));

↗ Java math

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));

↗ Java math

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));

↗ Java math

if

Definition: Use if , else if and else .

Syntax:

if

Example:

if (score >= 90) {
} else if (score >= 70) {

↗ Java if...else

else

Definition: Use if , else if and else .

Syntax:

else

Example:

} else if (score >= 70) {
} else {

↗ Java if...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;

↗ Java switch

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;

↗ Java switch

continue

Definition: continue skips the rest of the current round and goes to the next one.

Syntax:

continue

Example:

if (i == 2) continue;

↗ Java break and 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));

↗ Java arrays

sort

Definition: The Arrays class ( import java.util.Arrays ) offers sort and toString .

Syntax:

sort

Example:

Arrays.sort(nums);

↗ Java arrays

toString

Definition: The Arrays class ( import java.util.Arrays ) offers sort and toString .

Syntax:

toString

Example:

System.out.println(Arrays.toString(nums));

↗ Java arrays

void

Definition: Use void when it returns nothing.

Syntax:

void

Example:

static void greet(String name) {
public static void main(String[] args) {

↗ Java methods

this

Definition: this refers to the current object.

Syntax:

this

Example:

this.name = name;
this.age = age;

↗ Java classes and objects

Counter.count

Definition: Access it with the class name, like Counter.count .

Syntax:

Counter.count

Example:

System.out.println(Counter.count);

↗ Java static members

private

Definition: Mark fields private so other classes cannot change them directly, then offer public getters and setters .

Syntax:

private

Example:

private double balance;

↗ Java encapsulation

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 {

↗ Java encapsulation

super

Definition: A child can override a method (mark it @Override ) and call the parent's version with super .

Syntax:

super

Example:

super.speak();

↗ Java inheritance

enum

Definition: An enum defines constants such as days or levels.

Syntax:

enum

Example:

enum Level { LOW, MEDIUM, HIGH }

↗ Java enums

values()

Definition: values() returns all of them and ordinal() gives the position.

Syntax:

values()

Example:

for (Level x : Level.values()) {

↗ Java enums

ordinal()

Definition: values() returns all of them and ordinal() gives the position.

Syntax:

ordinal()

Example:

System.out.println(l.ordinal());

↗ Java enums

java.util

Definition: ArrayList (from java.util ) is a resizable list.

Syntax:

java.util

Example:

import java.util.*;

↗ Java ArrayList

add

Definition: Use add , get , remove , size and Collections.sort .

Syntax:

add

Example:

fruits.add("pear");
fruits.add("apple");
fruits.add(0, "fig");

↗ Java ArrayList

get

Definition: Use add , get , remove , size and Collections.sort .

Syntax:

get

Example:

System.out.println(fruits.get(1));

↗ Java ArrayList

remove

Definition: Use add , get , remove , size and Collections.sort .

Syntax:

remove

Example:

fruits.remove("apple");

↗ Java ArrayList

size

Definition: Use add , get , remove , size and Collections.sort .

Syntax:

size

Example:

System.out.println(fruits.size());

↗ Java ArrayList

Collections.sort

Definition: Use add , get , remove , size and Collections.sort .

Syntax:

Collections.sort

Example:

Collections.sort(fruits);

↗ Java ArrayList

HashMap

Definition: A HashMap stores values by key: put , get , containsKey .

Syntax:

HashMap

Example:

HashMap<String, Integer> ages = new HashMap<>();

↗ Java HashMap and HashSet

put

Definition: A HashMap stores values by key: put , get , containsKey .

Syntax:

put

Example:

ages.put("Ada", 36);
ages.put("Linus", 28);

↗ Java HashMap and HashSet

containsKey

Definition: A HashMap stores values by key: put , get , containsKey .

Syntax:

containsKey

Example:

System.out.println(ages.containsKey("Bob"));

↗ Java HashMap and HashSet

HashSet

Definition: A HashSet keeps only unique values.

Syntax:

HashSet

Example:

Set<Integer> s = new HashSet<>(List.of(1, 2, 2, 3));

↗ Java HashMap and HashSet

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()) {

↗ Java HashMap and HashSet

try

Definition: Put risky code in try and handle the problem in catch .

Syntax:

try

Example:

try {

↗ Java exceptions

catch

Definition: Put risky code in try and handle the problem in catch .

Syntax:

catch

Example:

} catch (ArithmeticException e) {
} catch (NumberFormatException e) {

↗ Java exceptions

finally

Definition: finally always runs.

Syntax:

finally

Example:

} finally {

↗ Java exceptions

filter

Definition: A stream chains steps such as filter , map and sum over a collection.

Syntax:

filter

Example:

.filter(n -> n % 2 == 0)

↗ Java lambdas and streams

map

Definition: A stream chains steps such as filter , map and sum over a collection.

Syntax:

map

Example:

.map(n -> n * n)

↗ Java lambdas and streams

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();

↗ Java lambdas and streams

System.out::println

Definition: System.out::println is a method reference.

Syntax:

System.out::println

Example:

.forEach(System.out::println);

↗ Java lambdas and streams

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");

↗ Java user input

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)

↗ Java user input

nextInt()

Definition: Create a Scanner on System.in to read what the user types: nextLine() , nextInt() , nextDouble() .

Syntax:

nextInt()

Example:

int age = sc.nextInt();

↗ Java user input

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 file handling

java.time

Definition: Classes are grouped in packages such as java.util and java.time .

Syntax:

java.time

Example:

import java.time.LocalDate;

↗ Java packages, imports and dates

LocalDate

Definition: LocalDate handles dates without time zones.

Syntax:

LocalDate

Example:

import java.time.LocalDate;
LocalDate d = LocalDate.of(2026, 1, 15);

↗ Java packages, imports and dates

Example code

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, Java!");
  }
}

Run this code in the online editor

← Welcome to the Java courseJava introduction →