Java II Unit Study Material 2024
Java II Unit Study Material 2024
Convert a value from one data type to another data type is known as type casting.
casting
Types of Type Casting
There are two types of type casting:
o Widening Type Casting
o Narrowing Type Casting
Widening Type Casting:
Converting a lower data type into a higher one is called widening type casting. It is also known as implicit
conversion or casting down.. It is done automatically. It is safe because there is no chance to lose data.
o Both data types must be compatible with each other.
o The target type must be larger than the source type.
byte -> short -> char -> int -> long -> float -> double
For example, the conversion between numeric data type to char or Boolean is not done automatically.
automati Also,
the char and Boolean data types are not compatible with each other. Let's see an example.
example
WideningTypeCastingExample.java
public class WideningTypeCastingExample
{
public static void main(String[] args)
{
int x = 7;
//automatically converts the integer type into long type
long y = x;
//automatically converts the long type into float type
float z = y;
System.out.println("Before conversion, int value "+x);
System.out.println("After conversion, long value "+y);
System.out.println("After conversion, float value "+z);
}
}
OUTPUT:
Before conversion, the value is: 7
After conversion, the long value is: 7
After conversion, the float value is: 7.0
Narrowing Type Casting:
Converting a higher data type into a lower one is called narrowing type casting. It is also known as explicit
conversion or casting up. It is done manually by the programmer. If we do not perform casting then the
compiler reports a compile-time error.
double -> float -> long -> int -> char -> short -> byte
NarrowingTypeCastingExample.java
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Special Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the java language. Assume
variable A holds 10 and variable B holds 20 then −
Show Examples
Relational Operators
The following table shows all the relational operators supported by java. Assume variable A holds 10 and
variable B holds 20 then −
Show Examples
<= Checks if the value of left operand is less than or equal to the (A <= B)
value of right operand. If yes, then the condition becomes true. is true.
Logical Operators
Following table shows all the logical operators supported by java language. Assume variable A holds 1 and
variable B holds 0, then −
Show Examples
Called Logical AND operator. If both the operands are non- (A && B) is
&&
zero, then the condition becomes true. false.
Bitwise operators
Bitwise operators perform manipulations of data at bit level. These operators also perform shifting of
bits from right to left. Bitwise operators are not applied to float or double(These are datatypes, we will learn
about them in the next tutorial).
Operator Description
| Bitwise OR
^ Bitwise exclusive OR
Assignment Operators
Assignment operators supported by java language are as follows.
Operator Description Example
= assigns values from right side operands to left side operand a=b
+= adds right operand to the left operand and assign the result to left a+=b is same as a=a+b
-= subtracts right operand from the left operand and assign the result a-=b is same as a=a-b
to left operand
*= mutiply left operand with the right operand and assign the result a*=b is same as a=a*b
to left operand
/= divides left operand with the right operand and assign the result to a/=b is same as a=a/b
left operand
%= calculate modulus using two operands and assign the result to left a%=b is same as a=a%b
operand
Conditional operator
The conditional operators in java language are known by two more names
1. Ternary Operator
2. ? : Operator
It is actually the if condition that we use in java language decision making, but using conditional operator,
we turn the if condition statement into a short and simple operator.
The syntax of a conditional operator is :
Explanation:
• Dot Operator
• InstanceOf Operator
java.lang.System.out.println(("Hello");
• This operator checks the existence of an object and returns a boolean value.
• It is also called a type/object comparison operator.
operator
o it compares if the object belongs to the specified type.
• Applying the instanceof operator with any object having a null value, returns false.
• It can also identify objects of derived and inherited classes.
3. Conditional and Branching Statements
1.If Statements
The if statement is used to test the condition. It checks boolean conditions true or false. There are
various types of if statements in Java.
• Simple if statement
• if-else statement
• if-else-if ladder
• nested if statement
i) Simple if Statement :
The Java if statement tests the condition. It executes the *if block* if the condition is true.
Where boolean_expression is a condition.
Syntax:
if(Boolean_expression)
{
// Statements will execute if the Boolean expression is true
tru
}
Syntax:
if(Boolean_expression) {
// Executes when the Boolean expression is true
tru
}else {
// Executes when the Boolean expression is false
fals
}
iii) if-else-if
if ladder Statement :
The if-else-ifif ladder statement executes one condition from multiple statements.
Syntax:
if(Boolean_expression 1)
{
// Executes when the Boolean expression 1 is true
tru
}
else if(Boolean_expression 2)
{
// Executes when the Boolean expression
express 2 is true
}
else if(Boolean_expression 3)
{
// Executes when the Boolean expression 3 is true
tru
}
else
{
// Executes when the none of the above condition is true.
true
}
case value :
// Statements
break;
• For loop primarily depends on the three major sections index of the control statement which is the
"declaration", "conditions" and "updates" statement.
• This particular loop statement generates iteration over a "collection of values". That can be the same
value or can be different.
• For loop is generally used when the user knows the exact number of iterations.
• There are various types of "For loop" which are "Empty For Loop", "Infinite For Loop", and "For
each loop".
Syntax:
Example:
class ForLoopDemo
{
public static void main(String[] args)
{
for (int i=1; i<=10; i++)
{
if (i%2==0)
System.out.println(i);
}
System.out.println("Loop Ending");
}
}
• While Loop is also an iteration statement that can iterate over various types of values.
• After accepting the condition While loop evaluates it to a "boolean value".
• Execute a particular statement or a block of the statement until it generates "false".
• While loop uses when the user is not aware of the exact iteration number.
• There are various types of while loops; those are "Empty While Loop", and "Infinite While Loops".
Syntax:
//initialization
while (condition)
{
//Execute a set of statements
//increment
}
Example:
class While_Loop_Demo
{
//print even numbers ranging from 1 to 10
public static void main(String args[])
{
int i = 1; //initialization
while (i<=10) //condition or termination
{
if (i%2==0)
{
System.out.println(i);
}
i++; //increment
}
}
}
• Do while loop works the same as while loop but it only generates the expression only after the
execution of the code block has been done. The concept of Abstraction in Java can be applied to
understand the Do-While loop better
• This statement first executes the statement under "do" after that it checks the condition of "while".
• This statement executes at least once even though the statement generates a "false" value.
• Do while loop is basically an "exit control loop".
• There is only one type of “Do while loop” which is called "Infinite Do While Loop".
Syntax:
do
{
//statements to be iterated
}
while(conditions);
Example:
class Do_While_Loop_Demo
{
//print even number
public static void main(String args[])
{
int i = 1; //initialization
do
{
System.out.println(i);
i++; //increment
} while (i%2==0);//condition or termination
}
}