














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!















Unit 1. OOPs Concepts and Java Programming: Introduction to Object-Oriented concepts, procedural and object-oriented programming paradigm Java programming: An Overview of Java, Java Environment, Data types, Variables, constants, scope and life time of variables, operators, type conversion and casting, Accepting Input from the Keyboard, Reading Input with Java.util.Scanner Class, Displaying Output, Displaying Formatted Output with String.format(), Control Statements Unit 2. Arrays and OOP Constructs: Arrays, Command Line Arguments, Strings-String Class Methods Classes & Objects: Creating Classes, declaring objects, Methods, parameter passing, static fields and methods, Constructors, and โthisโ keyword, overloading methods and access Inheritance: Inheritance hierarchies, super and subclasses, member access rules, โsuperโ keyword, preventing inheritance: final classes and methods, the object class and its methods; Polymorphism: Dynamic binding, method overriding, abstract classes and methods; Unit 3. Interfaces, Packages & Exception Handling: Interfaces VS Abstract classes, defining an interface, implement interfaces, accessing implementations through interface references, extending interface; Packages: Defining, creating and accessing a package, importing packages. Exception Handling: Benefits of exception handling, the classification of exceptions, exception hierarchy, checked exceptions and unchecked exceptions, usage of try, catch, throw, throws and finally, rethrowing exceptions, exception specification, built in exceptions, creating own exception sub classes. Unit 4. Multithreading & Stream based I/O: Differences between multiple processes and multiple threads, thread states, thread life cycle, creating threads, interrupting threads, thread priorities, synchronizing threads, inter thread communication. Stream based I/O (java.io) โ The Stream classes-Byte streams and Character streams, Reading console Input and Writing Console Output, File class, Reading and writing Files, The Console class, Serialization Unit 5. GUI Programming with Swing: Introduction, MVC architecture, components, containers. Understanding Layout Managers - Flow Layout, Border Layout, Grid Layout, Card Layout, GridBag Layout. Event Handling- The Delegation event model- Events, Event sources, Event Listeners, Event classes, Handling mouse and keyboard events, Adapter classes
One of Java's most famous principles is:โWrite Once, Run Anywhere.โ This means a Java program can be written once and run on different operating systems with the help of the Java Virtual Machine (JVM). Java source code is compiled into bytecode. This bytecode can run on any operating system that has a suitable JVM. Therefore, Java is called platform independent. Originally, Java was developed and maintained by Sun Microsystems. In 2010, Oracle Corporation acquired Sun Microsystems. After the acquisition, Oracle became responsible for the development and maintenance of Java. Oracle has continued to develop Java and release new versions with improvements, new features, performance enhancements, and security updates.
Major Features of Java Java provides many important features: โ Simple โ Easy to learn and use. โ Object-Oriented โ Uses classes and objects. โ Platform Independent โ Runs on different operating systems. โ Secure โ Provides features for secure application development. โ Robust โ Supports exception handling and automatic memory management. โ Portable โ Java programs can be moved between platforms. โ Multithreaded โ Allows multiple tasks to execute simultaneously. โ High Performance โ JVM technologies provide efficient execution.
โ JRE โ Compiler (javac) โ Debugging and other development tools So, if we want to write and develop Java programs, we need the JDK.
โ Use the Java interpreter (java) to run the bytecode. โ Command: java Hello โ Output: Hello, Java!
Data Type Purpose Example byte Small whole numbers byte age = 20; short Small whole numbers short num = 1000; int Whole numbers int marks = 90; long Large whole numbers long population = 100000L; float Decimal numbers float price = 10.5f; double Decimal numbers double salary = 50000.50; char Single character char grade = 'A'; boolean True or false boolean pass = true;
Example: class DataTypes { public static void main(String[] args) { int age = 20; double salary = 25000.50; char grade = 'A'; String name = "Ravi"; int[] marks = {90, 85, 95}; System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary); System.out.println("Grade: " + grade); System.out.println("First Mark: " + marks[0]); } } Output Name: Ravi Age: 20 Salary: 25000. Grade: A First Mark: 90
โ Destroyed when the method/block finishes execution. void method() { int x = 10; // x is local to this method }
co n s ta nts in tw o w a ys :
in itia liz ed.
fin a l d a ta T yp e C O N ST A N T_ N A M E = va lu e;
fin a l in t M A X _ S T U D E N T S = 5 0 ;
fin a l d o u b le P I = 3 .1 4 1 5 9 ;
fin a l S trin g C O M P A N Y = "A B C C o rp ";
Operator Description Example Result | Logical AND (5 > 3) || (3 < 4) Logical OR ! Logical NOT !(5 > 3) false
Operator Description Example Meaning = Simple assignment a = 10 Assign 10 to a += Add and assign a += 5 a = a + 5 -= Subtract and assign a -= 3 a = a - 3 *= Multiply and assign a *= 2 a = a * 2 /= Divide and assign a /= 4 a = a / 4 %= Modulus and assign a %= 3 a = a % 3
Operator Description Example Result ++ Increment by 1 a++ or ++a a = a + 1 -- Decrement by 1 a-- or --a a = a - 1
6.Ternary Operator (?:) โ Conditional operator that acts as a shortcut for if-else. โ Syntax: condition? expression1 : expression โ If condition is true, evaluates expression1, else expression2. Example: int a = 10, b = 20; int max = (a > b)? a : b; // max = 20 System.out.println(max); // Output: 20
Example
public class OperatorExample { public static void main(String[] args) { int a = 10, b = 20, c = 5, d = 15; // Arithmetic operators int sum = a + b; System.out.println("Sum: " + sum); // Relational operators System.out.println("a == b " + (a == b)); // Logical operators boolean result = (a < b) && (b > c); System.out.println("Logical AND: " + result); // Assignment operators d += 5; System.out.println("d after += 5: " + d); } }
Output:
Sum: 30 a == b false Logical AND: true d after += 5: 20
From To byte short, int, long, float, double short int, long, float, double int long, float, double long float, double float double
Example: int i = 100; long l = i; // int to long Type Casting Type casting = manually changing one data type into another.
Scanner sc = new Scanner(System.in); System.out.print("Enter your age: "); int age = sc.nextInt(); System.out.println("Age = " + age); } } If the user enters: Enter your age: 20 Output: Age = 20
System.out.println() - It displays the output and then moves to the next line. System.out.println("Hello"); System.out.println("Java"); Output: Hello Java
System.out.print() It displays the output but does not move to the next line. System.out.print("Hello "); System.out.print("Java"); Output: Hello Java
Printing Variables We can also display variable values. int age = 20; System.out.println(age); Output: 20
We can combine text and variables using +. String name = "Ravi"; int age = 20; System.out.println("Name: " + name); System.out.println("Age: " + age); Output:
Name: Ravi Age: 20
Formatted Output Sometimes we want output in a specific format. For this, Java provides: String.format() Example String name = "Ravi"; int age = 20; String result = String.format("Name: %s, Age: %d", name, age); System.out.println(result); Output: Name: Ravi, Age: 20
// code if condition3 is true } else { // code if none of the above conditions are true} Example Program: public class IfElseIfExample { public static void main(String[] args) { int marks = 75; if (marks >= 90) { System.out.println("Grade: A"); } else if (marks >= 75) { System.out.println("Grade: B"); } else if (marks >= 60) { System.out.println("Grade: C"); } else { System.out.println("Grade: F"); } } } Output: Grade: B
i++; } } } Output: i = 1 i = 2 i = 3
The do-while Loop - The do-while loop is similar to the while loop, but the code block executes at least once, because the condition is checked after the loop body. Syntax: do { // code to be executed } while (condition); Example Program: public class DoWhileExample { public static void main(String[] args) { int i = 1; do { System.out.println("i = " + i); i++; } while (i <= 3); } } Output: i = 1 i = 2 i = 3 The for Loop- The for loop is used when the number of iterations is known. It has three parts: initialization, condition, and increment/decrement. Syntax: for (initialization; condition; update) { // code to be executed }