void main() {
// These lines are comments representing expression statements, but they don't affect the program as they are commented out:
// 10 + 15;
// 3 > 2;
int x; //Local variable declaraion Statement - when varibale is a local, its creates in a special location in memory
x = 5; // 1. Assignment Expression Statement: Assigns the value 5 to the variable 'x'.
// The following lines are unused expressions in the code but are examples of different expression types:
// x + 3; // Unused addition expression
// x - 1; // Unused subtraction expression
++x; // 2. Pre-Increment Expression Statement: Increments 'x' by 1 before its value is used.
x++; // 3. Post-Increment Expression Statement: Increments 'x' by 1 after its value is used.
x--; // 4. Post-Decrement Expression Statement: Decrements 'x' by 1 after its value is used.
--x; // 5. Pre-Decrement Expression Statement: Decrements 'x' by 1 before its value is used.
// 6. Method Invocation Expression Statements: Invoking methods to perform actions.
System.out.println("Programming Languages 02"); // Prints a string to the console.
System.out.println(10 + 15); // Prints the result of the addition (25) to the console.
System.out.println(5 > 2); // Prints the result of the comparison (true) to the console.
System.out.println(x = 5); // Assigns 5 to 'x' and then prints the assigned value (5).
sum(10, 15); // Method invocation statement for 'sum' with no output as it's not used in any expression.
System.out.println(sum(20, 10)); // Calls 'sum' method and prints the returned result (30).
print(10, 20); // Method invocation for 'print' that prints the sum (30) of two arguments directly.
// System.out.println(print(10, 20)); // Error: 'print' is a void method and cannot be used in another expression.
//7.Class Instance Creation Expression Statements
new A();
System.out.println(new A());
}
// 'sum' method that takes two integers and returns their sum.
int sum(int num1, int num2) {
return num1 + num2;
}
// 'print' method that takes two integers and prints their sum.
void print(int num1, int num2) {
System.out.println(num1 + num2);
}
class A{}
Expression statements in Java perform operations such as assignment, method invocation, object creation, or increment/decrement actions. Common types of expression statements include:
x = 5;.++x; or x--;.System.out.println(sum(10, 20));.sum(int, int), where a value is returned.
When a local variable is declared, it is stored in a special region of memory called the stack, which is part of the RAM. Each thread has its own stack, and when a method is called, a new stack frame is created to store the local variables and other information related to that method's execution. Local variables are only accessible within the method or block in which they are declared and are automatically removed from the stack when the method or block completes execution.
if a variable declared in inside of a block which inside of a type (Class, Interface,Enum, Annotation, Record) its conciderd a local variable.
When a variable is declared within any method, constructor, or block, regardless of its data type, it is considered a local variable.
in java 22 preview there is a new feature called unnamed class, void main() { } here a hidden unnamed class is compiled during run time (class - { void main(){ } }
An empty statement does nothing.
EmptyStatement:;