Java Notes
HubWhat Is Java?History of JavaFeatures of JavaJava EditionsJDK, JRE, and JVMYour First Java ProgramJava Program StructureJava Compilation ProcessIdentifiers in JavaKeywords in JavaVariables in JavaData Types in JavaType Casting in JavaOperators in JavaInput and Output in JavaComments in JavaIf-Else Statements in JavaSwitch-Case in JavaFor Loop in JavaWhile Loop in JavaDo-While Loop in JavaNested Loops in Javabreak and continue in JavaArray IntroductionOne Dimensional ArrayMulti Dimensional ArrayJagged Array in JavaArray OperationsArray Programs — FundamentalsArray Interview QuestionsString Class in JavaImmutable Strings in JavaString MethodsStringBuilder in JavaStringBuffer in JavaString Programs — FundamentalsString Interview QuestionsOOPs IntroductionClass and Object in JavaConstructor in Javathis Keyword in Javastatic Keyword in JavaEncapsulation in JavaInheritance in Javasuper Keyword in JavaPolymorphism in JavaMethod Overloading in JavaMethod Overriding in JavaAbstraction in JavaAbstract Class in JavaInterface in JavaObject Class in JavaWrapper Class in JavaInner Class in JavaAnonymous Class in JavaObject Cloning in JavaJava Collections Framework OverviewGenerics in JavaArrayList in JavaLinkedList in JavaVector in JavaStack in JavaHashSet in JavaLinkedHashSet in JavaTreeSet in JavaPriorityQueue in JavaArrayDeque in JavaDeque Interface in JavaHashMap in JavaLinkedHashMap in JavaTreeMap in JavaHashtable in JavaIterator in JavaComparable vs Comparator in JavaJava Collections Practice ProgramsException Handling IntroductionException Hierarchy in JavaTry-Catch Block in JavaFinally Block in Javathrow and throws Keywords in JavaCustom Exceptions in JavaException Handling Best PracticesFile Class in JavaByte Streams in JavaCharacter Streams in JavaBuffered Streams in JavaSerialization in JavaDeserialization in JavaJava NIO PackageLambda Expression in Java 8Functional Interface in Java 8Method Reference in Java 8Default and Static Methods in InterfacesStream API in Java 8Optional Class in Java 8Date and Time API in Java 8Thread IntroductionThread Life Cycle in JavaThread Class in JavaRunnable Interface in JavaCreating Threads in JavaThread Priority in JavaDaemon Threads in JavaSynchronization in JavaInter-Thread Communication in JavaExecutor Framework in JavaMultithreading Programs in JavaJDBC IntroductionJDBC ArchitectureMySQL Connection with JDBCStatement Interface in JDBCPreparedStatement in JDBCResultSet in JDBCCallableStatement in JDBCTransaction Management in JDBCBatch Processing in JDBCJDBC ProjectsServlet IntroductionGenericServletHttpServletServlet Life CycleRequest and Response ObjectsCookies in ServletsSession TrackingJSP IntroductionJSP Tags and JSTLExpression Language (EL)MVC ArchitectureSpring Framework IntroductionSpring Core ContainerSpring BeansDependency InjectionSpring Boot IntroductionREST API with Spring BootValidation in Spring BootCRUD ApplicationSpring Data JPAException Handling in Spring BootSpring SecurityJWT Authentication — Java ProgrammingConfig ServerEureka Server - Service DiscoveryAPI GatewayDocker DeploymentTime ComplexityRecursion in JavaLinked List in JavaStack in Java — DSA in JavaQueue in JavaHashing in JavaSorting Algorithms in JavaSearching Algorithms in JavaTree Data Structure in JavaHeap (DSA)Graph Data Structure in JavaDynamic Programming in JavaSingleton Design PatternFactory Design PatternBuilder Design PatternAdapter Design PatternObserver Design PatternMVC Pattern — Java ProgrammingBasic Java ProgramsArray Programs — PracticeString Programs — PracticeOOP ProgramsCollection ProgramsMultithreading ProgramsJDBC ProgramsPlacement Coding QuestionsStudent Management System — Java ProgrammingEmployee Management SystemLibrary Management System — Java ProgrammingBanking System — Java ProgrammingChat ApplicationE-Commerce BackendSpring Boot Fullstack ProjectCore Java Interview QuestionsOOPs Interview QuestionsCollections Interview QuestionsMultithreading Interview QuestionsJDBC Interview QuestionsSpring Framework Interview QuestionsCoding Interview QuestionsJava Cheat SheetQuick Revision Notes — Java ProgrammingImportant Java ProgramsImportant Formulas & ConceptsJava Master Course - Learning Roadmap
Collection of most important Java programs frequently asked in exams and interviews - patterns, number programs, string programs, and array programs.
Number Programs
1. Check Armstrong Number
java example
public boolean isArmstrong(int num) {
int original = num, sum = 0;
int digits = String.valueOf(num).length();
while (num > 0) {
int d = num % 10;
sum += Math.pow(d, digits);
num /= 10;
}
return sum == original;
}
// 153 = 1³ + 5³ + 3³ = 153 ✅2. Reverse a Number
java example
public int reverse(int num) {
int rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
return rev;
}
// 12345 → 543213. Check Palindrome Number
java example
public boolean isPalindrome(int num) {
return num == reverse(num);
}
// 121 → true, 123 → false4. Swap Two Numbers Without Temp
java example
a = a + b; // a=15
b = a - b; // b=5 (original a)
a = a - b; // a=10 (original b)
// OR using XOR
a = a ^ b; b = a ^ b; a = a ^ b;5. GCD and LCM
java example
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return (a * b) / gcd(a, b); }6. Sum of Digits
java example
int sumOfDigits(int n) {
int sum = 0;
while (n > 0) { sum += n % 10; n /= 10; }
return sum;
}String Programs
12. Count Vowels and Consonants
Example
String s = "Hello World";
int vowels = 0, consonants = 0;
for (char c : s.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) != -1) vowels++;
else if (c >= 'a' && c <= 'z') consonants++;
}
13. Check Anagram
java example
boolean isAnagram(String a, String b) {
char[] x = a.toLowerCase().toCharArray();
char[] y = b.toLowerCase().toCharArray();
Arrays.sort(x); Arrays.sort(y);
return Arrays.equals(x, y);
}14. Remove Whitespace
java example
String result = str.replaceAll("\\s+", "");15. Count Words in String
java example
int words = str.trim().split("\\s+").length;Array Programs
16. Find Max and Min
Example
int max = arr[0], min = arr[0];
for (int num : arr) {
if (num > max) max = num;
if (num < min) min = num;
}
17. Bubble Sort
Example
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp;
}
}
}
18. Linear Search
Example
int search(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) return i;
}
return -1;
}
19. Matrix Addition
Example
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
result[i][j] = a[i][j] + b[i][j];
20. Matrix Transpose
Example
int[][] transpose = new int[cols][rows];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
transpose[j][i] = matrix[i][j];
Recursion Programs
21. Tower of Hanoi
java example
void hanoi(int n, char from, char to, char aux) {
if (n == 1) { System.out.println(from + " → " + to); return; }
hanoi(n-1, from, aux, to);
System.out.println(from + " → " + to);
hanoi(n-1, aux, to, from);
}22. Power Function
java example
double power(double base, int exp) {
if (exp == 0) return 1;
if (exp < 0) return 1.0 / power(base, -exp);
return base * power(base, exp - 1);
}23. Sum of Array (Recursive)
Example
int sum(int[] arr, int n) {
if (n == 0) return 0;
return arr[n-1] + sum(arr, n-1);
}
OOP Programs
24. Singleton Pattern
java example
class Singleton {
private static Singleton instance;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) instance = new Singleton();
return instance;
}
}25. Custom Exception
java example
class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(double amount) {
super("Insufficient balance. Tried to withdraw: " + amount);
}
}Continue the lesson path
Study next in this module
01NotesImportant Formulas & ConceptsEssential Java formulas, time complexity reference, bit manipulation tricks, and mathematical concepts used in programming and DSA.02NotesQuick Revision Notes — Java ProgrammingConcise Java quick revision notes covering all major topics - OOP, Collections, Multithreading, JDBC, Spring Boot in table format for fast review before exams or interviews.03NotesJava Cheat SheetComplete Java programming cheat sheet with syntax reference, common operations, built-in methods, and code snippets for quick lookup.04Collections FrameworkJava Collections Practice ProgramsHands-on Java Collections programs with full explanations — word frequency, duplicate removal, grouping, sorting, LRU cache, and task scheduling. Each program explains what, why, and how.