CHAPTER
basic
ONE
Java Overview
javaint for (int i == 10;
number 0; i < 5; i++) {
An object-oriented, class-based [Link]("Iteration: " + i);
programming language that runs }
if (number > 0) {
//(initialization; condition; update)
any system with a Java Virtual [Link]("The number is positive.");
} else if (number < 0) {
Machine (responsible on compiling Switch Statement
[Link]("The – a iscontrol
number negative.");flow structure
and interpreting bytecode into } else { to execute one block of code out of
used
machine code). [Link]("The number is zero.");
multiple options.
}
E.g. Eclipse, NetBeans.
Data types
Byte – holds 8-bit signed integer.
Short – holds 16-bit
int day = 3;
Int – holds 32-bit
switch (day) {
Long – holds 64-bit, used for large integer case 1:
[Link]("Monday");
value. break;
case 2:
Float - holds 32-bit, used for decimal num [Link]("Tuesday");
break;
with single precision.
case 3:
[Link]("Wednesday");
Double – 64-bit, used for double precision. break;
case 4:
Char – 16-bit Unicode character [Link]("Thursday");
break;
case 5:
Boolean – true or false [Link]("Friday");
break;
String – sequence of characters default:
[Link]("Weekend");
break;
Array – holds fixed size collection of While
} loop – repeats a block
of code as
elements long as the given condition is ‘true’, It
checks the condition before iteration.
Looping
Input and Output
Java allows input and output operations int i = 0;
using the Scanner class for input and while (i < 5) {
[Link]("Iteration: " + i + “\t”);
[Link] for output. i++;
}
Iteration 1 Iteration 2 Iteration 3 Iteration 4
Iteration 5
import [Link]; Do-while – similar to while loop but the
public class InputOutputExample { code block(contains the code that will be
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
executed) is executed first before the loop
// Taking input body(executed statements within a loop) is
[Link]("Enter your name: "); executed at least once.
String name = [Link]();
[Link]("Hello, " + name + "!");
Conditional Statements
}
}
If-else – if the first condition evaluates to
‘true’ the code inside the ‘if’ block is
executed, if not the code inside ‘else’ block
is executed. else if can be used for multiple
int i = 0;
condition. do {
[Link]("Iteration: " + i);
i++;
} while (i < 5);
For loop – is a type of loop with a control
flow
Iteration structure
1 Iteration 2 usedIteration
to execute
3 Iterationa4 block of
code
Iteration 5
in a specific number of executions.
Nested for loop – placing one ‘for loop’
inside another and is used for multi-
dimensional
Accessing Arraydata structures or creating
Elements
pattern.
int firstElement = numbers[0]; // Accesses the first element
(10)
int lastElement = numbers[[Link] - 1]; // Accesses
the last element (50)
Array Example
for (initialization1; condition1; update1) { public class ArrayExample {
for (initialization2; condition2; update2) { public static void main(String[] args) {
//Inner loop body
} // Array declaration and initialization
// Outer loop body (can also include statements outside of int[] numbers = {10, 20, 30, 40, 50};
the inner loop)
} // Iterating through the array
[Link]("Array elements:");
for (int i = 1; i <= 3; i++) { // Outer loop for (int i = 0; i < [Link]; i++) {
for (int j = 1; j <= 3; j++) { // Inner loop [Link](numbers[i]);
Multiplication Table
[Link](i * j + "\t"); }
} int size = 10; // Size of the table (10x10)
[Link](); // Move to the next line after inner // Enhanced for loop
loop //
completes [Link]("Enhanced for loop:");
First, print the header row (1 2 3 ... 10)
} for (int number : numbers) {
[Link](" "); // For alignment with the row
[Link](number);
labels
}
for (int col = 1; col <= size; col++) {
}
[Link](col + "\t"); // Print column headers
}
}
[Link](); // Move to the next line
// Now, print the table with row labels
for (int row = 1; row <= size; row++) {
[Link](row + "| "); // Print row label
for (int col = 1; col <= size; col++) {
[Link]((row * col) + "\t"); // Print the
product
}
[Link](); // Move to the next row
}
}
}
Arrays
Array – a data structure in Java that allows
the user to store multiple values of the
same data type.
Array initialization
int[] numbers = new int[5]; // Creates an array of size 5 that
can store integers
// Assigning values to the array numbers[0] = 10;
numbers[1] = 20; numbers[2] = 30; numbers[3] = 40;
numbers[4] = 50;
// Accessing and printing values from the array
[Link](numbers[0]); // Output: 10
[Link](numbers[3]); // Output: 40
Array initialization with Values
int[] numbers = {10, 20, 30, 40, 50}; // Declares and
initializes an array with values