0 ratings 0% found this document useful (0 votes) 2 views 3 pages Java Notes 08
The document provides an overview of various Java keywords and control statements, including their definitions and usage. It covers keywords like 'while', 'null', 'private', 'public', and control flow statements such as 'if', 'switch', and loops. Additionally, it explains decision-making statements and their syntax, including simple if statements and if-else structures.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
30.
31.
32.
33,
34,
35.
36.
37.
38.
39.
1.
42.
43,
. while: Java while keyword is used to start a w!
null: Java null keyword is used to indicate that a reference does not refer to anything. It
removes the garbage value.
package: Java package keyword is used to declare a Java package that includes the
classes.
private: Java private keyword is an access modifier. It is used to indicate that a method
or variable may be accessed only in the class in which it is declared.
protected: Java protected keyword is an access modifier. It can be accessible within the
package and outside the package but through inheritance only. It can't be applied with
the class.
public: Java public keyword is an access modifier. It is used to indicate that an item is
accessible anywhere. It has the widest scope among all other modifiers.
return: Java return keyword is used to return from a method when its execution is
complete
short: Java short keyword is used to declare a variable that can hold a 16-bit integer.
static: Java static keyword is used to indicate that a variable or method is a class method.
The static keyword in Java is mainly used for memory management.
strictfp: Java strictfp is used to restrict the floating-point calculations to ensure
portability.
super: Java super keyword is a reference variable that is used to refer to parent class
objects. It can be used to invoke the immediate parent class method.
switch: The Java switch keyword contains a switch statement that executes code based
on test value. The switch statement tests the equality of a variable against multiple values.
|. synchronized: Java synchronized keyword is used to specify the critical sections or
methods in multithreaded code.
this: Java this keyword can be used to refer the current object in a method or constructor.
throw: The Java throw keyword is used to explicitly throw an exception. The throw
keyword is mainly used to throw custom exceptions. It is followed by an instance
throws: The Java throws keyword is used to declare an exception. Checked exceptions
can be propagated with throws.
transient: Java transient keyword is used in serialization. If you define any data member
as transient, it will not be serialized.
. try: Java try keyword is used to start a block of code that will be tested for exceptions.
The try block must be followed by either catch or finally block.
>. void: Java void keyword is used to specify that a method does not have a return value.
vol
: Java volatile keyword is used to indicate that a variable may change
asynchronously.
le loop. This loop iterates a part of the
program several times, If the number of iteration is not fixed, it is recommended to use
the while loop.
muJava Control Statements
Java provides three types of control flow statements.
1. Decision Making statements
©. ifstatements
© switch statement
2. Loop statements
© dowhile loop
© while loop
© forloop
© for-each loop
3. Jump statements
© break statement
© continue statement
Der
jon-Making statements:
As the name suggests, decision-making statements decide which statement to execute and
when.
1) If Statement:
The "if" statement is used to evaluate a condition, The control of the program is diverted
depending upon the specific condition. The condition of the If statement gives a Boolean value,
either true or false. In Java, there are four types of if-statements given below.
1. Simple if statement
2. if-else statement
3. ifelse-if ladder
4, Nested if-statement
Simple if statement:
It is the most basic statement among all control flow statements in Java. It evaluates a Boolean
expression and enables the program to enter a block of code if the expression evaluates to true.
25Syntax:
if(condition)
{
statement 1; //executes when condition is true
}
if-else statement
‘The if-else statement is an extension to the if-statement, which uses another block of code, i.e.,
else block. The else block is executed if the condition of the if-block is evaluated as false.
Syntax:
if(condition)
{
statement 1; //executes when condition is true
}
else
{
statement 2; //executes when condition is false
}
3) ifelse-if ladder:
The if-else-if statement contains the if-statement followed by multiple else-if statements. It is the
chain of if-else statements that create a decision tree where the program may enter in the block
‘of code where the condition is true. We can also define an else statement at the end of the chain.
Syntax :
if(condition 1)
{
statement 1; //executes when condition 1 is true
I
else if(condition 2)
t
statement 2; //executes when condition 2 is true
26