2 Lecture2
2 Lecture2
Lecture2:
• Data Types in Java
• Variables in Java
• Arrays
• Operators
• Control Statements
• Input and output
• Scanner and System class
• print(),println(), and printf() methods
Data Types in Java
• Definition: Specifies different sizes and values
for storing variables.
• Importance: Ensures efficient memory use
and type checks at compile time.
• Use: Declaring variables and managing data in
a program.
Primitive Data Types
Data type Range of values
// Bitwise AND
int andResult = a & b; // 0101 & 0011 = 0001
System.out.println("a & b = " + andResult); // Output: 1
// Bitwise OR
int orResult = a | b; // 0101 | 0011 = 0111
System.out.println("a | b = " + orResult); // Output: 7
// Bitwise XOR
int xorResult = a ^ b; // 0101 ^ 0011 = 0110
System.out.println("a ^ b = " + xorResult); // Output: 6
// Bitwise NOT
int notResult = ~a; // ~0101 = 1010 (in 2's complement, -6)
System.out.println("~a = " + notResult); // Output: -6
5. Assignment Operators
• Assignment operators are used to assign values to variables. The most
common assignment operator is =. There are also compound assignment
operators that combine a binary operation with assignment.
• Simple Assignment (=): Assigns the right-hand side value to the left-hand
side variable.
• Addition Assignment (+=): Adds the right-hand side value to the left-hand
side variable and assigns the result.
• Subtraction Assignment (-=): Subtracts the right-hand side value from
the left-hand side variable and assigns the result.
• Multiplication Assignment (*=): Multiplies the left-hand side variable by
the right-hand side value and assigns the result.
• Division Assignment (/=): Divides the left-hand side variable by the right-
hand side value and assigns the result.
• Modulus Assignment (%=): Computes the modulus of the left-hand side
variable by the right-hand side value and assigns the result.
• Example:
• public class AssignmentOperatorsExample {
• public static void main(String[] args) {
• int a = 10;
•
• // Simple Assignment
• int b = a;
• System.out.println("b = " + b); // Output: 10
•
• // Addition Assignment
• a += 5; // a = a + 5
• System.out.println("a += 5 -> a = " + a); // Output: 15
•
• // Subtraction Assignment
• a -= 3; // a = a - 3
• System.out.println("a -= 3 -> a = " + a); // Output: 12
•
• // Multiplication Assignment
• a *= 2; // a = a * 2
• System.out.println("a *= 2 -> a = " + a); // Output: 24
•
• // Division Assignment
• a /= 4; // a = a / 4
• System.out.println("a /= 4 -> a = " + a); // Output: 6
•
• // Modulus Assignment
• a %= 5; // a = a % 5
• System.out.println("a %= 5 -> a = " + a); // Output: 1
• }
• }
Control Statements in Java
• Definition: Control the flow of execution
based on conditions.
• Importance: Allow conditional execution and
repetitive tasks, making programs dynamic
and responsive.
• Use: Implement logic involving decision
making and repetitive tasks.
Cont…
• These statements can be classified into three categories:
1. Selection Statements (Conditional Statements)
2. Iteration Statements (Looping Statements)
3. Jump Statements
Cont…
1. Selection Statements
Selection statements allow the program to choose different paths of execution based on the
outcome of an expression or condition.
if Statement
The if statement evaluates a boolean expression and executes a block of code if the
expression is true.
int num = 10;
if (num > 5) {
System.out.println("Number is greater than 5");
}
if-else Statement
The if-else statement provides a secondary path of execution when an if clause
evaluates to false.
int num = 3;
if (num > 5) {
System.out.println("Number is greater than 5");
} else {
System.out.println("Number is not greater than 5");
}
One-way if Statements
if (radius >= 0) {
area = radius * radius * PI;
if (boolean-expression) { System.out.println("The area"
statement(s);
} + " for the circle of radius "
+ radius + " is " + area);
}
36
The Two-way if Statement
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
37
Cont..
• if-else-if Ladder
• This allows multiple conditions to be checked in sequence.
• int num = 10;
• if (num == 1) {
• System.out.println("Number is one");
• } else if (num == 10) {
• System.out.println("Number is ten");
• } else {
• System.out.println("Number is neither one nor ten");
• }
Multi-Way if-else Statements
39
Multiple Alternative if Statements
(a) (b)
40
Cont…
• switch Statement
• The switch statement allows a variable to be tested for equality against a list of values.
• int day = 3;
• switch (day) {
• 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("Other day");
• break;
• }
2. Iteration Statements
• Iteration statements allow blocks of code to be executed repeatedly based on a
condition.
• for Loop
• The for loop is used when the number of iterations is known.
• for (int i = 0; i < 5; i++) {
• System.out.println("Value of i: " + i);
• }
• while Loop
• The while loop is used when the number of iterations is not known and the loop
needs to run until a condition is false.
• int i = 0;
• while (i < 5) {
• System.out.println("Value of i: " + i);
• i++;
• }
Cont..
• do-while Loop
• The do-while loop is similar to the while loop but it guarantees the block of
code will be executed at least once.
• int i = 0;
• do {
• System.out.println("Value of i: " + i);
• i++;
• } while (i < 5);
3. Jump Statements
• Jump statements are used to transfer control to another part of the program.
• break Statement
• The break statement is used to exit from a loop or switch statement.
• for (int i = 0; i < 10; i++) {
• if (i == 5) {
• break; // exits the loop when i is 5
• }
• System.out.println("Value of i: " + i);
• }
Cont…
• continue Statement
• The continue statement is used to skip the current iteration of a loop and continue with
the next iteration.
• for (int i = 0; i < 10; i++) {
• if (i == 5) {
• continue; // skips the rest of the loop when i is 5
• }
• System.out.println("Value of i: " + i);
• }
• Output
• Value of i: 0
• Value of i: 1
• Value of i: 2
• Value of i: 3
• Value of i: 4
• Value of i: 6
• Value of i: 7
• Value of i: 8
• Value of i: 9
Input and Output in Java
• Definition: Reading data from an input source
and writing data to an output destination.
• **Input:** Receiving data from the user.
• - **Output:** Displaying data to the user.
• - **Streams:** Java uses streams to perform input
and output operations.
• Importance: Important for interacting with users
and handling data input/output operations.
• Use: Handle data input from users, files, and
other sources; output data to the console, files,
or other destinations.
Example - Input and Output
import java.util.Scanner;
public class InputOutputDemo {
public static void main(String[] args) {
Scanner input1 = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input1.nextLine();
System.out.println("Hello " + name + "!");
input1.close(); Explanation: Uses the Scanner
} class to read input from the
user and prints a greeting
} message.
Output:
Output:
Hello World
Hello World
My name is John and I am 25 years old.
Cont…
• Common Format Specifiers for printf()
• %d: Decimal integer
• %f: Floating-point number
• %s: String
• %c: Character
• %x: Hexadecimal integer
• %o: Octal integer
• %b: Boolean
Example of how printf() is used:
public class Main {
public static void main(String[] args) {
int age = 25;
String name = "Alice";
double score = 95.5;