0% found this document useful (0 votes)
13 views11 pages

Java II Unit Study Material 2024

The document covers key concepts in Java programming, focusing on type casting, operators, conditional statements, and looping structures. It explains widening and narrowing type casting, various types of operators including arithmetic and relational, and different conditional statements like if-else and switch. Additionally, it details the three types of loops: for, while, and do-while, along with their syntax and examples.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Java II Unit Study Material 2024

The document covers key concepts in Java programming, focusing on type casting, operators, conditional statements, and looping structures. It explains widening and narrowing type casting, various types of operators including arithmetic and relational, and different conditional statements like if-else and switch. Additionally, it details the three types of loops: for, while, and do-while, along with their syntax and examples.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA II UNIT Study Material

1. Type casting in Java


type casting is a method or process that converts a data type into another data type in both ways manually
and automatically. The automatic conversion is done by the compiler and manual conversion performed by
the programmer.

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

public class NarrowingTypeCastingExample


{
public static void main(String args[])
{
double d = 166.66;
//converting double data type into long data type
long l = (long)d;
//converting long data type into int data type
int i = (int)l;
System.out.println("Before conversion: "+d);
//fractional part lost
System.out.println("After conversion into long type: "+l);
//fractional part lost
System.out.println("After conversion into int type: "+i);
}
}
Output

Before conversion: 166.66


After conversion into long type: 166
After conversion into int type: 166
2. Java Operators:
Types of Operators:

• 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

Operator Description Example

+ Adds two operands A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an integer division. B%A=0

++ Increment operator increases the integer value by one. A++ = 11

-- Decrement operator decreases the integer value by one. A-- = 9

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

Operator Description Example

== Checks if the values of two operands are equal or not. If yes, (A == B)


then the condition becomes true. is not
!= Checks if the values of two operands are equal or not. If the (A != B)
values are not equal, then the condition becomes true. is true.
> Checks if the value of left operand is greater than the value of (A > B)
right operand. If yes, then the condition becomes true. is not
true.
< Checks if the value of left operand is less than the value of right (A < B)
operand. If yes, then the condition becomes true. is true.
>= Checks if the value of left operand is greater than or equal to the (A >= B)
value of right operand. If yes, then the condition becomes true. is not
true.

<= 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

Operator Description Example

Called Logical AND operator. If both the operands are non- (A && B) is
&&
zero, then the condition becomes true. false.

Called Logical OR Operator. If any of the two operands is (A || B) is true.


||
non-zero, then the condition becomes true.

Called Logical NOT Operator. It is used to reverse the !(A && B) is


! logical state of its operand. If a condition is true, then Logical true.
NOT operator will make it 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 AND

| Bitwise OR

^ Bitwise exclusive OR

<< left shift

>> right shift

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 :

expression 1 ? expression 2: expression 3

Explanation:

• The question mark "?" in the syntax represents the if part.


• The first expression (expression 1) generally returns either true or false, based on which it is decided
whether (expression 2) will be executed or (expression 3)
• If (expression 1) returns true then the expression on the left side of " : " i.e (expression 2) is
executed.
• If (expression 1) returns false then the expression on the right side of " : " i.e (expression 3) is
executed.
Special Operators:
Java also supports some special type of operators like:

• Dot Operator
• InstanceOf Operator

Dot Operator (.):

• Dot operator helps to access::


o variable/constant
o method
o Class
o package
o For Example:

java.lang.System.out.println(("Hello");

The instanceof Operator:

• 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
}

ii) if-else Statement :


The Java if-else
else statement also tests the condition. It executes the if block, if the condition is true
otherwise else block, is executed.

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
}

iv) nested if statement :


The nested if statement represents the if block within
within another if block. Here, the inner if block
condition executes only when the outer if block condition is
Syntax:
switch(expression)
{
case value :
// Statements
break;

case value :
// Statements
break;

// You can have any number of case statements.


default : // Optional
// Statements
}
4. Looping statements in Java
Types of Loops in Java
There are 3 types of "Loops" in Java. These are "while loop", "do while loop", and "for loop"

1. For loop in Java

• 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:

for (initialization; termination condition; increment/decrement)


{
//Set of statements to be executed repeatedly
}

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");

}
}

2. While loops in Java

• 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
}
}
}

3. Do..while loops in Java

• 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

}
}

You might also like