Java Notes
Java Notes
Java Virtual Machine (JVM) is a virtual machine that enables a computer to run Java programs. It
works in the following steps:
1. Loading: The class loader loads the bytecode of the class into memory.
2. Verification: The bytecode verifier checks the bytecode for correctness and security.
3. Preparation: The JVM allocates memory for static variables.
4. Initialization: The JVM initializes static variables and executes static blocks.
5. Execution: The JVM interprets the bytecode and executes it.
In Java, wrapper classes are used to convert primitive data types into objects. Each primitive data
type (like int, char, float, etc.) has a corresponding wrapper class. These classes are part of the
java.lang package and provide methods to convert between primitive values and objects.
class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
shape.draw(); }
}
8. Write a java program to count the number of objects instantiated from a user defined
class MyClass {
static int count = 0;
MyClass() {
count++;
}
}
9. Briefly describe the difference between abstract classes and interfaces with suitable
code.
Interface Example
interface AnimalActions {
void move(); // Abstract method
Output:
The bird flies.
This animal sleeps.
10. Write a java program to find the number of digits of a given number inputted though
command line argument.
11. Write a complete java program to generate custom exception while divide by zero is
attempted using a user defined exception class.
12. Write a java applet to draw a rectangle with red color on green background.
import java.applet.Applet;
import java.awt.*;
public class RectangleApplet extends Applet {
public void paint(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(0, 0, 300, 200);
g.setColor(Color.RED);
g.drawRect(50, 50, 200, 100);
}
}
13. Write a java program to find the factorial of given number inputted through command
line argument.
Exception handling in Java is a mechanism to handle runtime errors and ensure the smooth
execution of a program. It provides a way to respond to unexpected situations, such as invalid
user input, file not found, or network issues, without abruptly terminating the application.
15. With suitable java code explain the differences between ClassNotFoundException and
NoClassDefFoundError?
ClassNotFoundException Example:
NoClassDefFoundError:
class MissingClassExample {
void display() {
System.out.println("Class is available.");
}
}
16. Write a java program to find whether a given string is palindrome or not.
Terminated: The thread completes execution or ends due to an exception. It is permanently dead
and cannot be restarted.
19. Differentiate between Checked Exception and Unchecked Exceptions in Java
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("This mammal walks on land.");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("The dog barks.");
}
}
public class MultilevelInheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.walk();
dog.bark();
}
}