Java statements is the most fundamental topic to master to be an effective Java programmer.
Four types of statements are:
Assignment Statement:
Assignment statements of all primitives will return their primitive values.
1. Value can be assigned as shown below
int a;
int b;
a = (b=true);
2. common type of assigning value is
int a = 9;
3. Remember that the only valid literal values for the boolean primitives are true and false. Below statements are invalid
boolean isValid = TRUE;
boolean isValid = 1;
Conditional Statement:
Valid scenarios
1. true or false statements and the following type of assignment statement is correct inside a if statement.
boolean a = false;
if(a = true)
{}
2. Boolean wrapper class can be used as unboxing is the automatic production of its primitive value in cases where it is needed. So the following code is correct.
Boolean wrapperBoolean = new Boolean("true");
if(wrapperBoolean)
{}
3. Order does not matter in the switch cases. If a case is satisfied then default is ignored.
int a = 1;
switch(a)
{
default:
System.out.println("Default executed first");
case 1:
System.out.println("a executed first");
}
Output: a executed first
4. String can be used in cases starting Java 7
5. Arithmetic operations are allowed in case as shown below
int a = 2;
switch(a)
{
default:
System.out.println("Default executed first");
break;
case 3-1:
System.out.println("2 executed first");
break;
}
Output: 2 executed first
6. Switch fall through can be taken advantage because all the cases following a success case without a break will get executed. Eg of fallthrough is shown below
This is the output from the code:
7. Following statement compiles without any problem.
if (x == 0) ; else if (x == 1){} else {;}
8. Following statements compiles without any problem.
if (true) ;
if (true) {}
if (true) {;}
if (true) {;;}
if (true) ;{};
Invalid scenarios
1. Since assignment statements return their primitive values, following code will fail to compile
int a = 1;
if(a = 2)
{}
Four types of statements are:
Assignment Statement:
Assignment statements of all primitives will return their primitive values.
1. Value can be assigned as shown below
int a;
int b;
a = (b=true);
2. common type of assigning value is
int a = 9;
3. Remember that the only valid literal values for the boolean primitives are true and false. Below statements are invalid
boolean isValid = TRUE;
boolean isValid = 1;
Conditional Statement:
Valid scenarios
1. true or false statements and the following type of assignment statement is correct inside a if statement.
boolean a = false;
if(a = true)
{}
2. Boolean wrapper class can be used as unboxing is the automatic production of its primitive value in cases where it is needed. So the following code is correct.
Boolean wrapperBoolean = new Boolean("true");
if(wrapperBoolean)
{}
3. Order does not matter in the switch cases. If a case is satisfied then default is ignored.
int a = 1;
switch(a)
{
default:
System.out.println("Default executed first");
case 1:
System.out.println("a executed first");
}
Output: a executed first
4. String can be used in cases starting Java 7
String a = "hello";
switch(a)
{
default:
System.out.println("Default executed first");
break;
case "1":
System.out.println("1 executed first");
break;
case "hello":
System.out.println("hello executed first");
break;
}
Output: hello executed first5. Arithmetic operations are allowed in case as shown below
int a = 2;
switch(a)
{
default:
System.out.println("Default executed first");
break;
case 3-1:
System.out.println("2 executed first");
break;
}
Output: 2 executed first
6. Switch fall through can be taken advantage because all the cases following a success case without a break will get executed. Eg of fallthrough is shown below
public class SwitchDemoFallThrough {
public static void main(String[] args) {
java.util.ArrayList futureMonths =
new java.util.ArrayList();
int month = 8;
switch (month) {
case 1: futureMonths.add("January");
case 2: futureMonths.add("February");
case 3: futureMonths.add("March");
case 4: futureMonths.add("April");
case 5: futureMonths.add("May");
case 6: futureMonths.add("June");
case 7: futureMonths.add("July");
case 8: futureMonths.add("August");
case 9: futureMonths.add("September");
case 10: futureMonths.add("October");
case 11: futureMonths.add("November");
case 12: futureMonths.add("December");
break;
default: break;
}
if (futureMonths.isEmpty()) {
System.out.println("Invalid month number");
} else {
for (String monthName : futureMonths) {
System.out.println(monthName);
}
}
}
}
August
September
October
November
December
7. Following statement compiles without any problem.
if (x == 0) ; else if (x == 1){} else {;}
8. Following statements compiles without any problem.
if (true) ;
if (true) {}
if (true) {;}
if (true) {;;}
if (true) ;{};
Invalid scenarios
1. Since assignment statements return their primitive values, following code will fail to compile
int a = 1;
if(a = 2)
{}
2. Ranges are not allowed in cases.
3.Following statement throws compilation error because no space between elseif
if (x == 0) {;} elseif (x == 1) {System.out.println("Valid Statement");}
3.Following statement throws compilation error because no space between elseif
if (x == 0) {;} elseif (x == 1) {System.out.println("Valid Statement");}
No comments:
Post a Comment