Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: important topics in java
Description: In this notes there is explaination of the some of the important topics in java
Description: In this notes there is explaination of the some of the important topics in java
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
JAVA NOTES(UNIT-III,IV)
UNIT-III
Scanner class, console class (main method)
The Scanner class and the Console class are used for input/output operations in
Java
...
It is a class in the
java
...
Here is an
example of using the Scanner class to read a user's name from the console:
import java
...
Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System
...
out
...
nextLine();
System
...
println("Hello, " + name + "!");
}
}
The Console class is used to read input and display output to the console
...
io package and provides methods to read input without echoing it to
the console (such as readPassword() ), and to display output to the console
without a newline (such as format() )
...
io
...
console();
if (console == null) {
System
...
println("Console not available");
System
...
readPassword("Enter your password: ");
System
...
println("Your password is: " + new String(password));
}
}
Q1
...
In Java, a stream is a sequence of data elements made available over time
...
Character Stream:
import java
...
*;
public class CopyFile {
public static void main(String args[]) throws IOException {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("input
...
txt");
int c;
while ((c = in
...
write(c);
}
} finally {
if (in != null) {
in
...
close();
}
}
}
}
Byte stream:
import java
...
*;
public class CopyFile {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try
{
in = new FileInputStream("input
...
txt");
int c;
while ((c = in
...
write(c);
}
}
finally
{
if (in != null) {in
...
close();}
}
}
}
Q2
...
a) Serialization: Serialization is the process of converting an object's state to a byte
stream so that it can be easily saved to disk, transmitted over a network, or
manipulated in memory
...
In Java, this is achieved by
implementing the Serializable interface
...
io
...
id = id;
this
...
id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this
...
ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos
...
close();
fos
...
ser");
ObjectInputStream ois = new ObjectInputStream(fis);
SerializationExample object2 = (SerializationExample) ois
...
close();
fis
...
out
...
getId());
System
...
println("Name: " + object2
...
This is the opposite of serialization, which is the process of
converting an object into a stream of bytes so that it can be stored or transmitted over
a network
...
io
...
ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
employee = (Employee) in
...
close();
fileIn
...
printStackTrace();
return;
} catch (ClassNotFoundException c) {
System
...
println("Employee class not found");
c
...
out
...
");
System
...
println("Name: " + employee
...
out
...
address);
System
...
println("SSN: " + employee
...
out
...
number);
}
}
Q3
...
io
...
txt
PrintWriter pw = new PrintWriter("file3
...
txt
BufferedReader br = new BufferedReader(new FileReader("file1
...
readLine();
// loop to copy each line of
// file1
...
txt
while (line != null)
{
line = br
...
txt"));
line = br
...
txt to file3
...
println(line);
pw
...
readLine();
}
pw
...
close();
pw
...
out
...
txt and file2
...
txt");
}
}
Q4
...
Exception handling is a mechanism in Java that allows a program to handle errors
and exceptional situations gracefully, rather than crashing or terminating abruptly
...
try-catch: The try-catch block is used to catch and handle exceptions that are
thrown by a program
...
2
...
3
...
4
...
It is typically used to clean up resources
such as files or database connections
...
catch: The catch block is used to catch and handle exceptions that are thrown by
a program
...
public class Example {
public static void main(String[] args) {
try {
int x = 10 / 0; // this will throw an ArithmeticException
} catch (ArithmeticException e) {
System
...
println("Caught exception: " + e);
} finally {
System
...
println("Finally block executed");
}
}
}
Other keywords related to exception handling in Java include:
1
...
2
...
3
...
4
...
5
...
Q5
...
public class Main{
public static void main(String args[])
{
// outer (main) try block
try {
//inner try block 1
try {
// inner try block 2
try {
int arr[] = { 1, 2, 3, 4 };
//printing the array element out of its bounds
System
...
println(arr[10]);
}
// to handles ArithmeticException
catch (ArithmeticException e) {
System
...
println("Arithmetic exception");
System
...
println(" inner try block 2");
}
}
// to handle ArithmeticException
catch (ArithmeticException e) {
System
...
println("Arithmetic exception");
System
...
println("inner try block 1");
}
}
// to handle ArrayIndexOutOfBoundsException
catch (ArrayIndexOutOfBoundsException e4) {
System
...
print(e4);
System
...
println(" outer (main) try block");
}
catch (Exception e5) {
System
...
print("Exception");
System
...
println(" handled in main try-block");
}
}
}
Q6
...
A
...
out
...
getMessage());
}
// Null Pointer Exception
String str = null;
try {
System
...
println(str
...
out
...
getMessage());
}
// Array Index Out of Bounds Exception
int[] arr = {1, 2, 3};
try {
int num = arr[3]; // accessing element outside array size
} catch (ArrayIndexOutOfBoundsException e) {
System
...
println("Array Index Out of Bounds Exception: " + e
...
Different between throw and Throws
A
...
creating custom exception for validating votes
A
...
");
}
else {
System
...
println("Valid vote!");
}
}
catch (UnderAgeException e) {
System
...
println("Error: " + e
...
What do you mean by Annotation's and their significance
A
...
They provide additional
information about the code and can be used to control the behavior of the compiler
and the runtime environment
...
Some commonly used annotations in Java include:
@Override: Indicates that a method is intended to override a method in a
superclass
...
@SuppressWarnings: Suppresses compiler warnings for a specific code block or
element
...
@Test: Used in JUnit testing to mark a method as a test method
...
These custom annotations can be used to enforce coding standards,
document code, or automate tasks
...
Applet Life cycle
A
...
An applet has a specific life cycle that consists of several methods that are
called at different stages in its execution
...
init - this method is called when the applet is first loaded
...
start - this method is called after the init method and is used to start the applet's
execution
...
paint - this method is called to paint the applet's graphics on the screen
...
stop - this method is called when the applet is stopped, either because the user
navigates away from the page or because the applet has completed its execution
...
destroy - this method is called when the applet is being removed from memory
...
Passing parametres
A
...
applet
...
awt
...
util
...
drawString("Welcome!", 150, 150);
g
...
toString(), 150, 170);
}
}
/*
try {
// Sleep for some time
Thread
...
out
...
");
}
System
...
println(threadName + " finished
...
setPriority(Thread
...
setPriority(Thread
...
start();
t2
...
out
...
getName() + " is alive: " + t1
...
out
...
getName() + " is alive: " + t2
...
join();
t2
...
printStackTrace();
}
// Check if the threads are alive after join
System
...
println(t1
...
isAlive());
System
...
println(t2
...
isAlive());
// Get the name of the current thread
System
...
println("Current thread: " + Thread
...
getName());
}
}
Q8
...
Synchronized:
The synchronized keyword in Java is used to define a block of code that can be
accessed by only one thread at a time
...
Examples:
To synchronize a block of code:
public class Counter {
private int count;
public synchronized void increment() {
count++;
}
public synchronized int getCount() {
return count;
}
}
To synchronize a static method:
public class SynchronizedExample {
private static int count = 0;
public static synchronized void incrementCount() {
count++;
}
public static int getCount() {
return count;
}
}
To synchronize a class:
public class SynchronizedExample {
private static int count = 0;
public static void incrementCount() {
synchronized(SynchronizedExample
...
Producer Consumer(Foint, wait,notifing)
A
...
util
...
produce();
} catch (InterruptedException e) {
e
...
consume();
} catch (InterruptedException e) {
e
...
start();
t2
...
join();
t2
...
printStackTrace();
}
}
public static class PC {
LinkedList
int capacity = 2;
// Producer method
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (this) {
// Wait until the list is not full
while (list
...
out
...
add(value++);
// Notify the consumer that the list has changed
notify();
// Sleep for some time
Thread
...
size() == 0)
wait();
// Remove the first item from the list
int val = list
...
out
...
sleep(1000);
}
}
}
}
}
Made with❤️by Telegram @oxrogerthat
Title: important topics in java
Description: In this notes there is explaination of the some of the important topics in java
Description: In this notes there is explaination of the some of the important topics in java