Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Getting Started with Java

Tokens, Identifiers, Literals in Java

Tokens

Token is the smallest individual unit or element of a program. It is the building block used to write the code. The Java compiler breaks the source code into these tokens during the compilation process to understand and process the code.

Types of Tokens in Java

Java programs consist of the following types of tokens

  • Keywords

  • Identifiers

  • Literals

  • Operators

  • Comments

Keywords

Keywords are the predefined words in the programming language which will have some kind of predefined meaning.

Examples: class, public, static, void, int, etc.

Rule: All keywords should be written in lowercase.

Identifiers

Identifiers are the programmers defined words the words for which the programmer will give some kind of meaning. are called as identifiers.

Example: class name, variable name, function name, interface name, package name, etc.

Rules:

  • These can be alphanumeric.

  • No special character allowed apart from $, &.

  • keywords can’t be identifiers.

  • Because it is alphanumeric it can’t start with no, it either starts with alphabet, _, &.

Literals

literals are values used in the program

There are 4 types of literals

  • Numeric literals

  • character literals

  • String literals

  • Boolean literals

Numeric literals

Any numeric values considered numeric literals

Example: 10, 10.5, etc.

Character literals

Anything which is enclosed within single quotes is called character as character literals. and we should enclose only one character within the single quote.

Example: ‘A’, ‘g’, ‘9’ etc.

String literals

Anything which is enclosed within double quotes is considered as string literals.

Example: “Hello”, “1000”, “g” etc.

Boolean literals

there are only 2 boolean literals.

  • true

  • false

Comments

Comments in programming are notes written by the developer within the code that explain what the code is doing, clarify complex parts of the code, or provide additional context.

These notes are ignored by the computer when the code runs, meaning they do not affect the program's behavior. Comments are primarily used to make the code more readable and understandable for humans.

Types of Comments in Java:

  1. Single-line comments (//): Used for short explanations on a single line.

    
    // This is a comment
    
  2. Multi-line comments (/* */): Used for longer explanations or blocking out multiple lines of code.

    
    /* This is a multi-line comment
       that spans across multiple lines. */
  3. Javadoc comments (/** */): Special comments used to document classes and methods, usually for creating external documentation.

    /**
     * This method adds two numbers.
     * @param a First number
     * @param b Second number
     * @return Sum of a and b
     */
    public int add(int a, int b) {
        return a + b;
    }

Conclusion

After learning about Tokens, Variables, Identifiers, Literals, and Operators in Java, you’ve covered some of the basic building blocks needed to write a program.

  • Tokens are the smallest pieces of a program, including keywords, operators, and identifiers.

  • Variables store data, and identifiers are the names you give to things like variables and methods.

  • Literals represent fixed values like numbers or text, while operators allow you to perform actions on these values, like adding or comparing them.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board