




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





































1. Introduction to Java Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems in 1995. It is widely used for: Web Applications Mobile Applications Desktop Applications Enterprise Software Cloud Computing Embedded Systems Features of Java Simple Secure Platform Independent Object Oriented Robust Portable Multithreaded Distributed Dynamic 2. Complete History of Java Programming Language Year Event 1991 Java project started by James Gosling and team 1995 Java officially released by Sun Microsystems 1996 JDK 1.0 released 2004 Java 5 introduced Generics, Enhanced for-loop 2014 Java 8 introduced Lambda Expressions 2017 Java modular system introduced 2023+ Modern Java supports cloud and enterprise development Why Java was Developed? Java was designed for: Platform independence Internet-based applications Secure distributed computing 3. C vs Java vs Python Feature C Java Python Type Procedural Object-Oriented Interpreted Platform Independent No Yes Yes Compilation Compiled Compiled + Interpreted Interpreted Security Low High Medium
Feature C Java Python Speed Fast Moderate Slower Memory Management Manual Automatic (Garbage Collection) Automatic Syntax Complexity Medium Moderate Easy
4. Setting Up Environment in Java Steps: 1. Install JDK 2. Set JAVA_HOME 3. Configure PATH variable 4. Install IDE (Eclipse/IntelliJ/VS Code) 5. Verify installation Verify Java Installation java -version javac -version 5. JDK in Java JDK (Java Development Kit) JDK is a software package used to develop Java applications. Components of JDK JRE Compiler (javac) Debugger Documentation tools Development utilities 6. How JVM Works JVM (Java Virtual Machine) JVM converts bytecode into machine code. Working Process 1. Write Java Program 2. Compile using javac 3. Bytecode generated (.class file) 4. JVM loads bytecode 5. JIT converts bytecode to machine code 6. Program executes **7. JVM Architecture Components of JVM
Thus same Java program can run on: Windows Linux Mac without changing code.
13. Basic Syntax of Java Structure of Java Program class Demo { public static void main(String[] args) { System.out.println("Hello Java"); } } Important Points Class names start with uppercase main() is entry point Statements end with semicolon Code blocks use {} 14. Hello World Program class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } Output Hello World 15. Variables in Java Variables are containers used to store data. Syntax datatype variableName = value; Example int age = 20; 16. Data Types in Java Two Types: 1. Primitive Data Types 2. Non-Primitive/Object Data Types 17. Primitive Data Types Primitive data types store simple values. Data Type Size Example byte 1 byte 10 short 2 bytes 200 int 4 bytes 5000
Data Type Size Example long 8 bytes 100000L float 4 bytes 10.5f double 8 bytes 20. char 2 bytes 'A' boolean 1 bit true
18. Object Data Types Object data types store references to objects. Examples String Arrays Classes Interfaces Example String name = "Java"; 19. Primitive vs Object Data Type Primitive Object Stores value Stores reference Faster Slower Fixed size Dynamic size Cannot call methods Can call methods Example int a = 10; String s = "Hello"; 20. Identifiers in Java Identifiers are names given to: Variables Methods Classes Objects Rules for Identifiers Can contain letters, digits, _, $ Cannot start with digit Cannot use keywords Case-sensitive Valid Identifiers studentName _age $salary Invalid Identifiers
Scanner sc = new Scanner(System.in); System.out.print("Enter Name: "); String name = sc.nextLine(); System.out.print("Enter Age: "); int age = sc.nextInt(); System.out.println("Name: " + name); System.out.println("Age: " + age); } } Output Enter Name: John Enter Age: 20 Name: John Age: 20
4. Methods of Scanner Class Method Purpose nextInt() Reads integer nextFloat() Reads float nextDouble() Reads double next() Reads single word nextLine() Reads complete line nextBoolean() Reads boolean 5. BufferedReader Class BufferedReader is present in the java.io package. It reads text efficiently from input streams. Import Statements import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; Example Program import java.io.*; class BufferedReaderExample { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Name: "); String name = br.readLine(); System.out.println("Name: " + name); } }
6. Difference Between Scanner and BufferedReader Scanner BufferedReader Present in java.util Present in java.io Slower Faster Parses primitive types Reads only text Easy to use More complex Uses regex parsing No parsing 7. Ways to Read Input from Console Method Package Scanner java.util BufferedReader java.io Console java.io Command Line Arguments Default 8. System.out.println() Used to print output on console. Syntax System.out.println("Message"); Example System.out.println("Hello Java"); 9. Difference Between print() and println() print() println() Cursor remains on same line Cursor moves to next line No line break Adds line break Example System.out.print("Hello "); System.out.print("Java"); Output Hello Java Example System.out.println("Hello"); System.out.println("Java"); Output Hello Java
17. Assignment Operators Used to assign values. Operator Example = a = 10 += a += 5 -= a -= 5 *= a *= 5 /= a /= 5 Example int a = 10; a += 5; System.out.println(a); Output 15 18. Logical Operators Used with boolean expressions. Operator Meaning && Logical AND || Logical OR ! Logical NOT Example int age = 20; System.out.println(age > 18 && age < 30); Output true 19. Unary Operators Operate on single operand. Types 1. Increment 2. Decrement 20. Increment Operator (++) Increases value by 1. Example int a = 5; a++; System.out.println(a);
Operator Meaning ^ XOR ~ Complement << Left Shift >> Right Shift Example int a = 5; int b = 3; System.out.println(a & b); Output 1
25. Operator Precedence Priority Operators Highest ++ -- Arithmetic * / % Arithmetic + - Relational > < >= <= Equality == != Logical AND && Logical OR || Lowest = **UNIT III – Flow Control, Functions and Recursion in Java
return
2. Decision Making Statements Decision-making statements allow the program to choose different paths based on conditions. 2.1 if Statement The if statement executes a block of code only when the condition is true. Syntax if(condition) { // statements } Flow of Execution Condition is checked. If condition is true → statements execute. If false → statements are skipped. Example 1: Positive Number Check class PositiveCheck { public static void main(String args[]) { int num = 10; if(num > 0) { System.out.println("Positive Number"); } } } Output Positive Number Example 2: Even Number class EvenCheck { public static void main(String args[]) { int n = 8; if(n % 2 == 0) { System.out.println("Even Number"); } } }
// statements } else { // default statements } Example: Grade Calculation class Grade { public static void main(String args[]) { int mark = 85; if(mark >= 90) { System.out.println("Grade A"); } else if(mark >= 75) { System.out.println("Grade B"); } else if(mark >= 50) { System.out.println("Grade C"); } else { System.out.println("Fail"); } } } Output Grade B 2.4 switch Statement The switch statement selects one block from many choices. Syntax switch(expression) { case value1: // statements break;
case value2: // statements break; default: // statements } Important Points Expression must be integer, character, or string type. break stops execution of further cases. default executes if no case matches. Example: Simple Calculator class Calculator { public static void main(String args[]) { int a = 10, b = 5; char op = '+'; switch(op) { case '+': System.out.println(a + b); break; case '-': System.out.println(a - b); break; case '*': System.out.println(a * b); break; case '/': System.out.println(a / b); break; default: System.out.println("Invalid Operator"); }
Syntax while(condition) { // statements } Example: Print Numbers 1 to 5 class WhileLoop { public static void main(String args[]) { int i = 1; while(i <= 5) { System.out.println(i); i++; } } } 3.3 do-while Loop Executes the loop body at least once. Syntax do { // statements } while(condition); Example: Display Numbers class DoWhileLoop { public static void main(String args[]) { int i = 1; do { System.out.println(i); i++; } while(i <= 5); }} Difference Between while and do-while
while Loop do-while Loop Condition checked first Condition checked after execution May execute zero times Executes at least once
4. continue Statement The continue statement skips the current iteration and moves to the next iteration. Syntax continue; Example class ContinueDemo { public static void main(String args[]) { int i; for(i = 1; i <= 5; i++) { if(i == 3) { continue; } System.out.println(i); } } } Output 1 2 4 5 5. break Statement The break statement immediately terminates a loop or switch statement. Syntax break; Example class BreakDemo { public static void main(String args[]) { int i; for(i = 1; i <= 10; i++) {