Java Essentials
Java Essentials
Author
2017-02-20
1
Variables and constants This
BASICis fancy
SYNTAX AND DATA TYPES:
}
}
In this case, printNumber is a method that prints the value of the myNumber
field.
Constructors are special methods used for initializing objects. They are called
when we create a new instance of a class. Here’s an example of a constructor:
public class MyClass {
int myNumber;
So is this 2
Variables and constants This
BASICis fancy
SYNTAX AND DATA TYPES:
So is this 3
Variables and constants This
BASICis fancy
SYNTAX AND DATA TYPES:
So is this 4
Variables and constants This
BASICis fancy
SYNTAX AND DATA TYPES:
So is this 5
This
BASICis fancy
Conditional statements (if, else, switch) SYNTAX AND DATA TYPES:
So is this 6
This
BASICis fancy
Conditional statements (if, else, switch) SYNTAX AND DATA TYPES:
break;
case value2:
// code to be executed if the expression matches value2
break;
...
default:
// code to be executed if none of the cases match the expression
}
Example:
int day = 2;
switch(day){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
...
default:
System.out.println("Invalid day");
}
In the above example, if the value of day is 2, it will print “Tuesday”. If the
value is not matched with any of the cases, it will execute the code inside the
default block.
These conditional statements are essential in writing programs that require
decision making based on different conditions. They provide the flexibility
to control the flow of execution and produce different outputs based on the
conditions.
So is this 7
This
BASICis fancy
Conditional statements (if, else, switch) SYNTAX AND DATA TYPES:
So is this 8
This
BASICis fancy
Conditional statements (if, else, switch) SYNTAX AND DATA TYPES:
The break statement is used to exit a loop prematurely. It can be used with for,
while, and do-while loops. When the break statement is encountered within a
loop, the loop is immediately terminated, and the program execution continues
with the next statement following the loop.
Here’s an example that demonstrates the use of the break statement:
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // the loop will terminate when i becomes 5
}
System.out.println(i);
}
In the code above, the loop will print the numbers from 1 to 4, and when
i becomes 5, the break statement is executed, causing the loop to terminate.
Therefore, the output will be:
1
2
3
4
The continue statement is used to skip the remaining code within a loop
iteration and move to the next iteration. It can be used with for, while, and
do-while loops. When the continue statement is encountered within a loop, the
current iteration is immediately skipped, and the program execution continues
with the next iteration.
Here’s an example that demonstrates the use of the continue statement:
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // the loop will skip even numbers
}
System.out.println(i);
}
In the code above, the loop will print the odd numbers from 1 to 10. When i
is an even number, the continue statement is executed, causing the remaining
code within the loop to be skipped. Therefore, the output will be:
1
3
5
7
9
Both the break and continue statements provide control flow within loops,
allowing you to modify the loop’s behavior based on certain conditions.
So is this 9
This
BASICis fancy
Conditional statements (if, else, switch) SYNTAX AND DATA TYPES:
Certainly, here’s a table summarizing the basic syntax and some commonly used
data types in Java:
So is this 10
This
BASICis fancy
Conditional statements (if, else, switch) SYNTAX AND DATA TYPES:
So is this 11