Java Programming - Chapter 1
Introduction to Java Programming
Instructor Name
Date
Introduction to Java Programming
• This chapter covers the following topics:
• - Data Types and Variables
• - Arrays
• - Decision and Repetition Statements
• - Exception Handling
Java: A Brief Overview
• Java is a versatile, object-oriented
programming language used in various
applications.
• It follows the principle of 'write once, run
anywhere.'
Data Types in Java
• Java supports various data types, including:
• - Primitive types (e.g., int, float, char)
• - Non-primitive types (e.g., Arrays, Strings)
Variables in Java
• Java supports various data types, including:
• - Primitive types (e.g., int, float, char)
• - Non-primitive types (e.g., Arrays, Strings)
• Example:
• int num = 10;
• double price = 19.99;
• char grade = 'A';
Primitive Data Types
• Variables store data in Java.
• Example:
• - int age = 25;
• - String name = "John";
• Variables must be declared before use.
Non-Primitive Data Types
• Non-primitive data types include:
• - Arrays
• - Classes
• - Interfaces
Type Casting in Java
• Type casting allows you to convert one data
type into another.
• - Implicit casting (automatic)
• - Explicit casting (manual)
Arrays in Java
• An array is a container object that holds a
fixed number of values of a single type.
• Example: int[] arr = new int[5];
One-Dimensional Arrays
• An array stores multiple values of the same
type.
• Example (One-Dimensional):
• int[] numbers = {10, 20, 30, 40};
• // Accessing array element at index 2:
• numbers[2] = 100; // Changing value to 100
• // Loop through array and print elements:
Multi-Dimensional Arrays
• One-dimensional arrays store a list of
elements.
• Example:
• int[] arr = {10, 20, 30, 40};
• // Accessing elements:
• System.out.println(arr[0]); // Output: 10
• // Loop through the array and print values:
• for (int i = 0; i < arr.length; i++) {
Array Initialization
• Multi-dimensional arrays store data in a grid
format.
• Example:
• int[][] grid = {{1, 2}, {3, 4}, {5, 6}};
• // Accessing element at row 1, column 0:
• System.out.println(grid[1][0]); // Output: 3
• // Loop through the 2D array:
• for (int i = 0; i < grid.length; i++) {
Accessing Array Elements
• Array elements are accessed using an index:
• Example: int firstElement = arr[0];
Iterating over Arrays
• Array elements are accessed using an index.
• Example:
• int[] arr = {1, 2, 3, 4};
• int firstElement = arr[0]; // firstElement = 1
• // Loop to print each element:
• for (int i = 0; i < arr.length; i++) {
• System.out.println(arr[i]); // Prints each
element
Decision Statements in Java
• Decision-making statements are used to
perform different actions based on conditions.
• - if, if-else, switch
If-Else Statements
• Decision-making statements include if-else
and switch.
• Example (if-else):
• if (x > 10) {
• System.out.println("x is greater than 10");
• } else {
• System.out.println("x is less than or equal to
10");
• }
Switch Statement
• The if-else statement checks a condition.
• Example:
• int x = 20;
• if (x < 10) {
• System.out.println("x is less than 10");
• } else {
• System.out.println("x is 10 or greater");
• }
Repetition Statements in Java
• The switch statement checks a variable against
several possible values.
• Example:
• int day = 3;
• switch (day) {
• case 1: System.out.println("Monday");
break;
• case 2: System.out.println("Tuesday");
break;
For Loop
• Loops are used to repeat a block of code.
• Example (for loop):
• for (int i = 0; i < 5; i++) {
• System.out.println(i);
• }
While Loop
• For loop repeats a block of code a fixed
number of times.
• Example:
• for (int i = 0; i < 5; i++) {
• System.out.println("i = " + i); // Prints i from
0 to 4
• }
Do-While Loop
• While loop repeats code while a condition is
true.
• Example:
• int i = 0;
• while (i < 5) {
• System.out.println(i); // Prints i from 0 to 4
• i++;
• }
Nested Loops
• Do-while loop executes the code before
checking the condition.
• Example:
• do {
• System.out.println("i = " + i); // Prints i from
0 to 4
• i++;
• } while (i < 5);
Introduction to Exception Handling
• Exception handling helps manage runtime
errors.
• It provides a way to gracefully handle errors
using try-catch blocks.
Syntax of Exception Handling
• Exception handling is used to manage runtime
errors.
• Example:
• try {
• int result = 10 / 0; // This will throw an
exception
• } catch (ArithmeticException e) {
• System.out.println("Error: " +
e.getMessage()); // Handle error
Conclusion and Summary
• Syntax for exception handling:
• try { ... }
• catch (Exception e) { ... }
• throw new Exception();
• finally { ... }
• Example:
• try {
• // Code
More Examples of Data Types
• Example with float and boolean:
• float pi = 3.14f;
• boolean isValid = true;
• Floating-point operations:
• float result = pi * 2.0f;
More Examples of Variables
• Example of multiple variable declarations:
• int a = 5, b = 10;
• String firstName = "John";
• char grade = 'A';
More Array Examples - One-
Dimensional
• Example with array manipulation:
• int[] numbers = {10, 20, 30, 40};
• Changing value:
• numbers[2] = 100;
• Loop through array:
• for (int num : numbers) {
• System.out.println(num);
More Array Examples - Multi-
Dimensional
• 2D Array example:
• int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};
• Accessing an element:
• System.out.println(matrix[1][0]); // Output: 3
Array Operations - Sum of
Elements
• Example of summing elements in an array:
• int[] arr = {1, 2, 3, 4};
• int sum = 0;
• for (int num : arr) {
• sum += num;
• }
• System.out.println(sum); // Output: 10
More Examples of Decision
Statements (if-else)
• Example with nested if-else:
• int x = 5;
• if (x > 10) {
• System.out.println("x is greater than 10");
• } else if (x == 5) {
• System.out.println("x is equal to 5");
• } else {
• System.out.println("x is less than 5");
• }
Switch Statement - More Examples
• Switch example for days of the week:
• int day = 3;
• switch (day) {
• case 1: System.out.println("Monday");
break;
• case 2: System.out.println("Tuesday");
break;
• default: System.out.println("Weekend");
break;
More Examples of For Loop
• For loop example with array:
• int[] numbers = {1, 2, 3, 4};
• for (int i = 0; i < numbers.length; i++) {
• System.out.println(numbers[i]);
• }
More Examples of While Loop
• While loop example to find factorial of a
number:
• int number = 5;
• int factorial = 1;
• while (number > 0) {
• factorial *= number;
• number--;
• }
• System.out.println(factorial);
More Examples of Do-While Loop
• Do-while loop example with user input:
• int count = 0;
• do {
• count++;
• } while (count < 5);
• System.out.println(count);
Nested Loops - More Examples
• Example of nested loops (printing a matrix):
• int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};
• for (int i = 0; i < matrix.length; i++) {
• for (int j = 0; j < matrix[i].length; j++) {
• System.out.print(matrix[i][j] + " ");
• }
• System.out.println();
• }
Exception Handling - Try-Catch with
Multiple Exceptions
• Example of handling multiple exceptions:
• try {
• int result = 10 / 0;
• } catch (ArithmeticException e) {
• System.out.println("Arithmetic Exception: "
+ e.getMessage());
• } catch (Exception e) {
• System.out.println("General Exception: " +
e.getMessage());
Exception Handling - Finally Block
Example
• Example using finally block:
• try {
• int[] arr = {1, 2, 3};
• System.out.println(arr[3]);
• } catch (ArrayIndexOutOfBoundsException e) {
• System.out.println("Exception occurred");
• } finally {
• System.out.println("This will always
execute");
Type Casting - Implicit and Explicit
• Implicit casting (auto):
• int x = 10;
• double y = x; // no need to cast explicitly
• Explicit casting (manual):
• double a = 3.14;
• int b = (int) a;
Working with Strings
• Implicit casting (auto):
• int x = 10;
• double y = x; // x is automatically cast to
double
• Explicit casting (manual):
• double a = 3.14;
• int b = (int) a; // Manual cast from double to
int
String Methods Example
• Example of String concatenation:
• String firstName = "John";
• String lastName = "Doe";
• String fullName = firstName + " " +
lastName; // Concatenate strings
• System.out.println(fullName);
Final Example of Loops
• Common string methods:
• - length(), charAt(), substring()
• Example:
• String name = "Java";
• System.out.println(name.length()); // Output:
4
• System.out.println(name.charAt(0)); //
Output: 'J'
Debugging Example with Loops
• Debugging a loop for sum of odd numbers:
• int sum = 0;
• for (int i = 1; i <= 99; i += 2) {
• sum += i;
• }
• System.out.println(sum);
Common Errors in Java and How to
Avoid Them
• Examples of common errors:
• - ArrayIndexOutOfBoundsException
• - NullPointerException
• - Syntax errors
• Best practices: Always check array length and
object initialization.
Common Array Errors and
Solutions
• ArrayIndexOutOfBoundsException: Happens
when accessing an index outside array
bounds.
• Solution: Always check array size before
accessing.
Using Break and Continue in Loops
• Using break:
• for (int i = 0; i < 10; i++) {
• if (i == 5) break;
• System.out.println(i);
• }
• Using continue:
• for (int i = 0; i < 10; i++) {
• if (i == 5) continue;
• System.out.println(i);
Review of Key Concepts in Chapter
1
• Key concepts covered:
• - Data Types and Variables
• - Arrays
• - Loops (for, while, do-while)
• - Decision Statements (if, switch)
• - Exception Handling
Final Example - Complete Java
Program
• Key concepts covered:
• - Data Types and Variables
• - Arrays
• - Loops (for, while, do-while)
• - Decision Statements (if, switch)
• - Exception Handling
• // Reviewing and understanding each concept
is crucial for mastering Java.
Recap and Conclusion of Chapter 1
• In this chapter, we explored:
• - Data types
• - Arrays
• - Decision making
• - Loops
• - Exception handling
• Final thoughts: Practice is key to mastering
Java!
Questions and Discussion
• In this chapter, we explored:
• - Data types
• - Arrays
• - Decision making
• - Loops
• - Exception handling
• // Practice coding examples to reinforce your
understanding.