0% found this document useful (0 votes)
2 views

java1

The document explains implicit and explicit data type conversion in programming, highlighting that implicit conversion is automatic and safe, while explicit conversion requires manual intervention and may lead to data loss. It also covers various control flow statements in Java, including if-else, switch, and for-each loops, providing syntax and examples for each. Additionally, it discusses the characteristics and limitations of the for-each loop, emphasizing its ease of use for simple iterations.

Uploaded by

Sairam Nemmoju
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java1

The document explains implicit and explicit data type conversion in programming, highlighting that implicit conversion is automatic and safe, while explicit conversion requires manual intervention and may lead to data loss. It also covers various control flow statements in Java, including if-else, switch, and for-each loops, providing syntax and examples for each. Additionally, it discusses the characteristics and limitations of the for-each loop, emphasizing its ease of use for simple iterations.

Uploaded by

Sairam Nemmoju
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Implicit and Explicit Data Type Conversion

Implicit Type Conversion (Type Casting)

 Definition: Performed automatically by the compiler when converting a value from one
data type to another.
 Key Points:
o No explicit instruction from the programmer is needed.
o Converts a smaller data type to a larger one (e.g., int to float) to prevent data loss.
o Commonly referred to as type promotion.
 Examples:
 int a = 10;
 float b = a; // `int` is implicitly converted to `float`

Explicit Type Conversion (Type Casting)

 Definition: Performed manually by the programmer using a casting operator to specify


the conversion.
 Key Points:
o Used to force a conversion even when it may lead to data loss or require a type
downgrade.
o Requires explicit syntax, such as (type) expression.
o Useful for narrowing conversions (e.g., float to int).
 Examples:
 float a = 5.8;
 int b = (int)a; // Explicitly converting `float` to `int`

Differences:

Aspect Implicit Conversion Explicit Conversion


Initiated By Compiler Programmer
Risk of Data Loss Minimal Possible
Ease of Use Automatic Requires explicit instruction
Syntax No additional syntax required Requires (type) keyword

Usage Considerations:

 Use implicit conversion for safe type promotion.


 Use explicit conversion when control over the data type is essential, understanding
potential risks like precision loss or truncation.
If we want only one
word then use
sc.next();

If we want a set of
words including
spaces also then use

Sc.nextLine();

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your age ra babu: ");
int age = sc.nextInt();
System.out.println("Nee age idigo ra babu " + age);
}
}

1. if-else Statement
 Syntax:
Java
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
 Example:
Java
int age = 25;

if (age >= 18) {


System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
2. if-else if-else Statement1
 Syntax:
Java
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else if (condition3) {
// Code to execute if condition1 and condition2 are false and condition3 is true
}
// ... more else if blocks as needed
else {
// Code to execute if none of the above conditions are true
}
 Example:
Java
int grade = 85;

if (grade >= 90) {


System.out.println("Excellent!");
} else if (grade >= 80) {
System.out.println("Great job!");
} else if (grade >= 70) {
System.out.println("Good work!");
} else {
System.out.println("Needs improvement.");
}
3. Nested if-else Statement
 Syntax: An if-else statement within another if-else statement.
 Example:
Java
int age = 20;
char gender = 'F';

if (age >= 18) {


if (gender == 'M') {
System.out.println("Adult Male");
} else {
System.out.println("Adult Female");
}
} else {
System.out.println("Minor");
}
4. Working with Logical Operators
 Logical Operators:
o && (AND): Both conditions must be true for the entire expression to be true.
o || (OR): At least one of the conditions must be true for the entire expression to be
true.
o ! (NOT): Negates the condition.
 Example:
Java
int age = 25;
boolean isStudent = true;
if (age >= 18 && isStudent) {
System.out.println("You are eligible for student discounts.");
}
5. Java Ternary Operator
 Syntax: condition ? value_if_true : value_if_false
 Example:
Java
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status);
6. Java Switch Statement
 Syntax:
Java
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
// ... more cases
default:
// Code to execute if none of the cases match
}
 Example:
Java
int dayOfWeek = 3;

switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
.
EXAMPLE for “continue”

in this example when I becomes equal to 2


whatever there below that condition that will be skipped and remaining loop continues as it is

……………………………………………………………………………………
FOR EACH LOOP IN JAVA IS MORE LIKE ACCESSING EACH
ELEMENT OF AN ARRAY SEQUENTIALLY
Java for-each Loop
The for-each loop in Java, also known as the enhanced for loop, is a simplified loop
structure designed to iterate through elements of arrays or collections. It is particularly
useful when you need to access every element sequentially without worrying about the
index.

Syntax:
for (dataType element : arrayOrCollection) {
// body of loop
}

 dataType: The type of elements stored in the array.

 element: A temporary variable that holds the current element being accessed.

 arrayOrCollection: The array (or collection) being iterated over.

Key Points:
1. Ease of Use: The for-each loop eliminates the need for managing indices and reduces errors
like ArrayIndexOutOfBoundsException.

1. Read-Only Access: The element variable is a copy of the array element. Modifications to
element do not affect the original array.

2. Traversal Order: Iterates sequentially from the first to the last element.

Example with Arrays:


public class ForEachExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int num : numbers) {
System.out.println(num); // Prints each element
}
}
}

Output:
10
20
30
40
50
Characteristics:
1. Applicable to All Array Types: Works for arrays of primitive types (e.g., int, double) and
reference types (e.g., String, custom objects).

1. Immutable Access: You can read elements, but cannot modify them in the original array.

Example with Object Arrays:


public class ForEachObject {
public static void main(String[] args) {
String[] names = {"Alice", "Bob", "Charlie"};
for (String name : names) {
System.out.println(name.toUpperCase()); // Prints the
uppercase form
}
}
}

Output:
ALICE
BOB
CHARLIE

Limitations:
1. No Access to Index: If you need the index, use a traditional for loop. Example: for (int i
= 0; i < array.length; i++).

1. Forward Iteration Only: You cannot iterate backward or skip elements.

2. Cannot Directly Modify Array Elements: Example:

 int[] nums = {1, 2, 3};


for (int n : nums) {
n = n * 2; // This does NOT change the array
}

When to Use:
 Use for-each for simple iteration where you don’t need to modify the array or know the
index.

 Use traditional loops when you need to modify elements, require the index, or want to iterate in
reverse.
For (int number : numbers)
{
// body of for each loop
}
// we are saying like for every element(number) of array(numbers) do the below work(the
work we assign in body of for each loop)
Actually this is how a 2d array looks
but we have made it like a box rows
Multi dimensional array and columns for our easy
visualization
For above picture we are printing row 2: column 1 marks which is 98.
Functions && methods

You might also like