JAVA REV Flashcards
Decision Making Structures
Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Java programming language provides following types of decision making statements.
if statement
if..else statement
nested if statement
switch statement
IF Statement
An if statement consists of a Boolean expression followed by one or more statements.
Syntax :
if(Boolean_expression) {
// Statements will execute if the Boolean expression is true
}
If the Boolean expression evaluates to true then the block of code inside the if statement will be executed. If not, the first set of code after the end of the if statement (after the closing curly brace) will be executed.
IF..ELSE Statement
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
Syntax :
if(Boolean_expression) {
// Executes when the Boolean expression is true
}else {
// Executes when the Boolean expression is false
}
IF..ELSE IF Statement
An if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement.
When using if, else if, else statements there are a few points to keep in mind.
An if can have zero or one else’s and it must come after any else if’s.
An if can have zero to many else if’s and they must come before the else.
Once an else if succeeds, none of the remaining else if’s or else’s will be tested.
Nested IF Statement
It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.
SWITCH Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Rules for SWITCH Statement
The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums.
You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Loops In Java
There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.
Types Of Loops :
Java programming language provides the following types of loop to handle looping requirements :
While loop
For loop
Do..While loop
While Loop
A while loop statement in Java programming language repeatedly executes a target statement as long as a given condition is true.
Syntax :
while(Boolean_expression) {
// Statements
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non zero value.
When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true.
When the condition becomes false, program control passes to the line immediately following the loop.
For Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
Syntax :
for(initialization; Boolean_expression; update) {
// Statements
}
Do..While Loop
A do…while loop is similar to a while loop, except that a do…while loop is guaranteed to execute at least one time.
Syntax :
do {
// Statements
}while(Boolean_expression);
Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.
If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop execute again. This process repeats until the Boolean expression is false.
Enhanced For Loop
As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays.
Syntax :
for(declaration : expression) {
// Statements
}
Declaration − The newly declared block variable, is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element.
Expression − This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.
Usage of “this” keyword
There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.
Here is given the 6 usage of java this keyword.
this can be used to refer current class instance variable.
this can be used to invoke current class method (implicitly)
this() can be used to invoke current class constructor.
this can be passed as an argument in the method call.
this can be passed as argument in the constructor call.
this can be used to return the current class instance from the method.
Usage of the “new” keyword
When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class.
Declaration : First, you must declare a variable of the class type. This variable does not define an object.
Instantiation and Initialization : Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. The new operator instantiates a class by dynamically allocating(i.e, allocation at run time) memory for a new object and returning a reference to that memory. This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated.
The new operator is also followed by a call to a class constructor, which initializes the new object. A constructor defines what occurs when an object of a class is created. Constructors are an important part of all classes and have many significant attributes.
Usage of “super” keyword
The super keyword in Java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
super can be used to refer immediate parent class instance variable.
super can be used to invoke immediate parent class method.
super() can be used to invoke immediate parent class constructor.
Static Keyword
The static keyword in Java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than an instance of the class.
The static can be:
Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class
Static variable
If you declare any variable as static, it is known as a static variable.
The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.
The static variable gets memory only once in the class area at the time of class loading.
It makes your program memory efficient (i.e., it saves memory).
Final Keyword
The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:
variable
method
class
The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.