A Java program is made of tokens, which form statements. Command-line arguments let the user pass values to a program when it starts.

Java Tokens

A token is the smallest individual unit of a program. Java has five kinds. Keywords are reserved words with a fixed meaning, such as class, public, static, void, int, if, else, for, while, new and return; Java has about 50 of them, all in lower case. Identifiers are names given to classes, methods and variables. They may contain letters, digits, underscore and dollar sign, must not begin with a digit, must not be keywords, and are case-sensitive. By convention, class names start with a capital letter (Student), methods and variables use camelCase (totalMarks), and constants use capitals (MAX_SIZE). Literals are constant values: integer (25), floating-point (3.14), character ('A'), string ("Hello") and boolean (true, false). Operators are symbols for operations, such as + and ==. Separators are symbols such as ( ), { }, [ ], ;, , and . that group and separate code.

Definition: Token. The smallest meaningful unit of a Java program; the five kinds are keywords, identifiers, literals, operators and separators.

Java Statements

A statement is a complete instruction, usually ended by a semicolon. The kinds are empty statements (;), labelled statements, expression statements (x = y + 1;), selection statements (if, switch), iteration statements (for, while, do), jump statements (break, continue, return), synchronization statements and guarding statements (try, catch, finally). A block is a group of statements in braces.

Command-Line Arguments

Command-line arguments are values given after the class name when a program is run, such as java Square 7. They are passed to main as the String array args: args[0] is the first, args[1] the second, and args.length gives their number. Since they are strings, numbers must be converted, for example with Integer.parseInt(args[0]) or Double.parseDouble(args[0]). If the program reads an argument that was not given, an ArrayIndexOutOfBoundsException occurs, so the program should check args.length first.

Reading Input with Scanner

For interactive input, the Scanner class of java.util reads from the keyboard: Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); and so on.

Table 1.5: Java tokens

Token Meaning Examples
Keyword Reserved word class, int, if
Identifier Name chosen by programmer Student, total
Literal Constant value 25, 3.14, "Hi", true
Operator Performs an operation +, ==, &&
Separator Groups or separates ( ) { } ; ,

Example 1.5: Using command-line arguments

Problem: Write a program that prints the square of a number given on the command line, and greets a student whose name is also given.

Solution:

  1. Run it as java Square 7 Asha.
  2. args[0] is "7", converted with Integer.parseInt.
  3. args[1] is "Asha".
  4. The program checks that two arguments were given.

Result: The output shows the square 49 and a welcome message.

public class Square {
    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: java Square <number> <name>");
            return;
        }
        int n = Integer.parseInt(args[0]);
        System.out.println("Number of arguments = " + args.length);
        System.out.println("Square of " + n + " = " + (n * n));
        System.out.println("Welcome, " + args[1] + "!");
    }
}

Command line

java Square 7 Asha

Output

Number of arguments = 2
Square of 7 = 49
Welcome, Asha!

Common mistakes

  • Starting an identifier with a digit, as in 2total.
  • Using a keyword such as class or int as a variable name.
  • Using args[0] directly in arithmetic without converting it from String.

Key takeaways

  • The five tokens are keywords, identifiers, literals, operators and separators.
  • Identifiers are case-sensitive and cannot be keywords.
  • Command-line arguments arrive in String[] args.
  • Convert with Integer.parseInt() and check args.length.

Practice questions

  1. Name the five kinds of tokens.
  2. How is a number passed on the command line converted to int?
  3. Is "total$1" a valid identifier?

Answers to the practice questions

  1. Keywords, identifiers, literals, operators and separators.
  2. With Integer.parseInt(args[i]).
  3. Yes; it starts with a letter and uses only letters, digits and $.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit I