Java Examples
In this chapter, we will introduce common Java examples. By learning through examples, we can master Java applications more quickly.
Java Environment Setup Examples
- Java Example – How to compile a Java file?
- Java Example – How to run a compiled class file in Java?
- Java Example – How to execute a specified class file directory (classpath)?
- Java Example – How to Check the Currently Running Java Version?
Java String
- Java Example – String comparison
- Java Example – Find the last occurrence of a substring
- Java Example – Delete a character from a string
- Java Example – String replacement
- Java Example – String reversal
- Java Example – String search
- Java Example – String splitting
- Java Example – String splitting (StringTokenizer)
- Java Example – Convert string from lowercase to uppercase
- Java Example – Test whether two string regions are equal
- Java Example – String performance comparison test
- Java Example – String optimization
- Java Example – String formatting
- Java Example – Concatenate strings
Java Array
- Java Example – Array Sorting and Element Search
- Java Example – Adding Elements to an Array
- Java Example – Getting the Array Length
- Java Example – Array Reversal
- Java Example – Array Output
- Java Example – Getting the Maximum and Minimum Values of an Array
- Java Example – Merging Arrays
- Java Example – Array Fill
- Java Example – Array Expansion
- Java Example – Finding Duplicate Elements in an Array
- Java Example – Removing Array Elements
- Java Example – Array Difference (Set Difference)
- Java Example – Array Intersection
- Java Example – Finding a Specified Element in an Array
- Java Example – Checking Whether Arrays Are Equal
- Java Example – Array Union
Java Date and Time Handling
- Java Example – Formatting Time (SimpleDateFormat)
- Java Example – Getting the Current Time
- Java Example – Getting the Year, Month, etc.
- Java Example – Converting a Timestamp to Time
Java Methods
- Java Example – Method overloading
- Java Example – Output array elements
- Java Example – Tower of Hanoi algorithm
- Java Example – Fibonacci sequence
- Java Example – Factorial
- Java Example – Method overriding
- Java Example – Usage of the instanceof keyword
- Java Example – Usage of the break keyword
- Java Example – Usage of the continue keyword
- Java Example – Label
- Java Example – Using enum with switch statements
- Java Example – Using Enum constructors and methods
- Java Example – Using for and foreach loops
- Java Example – Using Varargs (variable arguments)
- Java Example – Using Varargs in overloaded methods
Print Shapes
- Java Example – Print a diamond
- Java Example – Multiplication table
- Java Example – Print a triangle
- Java Example – Print an inverted triangle
- Java Example – Print a parallelogram
- Java Example – Print a rectangle
Java File Operations
- Java Example - Writing to a File
- Java Example - Reading File Content
- Java Example - Deleting a File
- Java Example - Copying File Content to Another File
- Java Example - Appending Data to a File
- Java Example - Creating a Temporary File
- Java Example - Modifying a File's Last Modified Date
- Java Example - Getting File Size
- Java Example - Renaming a File
- Java Example - Setting a File to Read-Only
- Java Example - Checking Whether a File Exists
- Java Example - Creating a File in a Specified Directory
- Java Example - Getting File Modification Time
- Java Example - Creating a File
- Java Example - Comparing File Paths
Java Directory Operations
- Java Example – Recursively Creating a Directory
- Java Example – Deleting a Directory
- Java Example – Checking Whether a Directory Is Empty
- Java Example – Checking Whether a File Is Hidden
- Java Example – Getting the Directory Size
- Java Example – Finding Files in a Specified Directory
- Java Example – Getting the Parent Directory of a File
- Java Example – Getting the Last Modified Time of a Directory
- Java Example – Printing the Directory Structure
- Java Example – Traversing All Subdirectories Under a Specified Directory
- Java Example – Traverse all files in a specified directory
- Java Example – Finding Files in a Specified Directory
- Java Example – Traverse system root directories
- Java Example – Viewing the Current Working Directory
- Java Example – Traversing a Directory
Java Exception Handling
- Java Example - Exception Handling Method
- Java Example - Multiple Exception Handling (Multiple catch)
- Java Example – Usage of Finally
- Java Example – Handling Exceptions Using catch
- Java Example - Multithreading Exception Handling
- Java Example - Getting Exception Stack Trace
- Java Example - Exception Handling in Overloaded Methods
- Java Example – Chained Exceptions
- Java Example - Custom Exception
Java Data Structures
- Java Example – Summation (Sum of Numbers)
- Java Example – Convert infix expressions to postfix using a stack
- Java Example – At the beginning and end of a linked list (LinkedList)
- Java Example – Get the first element of a linked list (LinkedList)
- Java Example – Removing Elements from a LinkedList
- Java Example – Getting Elements of a LinkedList
- Java Example – Getting the Index Value of a Vector Element
- Java Example – Implementing a Stack
- Java Example – Searching for Elements in a LinkedList
- Java Example – Implementing String Reversal Using Push and Pop on a Stack
- Java Example – Queue Usage
- Java Example – Getting the Maximum Element of a Vector
- Java Example – Modifying a LinkedList
- Java Example – Rotating a Vector
Java Collections
- Java Example – Converting an Array to a Collection
- Java Example – Collection Comparison
- Java Example – HashMap Traversal
- Java Example – Collection Length
- Java Example – Shuffling a Collection
- Java Example – Collection Traversal
- Java Example – Reversing a Collection
- Java Example – Removing a Specified Element from a Collection
- Java Example – Read-Only Collection
- Java Example – Collection Output
- Java Example – Converting a Collection to an Array
- Java Example – Circularly Shifting List Elements
- Java Example – Finding the Maximum and Minimum Values in a List
- Java Example – Traversing the Keys and Values of a HashTable
- Java Example – Traversing a HashTable Using Enumeration
- Java Example – Adding Different Types of Elements to a Collection
- Java Example – Replacing List Elements
- Java Example – Truncating a List (Sublist)
Java Network Examples
- Java Example – Get the IP address of a specified host
- Java Example – Check whether a port is already in use
- Java Example – Get the local IP address and hostname
- Java Example – Get the size of a remote file
- Java Example – Implement a multi-threaded server program with Socket
- Java Example – View the last modified time of a specified file on a host
- Java Example – Connect to a specified host using Socket
- Java Example – Web scraping
- Java Example – Get the date information from a URL response header
- Java Example – Get URL response header information
- Java Example – Parse a URL
- Java Example – ServerSocket and Socket communication example
Java Threads
- Java Example – Check whether a thread is alive
- Java Example – Get the current thread name
- Java Example – State monitoring
- Java Example – Set thread priority
- Java Example – Deadlock and its resolution
- Java Example – Get the thread ID
- Java Example – Suspend a thread
- Java Example – Terminate a thread
- Java Example – Producer/consumer problem
- Java Example – Get the thread state
- Java Example – Get all threads
- Java Example – View thread priority
- Java Example – Interrupt a thread