1.
Multithreading with an Example Program
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
[Link]([Link]().getName() + " - " + i);
}
}
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
[Link]();
[Link]();
}
}
2. Ways to Achieve Multithreading in Java - By extending the Thread class - By implementing the
Runnable interface - Using ExecutorService framework (advanced)
3. JVM and JDK - JVM (Java Virtual Machine): It runs Java bytecode and enables platform
independence. - JDK (Java Development Kit): It is a software development kit that includes JRE,
compilers, and tools for developing Java applications.
4. Interfaces - Interface in Java is a blueprint of a class with abstract methods.
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
[Link]("Bark");
}
}
5. HashMap vs Hashtable - HashMap is not synchronized, allows one null key and multiple null values. -
Hashtable is synchronized, doesn’t allow any null key or value.
6. OOPs Concepts Programs - Encapsulation
class Person {
private String name;
public void setName(String name) { [Link] = name; }
public String getName() { return name; }
}
1
- Inheritance
class Animal {
void eat() { [Link]("eating"); }
}
class Dog extends Animal {
void bark() { [Link]("barking"); }
}
- Polymorphism
class Animal {
void sound() { [Link]("Animal sound"); }
}
class Dog extends Animal {
void sound() { [Link]("Bark"); }
}
- Abstraction
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() { [Link]("Drawing Circle"); }
}
7. Spring Boot - Simplifies Java app development using prebuilt configurations and embedded servers.
8. JDBC Database Connection Example
import [Link].*;
public class DBConnect {
public static void main(String[] args) throws Exception {
Connection con = [Link]("jdbc:mysql://localhost:
3306/test", "root", "password");
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM users");
while ([Link]()) {
[Link]([Link]("username"));
}
[Link]();
}
}
2
9. Abstract Class vs Interface - Abstract class can have constructors and implemented methods. -
Interface has only abstract methods (Java 8+ allows default/static methods).
10. Print Name in Java
public class MyName {
public static void main(String[] args) {
[Link]("My name is P Mail");
}
}
11. Private Constructor in Abstract Class - Yes, an abstract class can have a private constructor but
cannot be instantiated externally.
abstract class A {
private A() {}
}
12. Garbage Collector - Automatically deletes unused objects to free memory. - Triggered by
[Link]() but not guaranteed.
13. HTML Form with String-Only Validation
<!DOCTYPE html>
<html>
<body>
<form onsubmit="return validate()">
First name: <input type="text" id="fname"><br>
Last name: <input type="text" id="lname"><br>
<input type="submit" value="Submit">
</form>
<script>
function validate() {
let fname = [Link]("fname").value;
let lname = [Link]("lname").value;
let regex = /^[a-zA-Z]+$/;
if ( || ) {
alert("Only letters allowed");
return false;
}
return true;
}
</script>
</body>
</html>
3
14. Count of Repeated Values in Java
import [Link].*;
public class CountDuplicates {
public static void main(String[] args) {
int[] arr = {2, 3, 3, 3, 4, 5, 4, 3};
Map<Integer, Integer> map = new HashMap<>();
for (int num : arr) {
[Link](num, [Link](num, 0) + 1);
}
for ([Link]<Integer, Integer> entry : [Link]()) {
if ([Link]() > 1)
[Link]([Link]() + " occurs " +
[Link]() + " times");
}
}
}