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

Day 1

The document provides an overview of Java programming concepts, including the roles of JVM, JRE, and JDK, as well as identifiers, variables, literals, primitive data types, type conversion, and control structures like conditional statements and loops. It also covers array types, operations, and limitations, emphasizing the fixed size of arrays and their inability to perform dynamic operations. Examples are provided throughout to illustrate these concepts.

Uploaded by

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

Day 1

The document provides an overview of Java programming concepts, including the roles of JVM, JRE, and JDK, as well as identifiers, variables, literals, primitive data types, type conversion, and control structures like conditional statements and loops. It also covers array types, operations, and limitations, emphasizing the fixed size of arrays and their inability to perform dynamic operations. Examples are provided throughout to illustrate these concepts.

Uploaded by

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

1.

JVM, JRE, and JDK

 JVM (Java Virtual Machine):

o It is a runtime environment responsible for running Java bytecode.

o It converts bytecode into machine-specific code.

o Includes components like the class loader, memory management, and garbage
collector.

 JRE (Java Runtime Environment):

o A subset of the JDK that includes the JVM and standard libraries.

o Used to run Java applications but cannot be used to develop them.

 JDK (Java Development Kit):

o A superset of JRE that includes tools for development like the compiler (javac),
debugger, and libraries.

o Used to develop and run Java applications.

Example to Compile and Run:

// Save this code in Example.java

public class Example {

public static void main(String[] args) {

System.out.println("Hello, JVM!");

// Compile: javac Example.java (JDK)

// Run: java Example (JVM via JRE)

2. Identifiers, Variables, and Literals

 Identifiers: Names for variables, methods, classes, etc.

o Rules:

 Must begin with a letter, $, or _.

 Cannot use reserved keywords.

 Variables: Containers for storing data.


o Types:

 Local variables

 Instance variables

 Static variables

 Literals: Fixed values assigned to variables.

o Examples: 10, "Hello", true, 3.14.

Example:

public class VariablesExample {

public static void main(String[] args) {

int age = 25; // Variable

String name = "Alice"; // Literal

System.out.println(name + " is " + age + " years old.");

3. Primitive Data Types

Java has 8 primitive types:

 byte, short, int, long (integer types)

 float, double (floating-point types)

 char (character type)

 boolean (true/false)

Example:

public class PrimitiveExample {

public static void main(String[] args) {

int num = 100;

double pi = 3.14159;

char grade = 'A';

boolean isJavaFun = true;


System.out.println("Integer: " + num + ", Double: " + pi + ", Char: " + grade + ", Boolean: " +
isJavaFun);

4. Type Conversion and Casting

 Type Conversion (Widening): Automatic conversion from smaller to larger types.

 Type Casting (Narrowing): Explicit conversion from larger to smaller types.

Example:

public class TypeCastingExample {

public static void main(String[] args) {

int num = 10;

double d = num; // Widening

System.out.println("Widened: " + d);

double pi = 3.14;

int rounded = (int) pi; // Narrowing

System.out.println("Narrowed: " + rounded);

5. Autoboxing and Unboxing

 Autoboxing: Converting a primitive to its corresponding wrapper class.

 Unboxing: Converting a wrapper class to its primitive.

Example:

public class AutoBoxingExample {

public static void main(String[] args) {

int num = 10;

Integer boxed = num; // Autoboxing


int unboxed = boxed; // Unboxing

System.out.println("Autoboxed: " + boxed + ", Unboxed: " + unboxed);

6. Conditional Statements and Switch Case

 Conditional Statements: Use if, else if, and else for decision-making.

 Switch Case: Simplifies complex conditional logic.

Example:

public class ConditionalExample {

public static void main(String[] args) {

int age = 18;

// if-else

if (age < 18) {

System.out.println("Underage");

} else {

System.out.println("Adult");

// Switch-case

int day = 3;

switch (day) {

case 1 -> System.out.println("Monday");

case 2 -> System.out.println("Tuesday");

case 3 -> System.out.println("Wednesday");

default -> System.out.println("Invalid day");

}
}

7. Iteration Statements

Java provides three looping constructs:

1. while: Runs until the condition is false.

2. do-while: Executes the body at least once.

3. for: Used when the number of iterations is known.

Example:

public class LoopsExample {

public static void main(String[] args) {

// While loop

int count = 1;

while (count <= 5) {

System.out.println("Count: " + count);

count++;

// Do-while loop

int num = 10;

do {

System.out.println("Number: " + num);

num--;

} while (num > 5);

// For loop

for (int i = 1; i <= 3; i++) {

System.out.println("Iteration: " + i);


}

8. for and for-each Loop

 for Loop: Traditional loop with initialization, condition, and increment/decrement.

 for-each Loop: Used to iterate over arrays or collections.

Example:

public class ForEachExample {

public static void main(String[] args) {

int[] numbers = {10, 20, 30, 40};

// For loop

for (int i = 0; i < numbers.length; i++) {

System.out.println("Number (for): " + numbers[i]);

// For-each loop

for (int num : numbers) {

System.out.println("Number (for-each): " + num);

9. Jump, Break, and Continue Statements

 break: Terminates the loop or switch statement.

 continue: Skips the current iteration and proceeds to the next.

 return: Exits the method and optionally returns a value.


Example:

public class JumpStatementsExample {

public static void main(String[] args) {

// Break example

for (int i = 1; i <= 5; i++) {

if (i == 3) break;

System.out.println("Break Example: " + i);

// Continue example

for (int i = 1; i <= 5; i++) {

if (i == 3) continue;

System.out.println("Continue Example: " + i);

// Return example

System.out.println("Sum: " + calculateSum(10, 20));

static int calculateSum(int a, int b) {

return a + b; // Return statement

Summary Table

Topic Key Points

JVM runs bytecode, JRE includes JVM, JDK includes JRE and
JVM, JRE, JDK
development tools.

Identifiers, Variables, Identifiers name elements, variables store data, literals are constant values.
Topic Key Points

Literals

Primitive Data Types Eight types: byte, short, int, long, float, double, char, boolean.

Type Conversion and


Automatic widening and explicit narrowing.
Casting

Autoboxing and Unboxing Converts between primitives and wrapper classes.

Conditional and Switch


Decision-making using if-else and switch-case.
Case

Iteration Statements Loop constructs: while, do-while, for.

For and For-Each Loop for for known iterations, for-each for collections/arrays.

Jump, Break, Continue break exits loops, continue skips an iteration, return exits methods.
Arrays
An array in Java is a collection of elements of the same data type stored in contiguous memory
locations. Arrays are useful for storing and manipulating a fixed-size sequential collection of data.

Key Features:

 Fixed size: The size of an array is defined when it is created and cannot be changed.

 Zero-based indexing: The first element is at index 0.

 Homogeneous: All elements must be of the same data type.

Syntax:

dataType[] arrayName; // Declares an array

arrayName = new dataType[size]; // Allocates memory for the array

Example:

int[] numbers = new int[5]; // Declare and initialize an array of size 5

2. Types of Arrays

a. One-Dimensional Array

A linear collection of elements accessed using a single index.

Example:

public class OneDArrayExample {

public static void main(String[] args) {

int[] numbers = {10, 20, 30, 40, 50}; // Declaration and initialization

// Access elements using a loop

for (int i = 0; i < numbers.length; i++) {

System.out.println("Element at index " + i + ": " + numbers[i]);

b. Two-Dimensional Array
A table-like structure where elements are accessed using two indices.

Example:

public class TwoDArrayExample {

public static void main(String[] args) {

int[][] matrix = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

// Access elements using nested loops

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

c. Multi-Dimensional Array

An extension of 2D arrays for higher dimensions.

Example:

public class MultiDArrayExample {

public static void main(String[] args) {

int[][][] cube = {

{{1, 2}, {3, 4}},

{{5, 6}, {7, 8}}

};
// Access elements

System.out.println("Element at cube[1][0][1]: " + cube[1][0][1]); // Output: 6

3. Declaring, Initializing, and Accessing Arrays

a. Declaration

int[] numbers; // Preferred

int numbers[]; // Valid but less common

b. Memory Allocation

numbers = new int[5]; // Array of size 5

c. Initialization

int[] numbers = {10, 20, 30, 40, 50}; // Combined declaration and initialization

d. Accessing Elements

System.out.println(numbers[0]); // Access the first element

numbers[1] = 25; // Update the second element

4. Array Properties

 Length Property:

o The length property gives the size of the array.

o Example:

int[] arr = {10, 20, 30};

System.out.println("Array length: " + arr.length); // Output: 3

 Default Values:

o Numeric types (int, float, etc.): 0

o boolean: false

o Object references: null


5. Iterating Through Arrays

Using for Loop

int[] arr = {10, 20, 30, 40};

for (int i = 0; i < arr.length; i++) {

System.out.println("Element at index " + i + ": " + arr[i]);

Using Enhanced for Loop

for (int num : arr) {

System.out.println("Element: " + num);

6. Common Operations on Arrays

a. Finding Maximum and Minimum

public class MaxMinArray {

public static void main(String[] args) {

int[] arr = {15, 3, 9, 7, 20};

int max = arr[0];

int min = arr[0];

for (int num : arr) {

if (num > max) max = num;

if (num < min) min = num;

System.out.println("Max: " + max);

System.out.println("Min: " + min);


}

b. Copying Arrays

int[] original = {1, 2, 3, 4, 5};

int[] copy = new int[original.length];

System.arraycopy(original, 0, copy, 0, original.length);

c. Sorting Arrays

import java.util.Arrays;

public class SortArray {

public static void main(String[] args) {

int[] arr = {5, 1, 4, 2, 3};

Arrays.sort(arr);

System.out.println("Sorted Array: " + Arrays.toString(arr));

7. Arrays as Method Parameters

You can pass arrays to methods and return arrays from methods.

Example:

public class ArrayMethods {

static void printArray(int[] arr) {

for (int num : arr) {

System.out.print(num + " ");

System.out.println();

}
static int[] doubleArray(int[] arr) {

int[] doubled = new int[arr.length];

for (int i = 0; i < arr.length; i++) {

doubled[i] = arr[i] * 2;

return doubled;

public static void main(String[] args) {

int[] numbers = {1, 2, 3, 4, 5};

printArray(numbers);

int[] doubled = doubleArray(numbers);

printArray(doubled);

8. Array of Objects

An array can store objects of a class.

Example:

class Student {

String name;

int age;

Student(String name, int age) {

this.name = name;

this.age = age;

}
}

public class ArrayOfObjects {

public static void main(String[] args) {

Student[] students = {

new Student("Alice", 20),

new Student("Bob", 22)

};

for (Student s : students) {

System.out.println("Name: " + s.name + ", Age: " + s.age);

9. Limitations of Arrays

1. Fixed size: Cannot be resized after creation.

2. No methods for common operations like insertion or deletion (use collections like ArrayList
for dynamic arrays).

10. Summary Table

Concept Description

One-Dimensional Array Linear collection of elements.

Two-Dimensional Array Table-like structure with rows and columns.

Iteration Use for, enhanced for, or while loops to access elements.

Common Operations Finding max/min, sorting, copying.

Arrays as Method Parameters Pass and return arrays from methods.

Array of Objects Arrays can store object references.


Concept Description

Limitation Fixed size and lack of dynamic behavior.

You might also like