MY NOTES
MY NOTES
o Syntax:
java
Copy code
try {
// risky code
} catch (ExceptionType1 e) {
// handle type 1
} catch (ExceptionType2 e) {
// handle type 2
o Example:
java
Copy code
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
} finally {
System.out.println("Execution complete.");
java
Copy code
MyException(String message) {
super(message);
• Hierarchy:
o Throwable: Root class for all exceptions.
• Example:
java
Copy code
try {
System.out.println(str.length()); // NullPointerException
} catch (NullPointerException e) {
System.out.println("Handled NullPointerException.");
• Example:
java
Copy code
try {
obj.riskyMethod();
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
} finally {
System.out.println("Cleanup complete.");
• Steps:
• Example:
java
Copy code
CustomException(String message) {
super(message);
}
}
try {
validateAge(15);
} catch (CustomException e) {
• Significance:
o Ensures cleanup code always runs, regardless of
exceptions.
• Scenarios:
• Example:
java
Copy code
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Handled exception.");
} finally {
System.out.println("Always executes.");
o Example:
java
Copy code
try {
System.out.println(arr[5]); // Throws
ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
o Example:
java
Copy code
MyException(String message) {
super(message);
try {
} catch (MyException e) {
• Comparison:
2. Define multithreading.
7. What is JDK?
o JDK (Java Development Kit) is a toolkit for developing Java
applications.
o Extending Thread:
▪ Example:
java
Copy code
System.out.println("Thread running");
new MyThread().start();
o Implementing Runnable:
▪ Example:
java
Copy code
System.out.println("Thread running");
o Life Cycle:
▪ New: Thread created but not started (Thread t =
new Thread()).
o Diagram:
sql
Copy code
↑ ↓
Waiting/Blocked
java
Copy code
});
t1.start();
java
Copy code
java
Copy code
System.out.println("Synchronized block");
java
Copy code
t1.setPriority(Thread.MAX_PRIORITY);
o Applets:
▪ Runs in a browser.
▪ No main() method.
o Applications:
▪ Runs standalone.
html
Copy code
o Steps:
o Example:
java
Copy code
fos.write("Hello".getBytes());
fos.close();
1. Explain in detail the thread life cycle and the methods used
for thread control in Java.
1. New (Born):
▪ Example:
java
Copy code
2. Runnable:
▪ Example:
java
Copy code
3. Running:
▪ Example:
java
Copy code
System.out.println("Thread is running.");
4. Blocked:
5. Waiting:
6. Terminated:
• Thread Synchronization:
o Example:
java
Copy code
count++;
• Synchronized Blocks:
o Example:
java
Copy code
synchronized(this) {
count++;
• Explanation:
Synchronized methods and blocks ensure that only one
thread can access a shared resource at a time. When one
thread enters a synchronized method or block, it holds
the lock on the object until it exits.
Embedded in web
Standalone programs
Use Case pages for small,
for desktops or servers.
interactive programs.
Feature Java Applet Java Application
• Java Applets:
o Lifecycle:
• Java Applications:
java
Copy code
import java.applet.Applet;
import java.awt.Graphics;
bash
Copy code
javac MyApplet.java
Copy code
<html>
<body>
</applet>
</body>
</html>
bash
Copy code
appletviewer MyApplet.html
• Byte Streams:
java
Copy code
int data;
System.out.print((char) data);
fis.close();
java
Copy code
fos.close();
• Character Streams:
java
Copy code
String line;
System.out.println(line);
reader.close();
java
Copy code
PrintWriter writer = new PrintWriter(new
FileWriter("output.txt"));
writer.println("Hello, PrintWriter!");
writer.close();
• Explanation: