Java Reserved Keywords:
Java has a set of keywords that are reserved words that cannot be used as variables, methods, classes, or any other identifiers.
For example: break, char, double, final
Literal:
Any constant value which can be assigned to the variable is called literal/constant.
Literals in Java is a synthetic representation of boolean, numeric, character, or string data.
Control statements in Java:
Branching–>
1. Simple if statement
2. if-else statement
3. else-if ladder
4. Nested if-statement
5. switch
1)Syntax of if statement is given below.
if(condition) {
statement 1; //executes when condition is true
}
1. class Student {
2. public static void main(String[] args) {
3. int x = 10;
4. int y = 12;
5. if(x+y > 20) {
6. System.out.println(“x + y is greater than 20”);
7. }
8. }
9. }
output: x + y is greater than 20
2) if-else statement
Syntax:
1. if(condition) {
2. statement 1; //executes when condition is true
3. }
4. else{
5. statement 2; //executes when condition is false
6. }
1. class Student {
2. public static void main(String[] args) {
3. int x = 10;
4. int y = 12;
5. if(x+y < 10) {
6. System.out.println(“x + y is less than 10”);
7. }
else {
8. System.out.println(“x + y is greater than 20”);
9. }
10. }
11. }
Output: x + y is greater than 20
3) else-if ladder:
1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. }
4. else if(condition 2) {
5. statement 2; //executes when condition 2 is true
6. }
7. else {
8. statement 3; //executes when all the conditions are false
9. }
1. class Student {
2. public static void main(String[] args) {
3. String city = “Delhi”;
4. if(city == “Meerut”) {
5. System.out.println(“city is meerut”);
6. }else if (city == “Noida”) {
7. System.out.println(“city is noida”);
8. }else if(city == “Agra”) {
9. System.out.println(“city is agra”);
10. }else {
11. System.out.println(city);
12. }
13. }
14. }
Output: Delhi
4. Nested if-statement
Syntax of Nested if-statement is given below.
if(condition)
{
—->if(condition)
//statement1.1
else
//statement1.2
}
else
{
—->if(condition)
//statement2.1
else
//statement2.2
}
example:
class Student {
public static void main(String[] args) {
String address = “Delhi, India”;
if(address.endsWith(“India”)) //true
{
if(address.contains(“Meerut”))
{
System.out.println(“Your city is Meerut”);
}
else if(address.contains(“Noida”))
{
System.out.println(“Your city is Noida”);
}
else
{
System.out.println(address.split(“,”)[0]);
}
}
else
{
System.out.println(“You are not living in India”);
}
}
}
Output: Delhi
Switch Statement:
1. switch (expression){
2. case value1:
3. statement1;
4. break;
5. .
6. .
7. .
8. case valueN:
9. statementN;
10. break;
11. default:
12. default statement;
13. }
Example:
class Student {
public static void main(String[] args) {
int num = 2;
switch(num){
case 0:
System.out.println(“number is 0”);
break;
case 1:
System.out.println(“number is 1”);
break;
default:
System.out.println(num);
}
}
}
Output: 2