JAVA PROGRAMMING
Title Goes Here UNIT - 1
Operators, Control Statement, Array
Operators in Java
• Operator in Java
• is a symbol that is used to perform operations. For example: +, -, *, / etc.
• There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
Java Operator Precedence
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%
additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<=
>>= >>>=
Java Unary Operator
• The Java unary operators require only one operand. Unary operators
are used to perform various operations i.e.
• incrementing/decrementing a value by one
• negating an expression
• inverting the value of a boolean
Java Unary Operator Example: ~ and !
Java Arithmetic Operators
• Java arithmetic operators are used to perform addition, subtraction,
multiplication, and division. They act as basic mathematical
operations.
Java Left Shift Operator
• The Java left shift operator << is used to shift all of the bits in a value
to the left side of a specified number of times.
Java Right Shift Operator
• The Java right shift operator >> is used to move the value of the left
operand to right by the number of bits specified by the right operand.
Java AND Operator Example: Logical && and Bitwise &
• The logical && operator doesn't check the second condition if the first
condition is false. It checks the second condition only if the first one is
true.
• The bitwise & operator always checks both conditions whether first
condition is true or false.
Java AND Operator Example: Logical && vs Bitwise &
Java OR Operator Example: Logical || and Bitwise |
• The logical || operator doesn't check the second condition if the first
condition is true. It checks the second condition only if the first one is
false.
• The bitwise | operator always checks both conditions whether first
condition is true or false.
Java Ternary Operator
• Java Ternary operator is used as one line replacement for if-then-else
statement and used a lot in Java programming. It is the only
conditional operator which takes three operands.
Java Math Methods
• The [Link] class contains various methods for performing basic
numeric operations such as the logarithm, cube root, and trigonometric
functions etc.
Method Description
[Link]() It will return the Absolute value of the given value.
[Link]() It returns the Largest of two values.
[Link]() It is used to return the Smallest of two values.
[Link]() It is used to round of the decimal numbers to the nearest value.
[Link]() It is used to return the square root of a number.
[Link]() It is used to return the cube root of a number.
[Link]() It returns the value of first argument raised to the power to second
argument.
[Link]() It is used to find the sign of a given value.
[Link]() It is used to find the smallest integer value that is greater than or equal to
the argument or mathematical integer.
[Link]() It is used to find the Absolute value of first argument
along with sign specified in second argument.
[Link]() It is used to return the floating-point number adjacent
to the first argument in the direction of the second
argument.
[Link]() It returns the floating-point value adjacent to d in the
direction of positive infinity.
[Link]() It returns the floating-point value adjacent to d in the
direction of negative infinity.
[Link]() It is used to find the largest integer value which is
less than or equal to the argument and is equal to the
mathematical integer of a double value.
[Link]() It is used to find the largest integer value that is less
than or equal to the algebraic quotient.
[Link]() It returns a double value with a positive sign, greater
than or equal to 0.0 and less than 1.0.
[Link]() It returns the double value that is closest to the given argument and equal to mathematical integer.
[Link]() It returns sqrt(x2 +y2) without intermediate overflow or underflow.
[Link]() It returns the size of an ulp of the argument.
[Link]() It is used to return the unbiased exponent used in the representation of a value.
[Link]() It is used to calculate the remainder operation on two arguments as prescribed by the IEEE 754
standard and returns value.
[Link]() It is used to return the sum of its arguments, throwing an exception if the result overflows an int or
long.
[Link]() It returns the difference of the arguments, throwing an exception if the result overflows an int.
[Link]() It is used to return the product of the arguments, throwing an exception if the result overflows
an int or long.
[Link]( It returns the argument incremented by one, throwing an exception if the result overflows an int.
)
[Link] It is used to return the argument decremented by one, throwing an exception if the result overflows
() an int or long.
[Link]() It is used to return the negation of the argument, throwing an exception if the result overflows
an int or long.
[Link]() It returns the value of the long argument, throwing an exception if the value overflows an int.
Logarithmic Math Methods
Method Description
[Link]() It returns the natural logarithm of
a double value.
Math.log10() It is used to return the base 10 logarithm of
a double value.
Math.log1p() It returns the natural logarithm of the sum of
the argument and 1.
[Link]() It returns E raised to the power of
a double value, where E is Euler's number and it
is approximately equal to 2.71828.
Math.expm1() It is used to calculate the power of E and
subtract one from it.
Trigonometric Math Methods
Method Description
[Link]() It is used to return the trigonometric Sine value of a Given double value.
[Link]() It is used to return the trigonometric Cosine value of a Given double value.
[Link]() It is used to return the trigonometric Tangent value of a Given double value.
[Link]() It is used to return the trigonometric Arc Sine value of a Given double value
[Link]() It is used to return the trigonometric Arc Cosine value of a Given double value.
[Link]() It is used to return the trigonometric Arc Tangent value of a Given double
value.
Control Statements
Java Control Statements | Control Flow in Java
Java compiler executes the code from top to bottom. The statements in
the code are executed according to the order in which they appear.
However, Java provides statements that can be used to control the flow
of Java code. Such statements are called control flow statements. It is
one of the fundamental features of Java, which provides a smooth flow
of program.
Java provides three types of control
flow statements.
Decision Making statements
if statements
switch statement
Loop statements
do while loop
while loop
for loop
for-each loop
Jump statements
break statement
continue statement
Decision-Making statements:
• As the name suggests, decision-making statements decide which
statement to execute and when. Decision-making statements evaluate
the Boolean expression and control the program flow depending upon
the result of the condition provided. There are two types of decision-
making statements in Java, i.e., If statement and switch statement.
• 1) If Statement:
• In Java, 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.
Types of IF statement
• Simple if statement
• if-else statement
• if-else-if ladder
• Nested if-statement
1) 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.
• Syntax of if statement is given below.
•
2) 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.
3) if-else-if ladder:
• The if-else-if statement contains the if-statement followed by multiple
else-if statements. In other words, we can say that 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.
4. Nested if-statement
• In nested if-statements, the if statement can contain a if or if-
else statement inside another if or else-if statement.
Switch Statement:
• In Java, Switch statements are similar to if-else-if statements. The switch
statement contains multiple blocks of code called cases and a single case is
executed based on the variable which is being switched. The switch statement is
easier to use instead of if-else-if statements. It also enhances the readability of
the program.
• The case variables can be int, short, byte, char, or enumeration. String type is also
supported since version 7 of Java
• Cases cannot be duplicate
• Default statement is executed when any of the case doesn't match the value of
expression. It is optional.
• Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
• While using switch statements, we must notice that the case expression will be of
the same type as the variable. However, it will also be a constant value.
Loops in Java
• Simple for Loop
• For-each or Enhanced for Loop
• Labeled for Loop
Java Simple for Loop
• A simple for loop is the same as C/C++. We can initialize the variable, check
condition and increment/decrement value. It consists of four parts:
• Initialization: It is the initial condition which is executed once when the loop
starts. Here, we can initialize the variable, or we can use an already initialized
variable. It is an optional condition.
• Condition: It is the second condition which is executed each time to test the
condition of the loop. It continues execution until the condition is false. It must
return boolean value either true or false. It is an optional condition.
• Increment/Decrement: It increments or decrements the variable value. It is an
optional condition.
• Statement: The statement of the loop is executed each time until the second
condition is false.
Java Nested for Loop
• If we have a for loop inside another loop, it is known as nested for
loop. The inner loop executes completely whenever outer loop
executes.
Java for-each Loop
• The for-each loop is used to traverse array or collection in Java. It is
easier to use than simple for loop because we don't need to
increment value and use subscript notation.
• It works on the basis of elements and not the index. It returns
element one by one in the defined variable.
Java Labeled For Loop
• We can have a name of each Java for loop. To do so, we use label
before the for loop. It is useful while using the nested for loop as we
can break/continue specific for loop.
• Note: The break and continue keywords breaks or continues the
innermost for loop respectively.
Java Infinitive for Loop
• If you use two semicolons ;; in the for loop, it will be infinitive for
loop.
Java While Loop
• The Java while loop is used to iterate a part of the program repeatedly
until the specified Boolean condition is true. As soon as the Boolean
condition becomes false, the loop automatically stops.
• The while loop is considered as a repeating if statement. If the
number of iteration is not fixed, it is recommended to use the while
loop.
• Condition: It is an expression which is tested. If the condition is true, the
loop body is executed and control goes to update expression. When the
condition becomes false, we exit the while loop.
• Example:
• i <=100
• Update expression: Every time the loop body is executed, this
expression increments or decrements loop variable.
• Example:
• i++;
Java Infinitive While Loop
• If you pass true in the while loop, it will be infinitive while loop.
Java do-while Loop
• The Java do-while loop is used to iterate a part of the program
repeatedly, until the specified condition is true. If the number of
iteration is not fixed and you must have to execute the loop at least
once, it is recommended to use a do-while loop.
• Java do-while loop is called an exit control loop. Therefore, unlike while
loop and for loop, the do-while check the condition at the end of loop
body. The Java do-while loop is executed at least once because condition
is checked after loop body.
• Condition: It is an expression which is tested. If the condition is true, the
loop body is executed, and control goes to update expression. As soon as
the condition becomes false, loop breaks automatically.
• Example:
• i <=100
• Update expression: Every time the loop body is executed, the this
expression increments or decrements loop variable.
• Example:
• i++;
• The do block is executed at least once, even if the condition is false.
Java Infinitive do-while Loop
• If you pass true in the do-while loop, it will be infinitive do-while loop.
Java Break Statement
• When a break statement is encountered inside a loop, the loop is
immediately terminated, and the program control resumes at the next
statement following the loop.
• The Java break statement is used to break loop or switch statement. It
breaks the current flow of the program at specified condition. In case of
inner loop, it breaks only inner loop.
• We can use Java break statement in all types of loops such as for loop,
while loop and do-while loop.
Java Continue Statement
• The continue statement is used in loop control structure when you need
to jump to the next iteration of the loop immediately. It can be used
with for loop or while loop.
• The Java continue statement is used to continue the loop. It continues
the current flow of the program and skips the remaining code at the
specified condition. In case of an inner loop, it continues the inner loop
only.
• We can use Java continue statement in all types of loops such as for
loop, while loop and do-while loop.