A collection of Java programming examples covering a wide range of topics, from basic programs and pattern printing to arrays, strings, collections, file handling, exception handling, and multithreading.
- Arrays in Java are objects, which makes them more powerful than C/C++. In Java, arrays are objects with built-in properties.
- Java supports automatic garbage collection, so developers don’t need to manually free memory.
- Java has a rich library of Java Collections that can help you to solve problems without rewriting the standard data structure and algorithm codes.
These programs are useful for beginners to practice Java programming concepts and for experienced developers to revise commonly used Java techniques.
Basic Programs
This section covers basic Java programs that help beginners understand fundamental programming concepts such as input, operators, conditions, loops, numbers, and calculations.
- Read Number From Standard Input
- Get Input from the User
- Multiply Two Floating-Point Numbers
- Swap Two Numbers
- Add Two Binary Strings
- Add Two Complex Numbers
- Check Even or Odd Integers
- Find Largest Among 3 Numbers
- Find LCM of 2 Numbers
- Find GCD or HCF of 2 Numbers
- Display All Prime Numbers from 1 to N
- Check Leap Year
- Check Armstrong Number between Two Integers
- Check Whether the Input Number is a Neon Number
- Check Whether Input Character is Vowel or Consonant
- Find Factorial of a Number
- Find Even Sum of Fibonacci Series Till Number N
- Calculate Simple Interest
- Calculate Compound Interest
- Find the Perimeter of a Rectangle
Pattern Programs
This section contains Java programs for printing different patterns using stars, numbers, and characters. These programs help in practicing loops, nested loops, and conditional statements.
- Right Triangle Star Pattern
- Left Triangle Star Pattern
- Pyramid Star Pattern
- Reverse Pyramid Star Pattern
- Upper Star Triangle Pattern
- Mirror Upper Star Triangle Pattern
- Downward Triangle Star Pattern
- Mirror Lower Star Triangle Pattern
- Star Pascal's Triangle
- Diamond Star Pattern
- Square Star Pattern
- Spiral Pattern of Numbers
Conversion Programs
This section covers Java programs for converting values between different number systems and data types.
- Binary to Octal
- Octal to Decimal
- Decimal to Octal
- Hexadecimal to Decimal
- Decimal to Hexadecimal
- Decimal to Binary
- Binary to Decimal
- Boolean to String
- String to Double
- Double to String
- String to Long
- Long to String
- int to char
- char to int
Classes and Object Programs
This section covers Java programs related to classes, objects, and object-oriented programming concepts.
- Class and Object
- Abstract Class
- Singleton Class
- Create an Interface
- Encapsulation in Class
- Inheritance in Class
- Abstraction in Class
- Data Hiding in Class
- Polymorphism in Class
- Method Overloading in Class
- Method Overriding in Class
- super Keyword in Class
- this Keyword in Class
- static Keyword in Class
- Access Modifiers
Methods Programs
This section contains examples related to commonly used Java methods and method-based programming concepts.
- main() Method
- Static and Instance Methods
- forEach() Method
- toString() Method
- codePointAt() Method
- compare() Method
- equals() Method
- hasNext() and next() Methods
Thread Lifecycle Methods
Searching Programs
This section covers searching techniques used to find elements in arrays and other data structures.
1-D Array Programs
This section covers Java programs for searching, sorting, modifying, comparing, merging, and rotating one-dimensional arrays.
- Search an Element in an Array
- Find the Largest Element in an Array
- Sort an Array
- Sort the Elements of an Array in Descending Order
- Sort the Elements of an Array in Ascending Order
- Remove Duplicate Elements from an Array
- Merge Two Arrays
- Check if Two Arrays Are Equal
- Remove All Occurrences of an Element in an Array
- Find Common Array Elements
- Copy All Elements of One Array to Another Array
- Array Rotation
2-D Arrays (Matrix) Programs
This section covers programs for working with two-dimensional arrays and matrices, including matrix operations, traversal, and transformations.
- Print a 2D Array
- Add Two Matrices
- Sort a 2D Array Across Columns
- Check Whether Two Matrices Are Equal
- Find the Transpose of a Matrix
- Find the Determinant of a Matrix
- Find the Normal and Trace of a Matrix
- Print Boundary Elements of a Matrix
- Rotate Matrix Elements
- Compute the Sum of Diagonals of a Matrix
- Interchange First and Last Elements Across Rows
- Interchange First and Last Elements Across Columns
String Programs
This section contains Java programs for creating, accessing, comparing, modifying, and analyzing strings.
- Get a Character from a Given String
- Replace a Character at a Specific Index
- Reverse a String
- Reverse a String Using a Stack
- Sort a String
- Swap Pairs of Characters in a String
- Check Whether a String is a Pangram
- Print the First Letter of Each Word Using Regex
- Determine the Unicode Code Point at a Given Index
- Remove Leading Zeros from a String
- Compare Two Strings
- Compare Two Strings Lexicographically
- Print Words with Even Length
- Insert a String into Another String
- Split a String into Substrings
List Programs
This section covers Java programs for creating, searching, modifying, sorting, and converting lists.
- Initializing a List
- Find a Sublist in a List
- Find Minimum and Maximum in a List
- Split a List into Two Halves
- Remove a Sublist from a List
- Remove Duplicates from an ArrayList
- Remove Null Elements from a List
- Sort an ArrayList in Ascending Order
- Get First and Last Elements from an ArrayList
- Convert a List of Strings to a Comma-Separated String
- Add Elements at the First and Last Positions of a LinkedList
- Find Common Elements in Two ArrayLists
- Remove Repeated Elements from an ArrayList
Date and Time Programs
This section contains Java programs for working with dates, times, time zones, and different date and time formats.
- Format Time in AM-PM Format
- Display Dates of a Calendar Year in Different Formats
- Display Current Date and Time
- Display Time in Different Country Formats
- Convert Local Time to GMT
File Programs
This section covers Java programs for creating, reading, writing, copying, renaming, and managing files.
- Create a New File
- Create a Temporary File
- Write into a File
- Rename a File in Java
- Make a File Read-Only
- Compare Paths of Two Files
- Copy One File into Another File
- Print All Strings that Match a Pattern from a File
- Append a String to an Existing File
- Read Content from One File and Write it into Another File
- Read and Print All Files from a ZIP File
Directory Programs
This section covers Java programs for creating, traversing, searching, and managing directories and their contents.
- Traverse a Directory
- Get the Size of a Directory
- Delete a Directory
- Create Directories Recursively
- Search for a File in a Directory
- Find the Current Working Directory
- Display All Directories in a Directory
Exceptions and Errors Programs
This section contains Java programs for understanding exceptions, errors, exception hierarchies, and exception-handling techniques.
- Show Runtime Exceptions
- Show Types of Errors
- Exception Hierarchies
- Exception Methods
- Checked Exceptions
- Unchecked Exceptions
- Handle Divide-by-Zero and Multiple Exceptions
- Unreachable Code Error
- Thread Interference and Memory Consistency Errors
Collections Programs
This section contains Java programs for working with collections such as lists, sets, queues, and maps. These examples cover common collection operations such as adding, removing, sorting, searching, reversing, and converting elements.
- Use Different Types of Collections
- Print a Collection
- Compare Elements in a Collection
- Get the Size of a Collection
- Shuffle the Elements of a Collection
- Reverse a Collection
- Convert a Collection into an Array
- Convert an Array into a Collection
- Replace Elements in a List
- Rotate Elements of a List
- Iterate through Elements of a HashMap
Multithreading Programs
This section covers Java multithreading programs for creating, managing, and coordinating threads.
Miscellaneous Java Programs
This section contains additional Java programming examples involving collections, streams, iterators, strings, input streams, and other commonly used operations.
- Print Fibonacci Series in Different Ways
- Convert a LinkedList to an Array
- Convert a Vector to a List
- Convert a String to a List of Characters
- Convert an Iterator to a List
- Convert a List to a Map
- Convert a List to a Stream
- Convert a List to a Set
- Convert an InputStream to a String
- Convert a Set of Strings to an Array of Strings
- Convert a String to an Object
- Convert a String Value to a Byte Value