Java class libraries are pre-written code offering various functionalities like I/O, networking, and data
structures.
A typical Java development environment includes a JDK, JRE, and JVM, working together to compile,
interpret, and execute Java code.
Memory management in Java is handled automatically through garbage collection, simplifying
development and reducing potential errors.
Java Class Libraries:
Core Functionality: These libraries, part of the JDK and JRE, provide fundamental classes and interfaces
for common tasks like input/output, networking, and data structures.
Organization: Java libraries are organized into packages, making it easier to find and use specific
functionalities.
Examples: java.lang, java.util, java.io, java.net are some commonly used packages.
Integration: Libraries like JDBC (for database connectivity) and JNDI (for directory services) facilitate
interaction with external systems.
GUI Libraries: Swing and AWT provide tools for building graphical user interfaces.
Typical Java Development Environment:
JDK (Java Development Kit):
Includes the compiler, debugger, and other tools needed to develop Java applications.
JRE (Java Runtime Environment):
Provides the necessary components, including the JVM, to run Java applications.
JVM (Java Virtual Machine):
An abstract computing machine that executes Java bytecode, ensuring platform independence.
IDE (Integrated Development Environment):
Tools like Eclipse, IntelliJ IDEA, or NetBeans can be used for coding, debugging, and testing.
Memory Concepts in Java:
Automatic Memory Management:
Java uses garbage collection to automatically reclaim memory occupied by objects that are no longer
referenced, simplifying memory management for developers.
JVM Memory Areas:
The JVM manages memory in different areas, including the heap (where objects are stored), the stack
(for method calls), and the method area/Metaspace (for class metadata and static variables).
Heap:
The heap is a shared memory space where objects are dynamically allocated and garbage collected.
Stack:
The stack stores local variables and method call information.
Garbage Collection:
The garbage collector periodically identifies and removes unreachable objects from the heap, freeing up
memory.
Object Creation and Destruction:
Objects are created using the new operator and are automatically garbage collected when no longer
referenced
Types of Decision-Making Statements
if
if-else
nested-if
if-else-if
switch-case
Statement Use Case Example
if Single condition check if (age >= 18)
if-else Two-way decision if (x > y) {...} else {...}
nested-if Multi-level conditions if (x > 10) { if (y > 5) {...} }
if-else-if Multiple conditions if (marks >= 90) {...} else if (marks >= 80) {...}
switch-case Exact value matching switch (day) { case 1: ... }
break Exit loop/switch break;
continue Skip iteration continue;
return Exit method return result;
1. if Statement
if(condition) {
// Statements to execute if
// condition is true
}
import java.util.*;
class Geeks {
public static void main(String args[])
int i = 10;
if (i < 15)
// part of if block(immediate one statement
// after if condition)
System.out.println("Inside If block");
// always executes as it is outside of if block
System.out.println("10 is less than 15");
// This statement will be executed
// as if considers one statement by default again
// below statement is outside of if block
System.out.println("I am Not in if");
2. if-else Statement
3. if(condition){
4. // Executes this block if
5. // condition is true
6. }else{
7. // Executes this block if
8. // condition is false
9. }
3. nested-if Statement
import java.util.*;
class Geeks {
public static void main(String args[])
int i = 10;
if (i == 10 || i < 15) {
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println(
"i is smaller than 12 too");
else {
System.out.println("i is greater than 15");
}
4. if-else-if ladder
import java.util.*;
class Geeks {
public static void main(String args[])
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
5. Switch Case
Practical time seen
Iterartin Statements:
1 For Loop:
2.while
3. Do_while
import java.io.*;
class GFG {
public static void main(String[] args)
// For Loop
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
System.out.println();
// While Loop
int count = 0;
while (count < 5) {
System.out.print(count + " ");
count++;
System.out.println();
// Do-While Loop
count = 0;
do {
System.out.print(count + " ");
count++;
} while (count < 5);
System.out.println();