Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Examples

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

  1. Java Example – How to compile a Java file?
  2. Java Example – How to run a compiled class file in Java?
  3. Java Example – How to execute a specified class file directory (classpath)?
  4. Java Example – How to Check the Currently Running Java Version?

Java String

  1. Java Example – String comparison
  2. Java Example – Find the last occurrence of a substring
  3. Java Example – Delete a character from a string
  4. Java Example – String replacement
  5. Java Example – String reversal
  6. Java Example – String search
  7. Java Example – String splitting
  8. Java Example – String splitting (StringTokenizer)
  9. Java Example – Convert string from lowercase to uppercase
  10. Java Example – Test whether two string regions are equal
  11. Java Example – String performance comparison test
  12. Java Example – String optimization
  13. Java Example – String formatting
  14. Java Example – Concatenate strings

Java Array

  1. Java Example – Array Sorting and Element Search
  2. Java Example – Adding Elements to an Array
  3. Java Example – Getting the Array Length
  4. Java Example – Array Reversal
  5. Java Example – Array Output
  6. Java Example – Getting the Maximum and Minimum Values of an Array
  7. Java Example – Merging Arrays
  8. Java Example – Array Fill
  9. Java Example – Array Expansion
  10. Java Example – Finding Duplicate Elements in an Array
  11. Java Example – Removing Array Elements
  12. Java Example – Array Difference (Set Difference)
  13. Java Example – Array Intersection
  14. Java Example – Finding a Specified Element in an Array
  15. Java Example – Checking Whether Arrays Are Equal
  16. Java Example – Array Union

Java Date and Time Handling

  1. Java Example – Formatting Time (SimpleDateFormat)
  2. Java Example – Getting the Current Time
  3. Java Example – Getting the Year, Month, etc.
  4. Java Example – Converting a Timestamp to Time

Java Methods

  1. Java Example – Method overloading
  2. Java Example – Output array elements
  3. Java Example – Tower of Hanoi algorithm
  4. Java Example – Fibonacci sequence
  5. Java Example – Factorial
  6. Java Example – Method overriding
  7. Java Example – Usage of the instanceof keyword
  8. Java Example – Usage of the break keyword
  9. Java Example – Usage of the continue keyword
  10. Java Example – Label
  11. Java Example – Using enum with switch statements
  12. Java Example – Using Enum constructors and methods
  13. Java Example – Using for and foreach loops
  14. Java Example – Using Varargs (variable arguments)
  15. Java Example – Using Varargs in overloaded methods

Print Shapes

  1. Java Example – Print a diamond
  2. Java Example – Multiplication table
  3. Java Example – Print a triangle
  4. Java Example – Print an inverted triangle
  5. Java Example – Print a parallelogram
  6. Java Example – Print a rectangle

Java File Operations

  1. Java Example - Writing to a File
  2. Java Example - Reading File Content
  3. Java Example - Deleting a File
  4. Java Example - Copying File Content to Another File
  5. Java Example - Appending Data to a File
  6. Java Example - Creating a Temporary File
  7. Java Example - Modifying a File's Last Modified Date
  8. Java Example - Getting File Size
  9. Java Example - Renaming a File
  10. Java Example - Setting a File to Read-Only
  11. Java Example - Checking Whether a File Exists
  12. Java Example - Creating a File in a Specified Directory
  13. Java Example - Getting File Modification Time
  14. Java Example - Creating a File
  15. Java Example - Comparing File Paths

Java Directory Operations

  1. Java Example – Recursively Creating a Directory
  2. Java Example – Deleting a Directory
  3. Java Example – Checking Whether a Directory Is Empty
  4. Java Example – Checking Whether a File Is Hidden
  5. Java Example – Getting the Directory Size
  6. Java Example – Finding Files in a Specified Directory
  7. Java Example – Getting the Parent Directory of a File
  8. Java Example – Getting the Last Modified Time of a Directory
  9. Java Example – Printing the Directory Structure
  10. Java Example – Traversing All Subdirectories Under a Specified Directory
  11. Java Example – Traverse all files in a specified directory
  12. Java Example – Finding Files in a Specified Directory
  13. Java Example – Traverse system root directories
  14. Java Example – Viewing the Current Working Directory
  15. Java Example – Traversing a Directory

Java Exception Handling

  1. Java Example - Exception Handling Method
  2. Java Example - Multiple Exception Handling (Multiple catch)
  3. Java Example – Usage of Finally
  4. Java Example – Handling Exceptions Using catch
  5. Java Example - Multithreading Exception Handling
  6. Java Example - Getting Exception Stack Trace
  7. Java Example - Exception Handling in Overloaded Methods
  8. Java Example – Chained Exceptions
  9. Java Example - Custom Exception

Java Data Structures

  1. Java Example – Summation (Sum of Numbers)
  2. Java Example – Convert infix expressions to postfix using a stack
  3. Java Example – At the beginning and end of a linked list (LinkedList)
  4. Java Example – Get the first element of a linked list (LinkedList)
  5. Java Example – Removing Elements from a LinkedList
  6. Java Example – Getting Elements of a LinkedList
  7. Java Example – Getting the Index Value of a Vector Element
  8. Java Example – Implementing a Stack
  9. Java Example – Searching for Elements in a LinkedList
  10. Java Example – Implementing String Reversal Using Push and Pop on a Stack
  11. Java Example – Queue Usage
  12. Java Example – Getting the Maximum Element of a Vector
  13. Java Example – Modifying a LinkedList
  14. Java Example – Rotating a Vector

Java Collections

  1. Java Example – Converting an Array to a Collection
  2. Java Example – Collection Comparison
  3. Java Example – HashMap Traversal
  4. Java Example – Collection Length
  5. Java Example – Shuffling a Collection
  6. Java Example – Collection Traversal
  7. Java Example – Reversing a Collection
  8. Java Example – Removing a Specified Element from a Collection
  9. Java Example – Read-Only Collection
  10. Java Example – Collection Output
  11. Java Example – Converting a Collection to an Array
  12. Java Example – Circularly Shifting List Elements
  13. Java Example – Finding the Maximum and Minimum Values in a List
  14. Java Example – Traversing the Keys and Values of a HashTable
  15. Java Example – Traversing a HashTable Using Enumeration
  16. Java Example – Adding Different Types of Elements to a Collection
  17. Java Example – Replacing List Elements
  18. Java Example – Truncating a List (Sublist)

Java Network Examples

  1. Java Example – Get the IP address of a specified host
  2. Java Example – Check whether a port is already in use
  3. Java Example – Get the local IP address and hostname
  4. Java Example – Get the size of a remote file
  5. Java Example – Implement a multi-threaded server program with Socket
  6. Java Example – View the last modified time of a specified file on a host
  7. Java Example – Connect to a specified host using Socket
  8. Java Example – Web scraping
  9. Java Example – Get the date information from a URL response header
  10. Java Example – Get URL response header information
  11. Java Example – Parse a URL
  12. Java Example – ServerSocket and Socket communication example

Java Threads

  1. Java Example – Check whether a thread is alive
  2. Java Example – Get the current thread name
  3. Java Example – State monitoring
  4. Java Example – Set thread priority
  5. Java Example – Deadlock and its resolution
  6. Java Example – Get the thread ID
  7. Java Example – Suspend a thread
  8. Java Example – Terminate a thread
  9. Java Example – Producer/consumer problem
  10. Java Example – Get the thread state
  11. Java Example – Get all threads
  12. Java Example – View thread priority
  13. Java Example – Interrupt a thread