Language Overview

 is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is designed to be platform-independent, which means programs written in  can run on any device that has the  Virtual Machine (JVM) installed. It follows the principle of “Write Once, Run Anywhere” (WORA).

 is widely used for web development, mobile applications (especially Android), enterprise software, and more. It has a rich API and a large community of developers, making it one of the most popular programming languages in the world.

Key Features of :

Platform Independence:  programs can run on any system with a JVM, irrespective of the underlying architecture.

Object-Oriented: Everything in  is treated as an object, making it easier to model real-world entities.

Secure:  provides built-in security features to prevent unauthorized access to data.

Multithreading:  supports multithreading, allowing programs to perform multiple tasks simultaneously.

Garbage Collection:  automatically handles memory management through garbage collection, which frees up memory no longer in use.

Tokens in

In , a token is the smallest unit of a program that has meaningful significance. Tokens are the building blocks of a  program, and they are classified into the following types:

Keywords: Reserved words that have predefined meanings in . Examples include int, class, public, static, if, while, etc.

Identifiers: Names given to various program elements such as variables, methods, classes, etc. More on this below.

Literals: Constants in the program like numbers, characters, or string values. Example: 5, ‘a’, “Hello”.

Operators: Symbols used to perform operations on variables and values. Examples include +, -, *, /, &&, etc.

Separators: Symbols used to separate statements or code blocks. Examples: ;, {, }, (), ,.

Comments: Notes written within the code to explain it. They are not executed by the program. Example: // (single-line), /* … */ (multi-line).

Identifiers in

An identifier is the name given to elements like variables, methods, classes, arrays, etc. It is used to identify the entity in the program.

Rules for identifiers:

An identifier can contain letters (a-z, A-Z), digits (0-9), underscores (_), or dollar signs ($).

The first character of an identifier must be a letter, underscore, or dollar sign (it cannot be a digit).

 is case-sensitive, so myVariable and myvariable are considered different identifiers.

Identifiers cannot be  keywords (reserved words like class, public, int).

Examples of valid identifiers:

age

totalAmount

_counter

MY_VARIABLE

Examples of invalid identifiers:

2ndNumber (cannot start with a digit)

int (a keyword)

Variables in

A variable is a container that holds data that can change during the execution of a program. Variables in  must be declared with a specific data type, such as int, double, char, etc.

Syntax to declare a variable:

 

 

dataType variableName;

For example:

 

 

int age;  // Declaring an integer variable ‘age’

double salary;  // Declaring a double variable ‘salary’

char grade;  // Declaring a char variable ‘grade’

You can also initialize a variable at the time of declaration:

 

 

int age = 25;  // Declaring and initializing ‘age’

double salary = 50000.50;  // Declaring and initializing ‘salary’

Variable Types:

Local variables: Declared within a method or block of code and only accessible within that method/block.

Instance variables: Declared within a class but outside methods, constructors, or blocks. They belong to an object.

Class variables: Declared with the static keyword, meaning they belong to the class rather than instances of the class.

Operators in

Operators are symbols used to perform operations on variables or values.  supports various types of operators, which can be categorized as follows:

Arithmetic Operators

These operators perform basic arithmetic operations.

Operator

Description

Example

+

Addition

a + b

Subtraction

a – b

*

Multiplication

a * b

/

Division

a / b

%

Modulus (Remainder)

a % b

Relational (Comparison) Operators

These operators compare two values and return a boolean value (true or false).

Operator

Description

Example

==

Equal to

a == b

!=

Not equal to

a != b

Greater than

a > b

Less than

a < b

>=

Greater than or equal to

a >= b

<=

Less than or equal to

a <= b

Logical Operators

These operators are used to perform logical operations, typically on boolean values.

Operator

Description

Example

&&

Logical AND

a && b

`

 

`

!

Logical NOT

!a

Assignment Operators

These operators are used to assign values to variables.

Operator

Description

Example

=

Assigns value

a = 5

+=

Adds and assigns

a += 5

-=

Subtracts and assigns

a -= 5

*=

Multiplies and assigns

a *= 5

/=

Divides and assigns

a /= 5

%=

Modulus and assigns

a %= 5

Unary Operators

These operators operate on a single operand.

Operator

Description

Example

++

Increment by 1

a++

Decrement by 1

a–

+

Unary plus (positive value)

+a

Unary minus (negative value)

-a

Ternary Operator

A shorthand version of the if-else statement. It takes three operands.

Operator

Description

Example

?:

Ternary conditional operator

result = (a > b) ? a : b;

Bitwise Operators

These operators perform bit-level operations on integer types.

Operator

Description

Example

&

Bitwise AND

a & b

`

`

Bitwise OR

^

Bitwise XOR

a ^ b

~

Bitwise NOT

~a

<< 

Left shift

a << 2

>> 

Right shift

a >> 2

Instanceof Operator

Used to test whether an object is an instance of a specific class or interface.

Operator

Description

Example

instanceof

Checks if an object is an instance of a class/interface

obj instanceof MyClass

Conclusion

Tokens are the fundamental building blocks of  programs, which include keywords, identifiers, literals, operators, separators, and comments.

Identifiers are used to name variables, methods, and classes.

Variables are used to store data in a program, and they must be declared with a data type.

Operators perform various operations on data, and they are categorized into arithmetic, relational, logical, assignment, unary, ternary, bitwise, and instanceof operators.

 

Basic  Program Structure

A basic  program consists of:

Class Declaration:  code is written inside a class.

Main Method: The entry point for any  application.

Syntax of a  Program:

public class ClassName {

    public static void main(String[] args) {

        // Code goes here

    }

}

Example 1: Hello World Program

This program prints “Hello, World!” to the console.

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println(“Hello, World!”);

    }

}

Explanation:

public class HelloWorld: Declares a public class named HelloWorld.

public static void main(String[] args): The main method is the entry point of any  program. It is where the execution begins.

System.out.println(“Hello, World!”);: This prints the message to the console.

Example 2: Variables and Data Types

This program demonstrates the use of variables and basic data types.

public class DataTypesExample {

    public static void main(String[] args) {

        int age = 25;  // Integer data type

        double salary = 50000.50;  // Double data type

        char grade = ‘A’;  // Character data type

        boolean isActive = true;  // Boolean data type

       

        System.out.println(“Age: ” + age);

        System.out.println(“Salary: ” + salary);

        System.out.println(“Grade: ” + grade);

        System.out.println(“Active: ” + isActive);

    }

}

Explanation:

int, double, char, boolean are different data types in .

System.out.println() prints the value of the variables.

Example 3: Conditional Statement (If-Else)

This program demonstrates how to use if-else for decision making.

public class IfElseExample {

    public static void main(String[] args) {

        int number = 10;

       

        if (number > 0) {

            System.out.println(“The number is positive.”);

        } else {

            System.out.println(“The number is negative or zero.”);

        }

    }

}

Explanation:

The if statement checks whether a condition is true. If it is, the first block of code is executed; otherwise, the else block is executed.

Example 4: Loop (For Loop)

This program demonstrates a for loop to print numbers from 1 to 5.

public class ForLoopExample {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            System.out.println(i);

        }

    }

}

Explanation:

The for loop iterates from 1 to 5, printing the value of i during each iteration.

Example 5: Functions/Methods

This program demonstrates how to create and call a method in .

public class MethodExample {

    public static void main(String[] args) {

        int result = addNumbers(10, 20);

        System.out.println(“Sum: ” + result);

    }

 

    public static int addNumbers(int a, int b) {

        return a + b;

    }

}

Explanation:

addNumbers(int a, int b) is a method that takes two integer arguments and returns their sum.

The method is called in the main method, and the result is printed.

Conclusion

These are some basic concepts and examples in  to get started. ‘s object-oriented nature and its vast libraries make it a powerful language for building a wide variety of applications. Understanding these basics will allow you to dive deeper into more advanced topics such as classes, inheritance, polymorphism, exception handling, and more.

 

Scroll to Top