Junior
What types of operators (statements) exist in Java?
sobes.tech AI
Answer from AI
In Java, operators (statements) are commands executed by the program. They form the basic building blocks of programs.
Types of operators:
- Expression Statements: Consist of an expression followed by a semicolon (;). The expression is evaluated, and the result is discarded unless it is an assignment, method call, or
++/--operators. - Declaration Statements: Used to declare variables, classes, or methods.
- Control-Flow Statements: Define the order of execution of other operators. Include:
- Selection Statements:
if-thenif-then-elseswitch
- Looping Statements:
forwhiledo-whilefor-each(enhanced for)
- Jump Statements: Change the normal flow of execution.
breakcontinuereturn
- Selection Statements:
- Block Statements: A group of zero or more operators enclosed in curly braces
{}. Block statements create a scope.
Examples:
// Expression operator
int x = 10;
// Declaration operator
String name;
class MyClass {}
// Control flow operator - if
if (x > 5) {
System.out.println("x is greater than 5");
}
// Control flow operator - for
for (int i = 0; i < 3; i++) {
System.out.println(i);
}
// Control flow operator - break
while (true) {
if (x == 10) {
break; // Exit the loop
}
x++;
}
// Block operator
{
int y = 20; // Variable y is only visible here
System.out.println(y);
}