0% found this document useful (0 votes)
2 views18 pages

java 4

This document provides comprehensive notes on Java topics relevant for the DSSSB TGT Computer Science exam, focusing on exception handling, multithreading, file handling, collections, and generics. It covers key concepts, advantages, and examples related to each topic, including exception types, thread creation, file streams, and the collections framework. Additionally, it includes important output questions and final revision points summarizing essential Java concepts.

Uploaded by

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

java 4

This document provides comprehensive notes on Java topics relevant for the DSSSB TGT Computer Science exam, focusing on exception handling, multithreading, file handling, collections, and generics. It covers key concepts, advantages, and examples related to each topic, including exception types, thread creation, file streams, and the collections framework. Additionally, it includes important output questions and final revision points summarizing essential Java concepts.

Uploaded by

Arpit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

JAVA NOTES FOR DSSSB TGT COMPUTER

SCIENCE EXAM
PART 4: EXCEPTION HANDLING, MULTITHREADING,
FILE HANDLING, COLLECTIONS AND GENERICS

1. Exception Handling in Java


An exception is an abnormal condition that interrupts the normal flow of program execution.
Examples:
• Division by zero
• Array index out of range
• File not found
• Invalid input
Java provides a mechanism to handle these errors:
Exception Handling

2. Advantages of Exception Handling


1. Prevents abnormal termination of programs
2. Maintains normal program flow
3. Separates error handling code from normal code
4. Improves reliability

3. Exception Hierarchy
All exceptions are derived from:
Object
|
Throwable
|
|----------------|
Exception Error
4. Throwable Class
Throwable is the parent class of:
1. Exception
2. Error

5. Exception Class
Exceptions are problems that can usually be handled.
Examples:
• IOException
• SQLException
• ArithmeticException
• NullPointerException

6. Error Class
Errors are serious problems that generally cannot be handled.
Examples:
• OutOfMemoryError
• StackOverflowError

7. Types of Exceptions
1. Checked Exceptions
Checked at compile time.
Examples:
• IOException
• SQLException
• ClassNotFoundException
Program must handle them.
2. Unchecked Exceptions
Checked at runtime.
They are subclasses of:
RuntimeException

Examples:
• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException

8. try-catch Block
Used to handle exceptions.
Syntax:
try
{
risky code;
}
catch(Exception e)
{
handling code;
}

Example:
class Test
{
public static void main(String args[])
{
try
{
int a=10/0;
}
catch(ArithmeticException e)
{
[Link]("Cannot divide by zero");
}
}
}

Output:
Cannot divide by zero
9. Multiple catch Blocks
A try block can have multiple catch blocks.
Example:
try
{

}
catch(ArithmeticException e)
{

}
catch(ArrayIndexOutOfBoundsException e)
{

10. catch Order Rule


Specific exception must come before general exception.
Correct:
catch(ArithmeticException e)
{

catch(Exception e)
{

Wrong:
catch(Exception e)
{

catch(ArithmeticException e)
{

Because Exception catches everything.

11. finally Block


The finally block always executes.
Used for:
• Closing files
• Releasing resources
Example:
try
{
[Link]("Try");
}
finally
{
[Link]("Finally");
}

Output:
Try
Finally

12. throw Keyword


Used to explicitly throw an exception.
Example:
throw new ArithmeticException();

13. throws Keyword


Used to declare exceptions.
Example:
void read() throws IOException
{

14. throw vs throws


throw throws
Used to throw exception Used to declare exception
Used inside method Used with method declaration
Throws one exception Can declare multiple
15. Custom Exception
User-defined exception.
Example:
class MyException extends Exception
{

16. Output-Based Exception Questions


Question 1
class Test
{
public static void main(String args[])
{
try
{
int a=10/0;
}
catch(Exception e)
{
[Link]("Error");
}
}
}

Output:
Error

Question 2
class Test
{
public static void main(String args[])
{
try
{
[Link]("A");
int x=10/0;
}

catch(Exception e)
{
[Link]("B");
}

finally
{
[Link]("C");
}
}
}

Output:
A
B
C

17. Multithreading
A thread is a lightweight sub-process.
Multithreading means executing multiple threads simultaneously.
Example:
• Music player
• Downloading
• User interface

18. Advantages of Multithreading


1. Better CPU utilization
2. Faster execution
3. Improved responsiveness
4. Resource sharing

19. Thread Creation in Java


Two methods:
1. Extending Thread class
2. Implementing Runnable interface

20. Creating Thread Using Thread Class


Example:
class MyThread extends Thread
{
public void run()
{
[Link]("Thread running");
}

public static void main(String args[])


{
MyThread t=new MyThread();

[Link]();
}
}

21. start() vs run()


start()
• Creates a new thread
• Calls run()

run()
• Contains thread code
• Calling directly does not create new thread

22. Creating Thread Using Runnable


Example:
class MyThread implements Runnable
{
public void run()
{
[Link]("Running");
}

public static void main(String args[])


{
Thread t=new Thread(new MyThread());

[Link]();
}
}
23. Thread Life Cycle
States:
New

Runnable

Running

Blocked/Waiting

Terminated

24. Thread Methods


start()
Starts thread.

run()
Contains execution code.

sleep()
Pauses thread.
Example:
[Link](1000);

join()
Waits for another thread to complete.
getName()
Returns thread name.

setName()
Sets thread name.

25. Thread Priority


Range:
1 to 10

Constants:
MIN_PRIORITY = 1

NORM_PRIORITY = 5

MAX_PRIORITY = 10

26. Synchronization
Synchronization controls access to shared resources.
Used to avoid:
Race condition

Example:
synchronized void display()
{

27. File Handling in Java


Java provides file handling through:
[Link] package
28. File Class
Represents file and directory path.
Example:
File f=new File("[Link]");

29. File Streams


Two types:
1. Byte Stream
2. Character Stream

30. Byte Stream


Used for binary data.
Classes:
• FileInputStream
• FileOutputStream
Example:
FileInputStream fin;

31. Character Stream


Used for text data.
Classes:
• FileReader
• FileWriter

32. Serialization
Serialization converts:
Object → Byte Stream
Used for:
• Saving objects
• Transferring objects
Keyword:
Serializable

33. Deserialization
Reverse process:
Byte Stream → Object

34. Collections Framework


Collection Framework provides classes and interfaces to store groups of objects.
Located in:
[Link] package

35. Collection Framework Hierarchy


Collection

|
|----------------|
List Set

SortedSet

Map

36. List Interface


Features:
• Ordered collection
• Allows duplicates
• Index based
Classes:
1. ArrayList
2. LinkedList
3. Vector
4. Stack

37. ArrayList
Dynamic array.
Features:
• Fast searching
• Allows duplicates
• Maintains insertion order
Example:
ArrayList<Integer> list=new ArrayList<>();

[Link](10);

[Link](20);

38. LinkedList
Uses linked list structure.
Advantages:
• Fast insertion/deletion

39. Vector
Similar to ArrayList.
Difference:
Vector is synchronized.
40. Stack
Follows:
LIFO

Example:
Stack<Integer> s=new Stack<>();

41. Set Interface


Features:
• Does not allow duplicates
• No index
Classes:
1. HashSet
2. LinkedHashSet
3. TreeSet

42. HashSet
Uses hashing.
Properties:
• No duplicate values
• No guaranteed order
Example:
HashSet<Integer> s=new HashSet<>();

[Link](10);
[Link](10);

Output contains:
10 only once

43. LinkedHashSet
Maintains insertion order.
44. TreeSet
Stores elements in sorted order.

45. Map Interface


Stores data as:
Key - Value pairs

Classes:
1. HashMap
2. LinkedHashMap
3. TreeMap

46. HashMap
Properties:
• Key-value storage
• Allows one null key
• Allows multiple null values
Example:
HashMap<Integer,String> map=new HashMap<>();

[Link](1,"Java");

47. TreeMap
Stores keys in sorted order.

48. Generics
Generics provide type safety.
Before generics:
ArrayList list;

After generics:
ArrayList<Integer> list;

Advantages:
• Avoids type casting
• Compile-time checking

49. Important Output Questions


Question 1
class Test
{
public static void main(String args[])
{
Thread t=[Link]();

[Link]([Link]());
}
}

Output:
main

Question 2
class Test
{
public static void main(String args[])
{
try
{
int a[]=new int[2];

[Link](a[5]);
}
catch(Exception e)
{
[Link]("Exception");
}
}
}

Output:
Exception
Question 3
import [Link].*;

class Test
{
public static void main(String args[])
{
HashSet<Integer> s=new HashSet<>();

[Link](10);
[Link](10);

[Link]([Link]());
}
}

Output:
1

DSSSB TGT Final Revision Points


1. Exception class extends Throwable.
2. Error and Exception are subclasses of Throwable.
3. Checked exceptions occur at compile time.
4. RuntimeException is unchecked.
5. finally block generally always executes.
6. start() creates a new thread.
7. run() contains thread code.
8. Thread priority range is 1 to 10.
9. ArrayList allows duplicates.
[Link] does not allow duplicates.
[Link] stores sorted elements.
[Link] stores key-value pairs.
[Link] provide type safety.
[Link] converts object to byte stream.
[Link] prevents race conditions.
End of Java Part 4

You might also like