public class ReverseString {
public static void main(String[] args) {
String original = "Hello, World!";
String reversed = reverseRecursively(original);
System.out.println("Reversed string: " + reversed);
}
public static String reverseRecursively(String str) {
if (str.isEmpty()) {
return str;
}
return reverseRecursively(str.substring(1)) + str.charAt(0);
}
}
Let's break down the code (reverse string in java) step by step:
```java
public class ReverseString {
```
- This line declares a public class named `ReverseString`. In Java, every piece of executable code must be within a class.
```java
public static void main(String[] args) {
```
- The `main` method is the entry point of any Java application. When you run the program, this method is called first.
- `public` means the method is accessible from anywhere.
- `static` means the method belongs to the class rather than instances of it.
- `void` means the method doesn't return any value.
- `String[] args` is used to pass command-line arguments to the program (not used in this example).
```java
String original = "Hello, World!";
```
- This line creates a `String` variable named `original` and assigns it the value `"Hello, World!"`.
```java
String reversed = reverseRecursively(original);
```
- This line calls the `reverseRecursively` method, passing the `original` string as an argument.
- The `reverseRecursively` method processes the string and returns its reversed version, which is then stored in the `reversed` variable.
```java
System.out.println("Reversed string: " + reversed);
```
- This line prints the reversed string to the console.
```java
public static String reverseRecursively(String str) {
```
- This method is where the actual string reversal happens.
- `public static` makes the method accessible from anywhere and callable without creating an instance of the class.
- `String` indicates that this method returns a `String` value.
- `reverseRecursively(String str)` is the method signature, where `str` is the string to be reversed.
```java
if (str.isEmpty()) {
return str;
}
```
- This checks if the input string `str` is empty.
- If it is empty, the method returns the string as is, which effectively ends the recursion.
```java
return reverseRecursively(str.substring(1)) + str.charAt(0);
```
- If the string is not empty, the method does two things:
1. Recursive Call: It calls itself with the substring that excludes the first character: `str.substring(1)`.
2. Appending First Character: It appends the first character `str.charAt(0)` to the result of the recursive call.
- If `str` is `"Hello"`, `str.substring(1)` gives `"ello"`, and `str.charAt(0)` is `'H'`.
- The method keeps calling itself with smaller substrings until it reaches an empty string.
- Once it hits the base case, it starts returning and concatenating characters in reverse order.
- The recursion unfolds as follows:
- `reverseRecursively("Hello")` → `"olleH"`
- The final result is `"olleH"`, which is returned to the `main` method and printed.
- The program outputs: `Reversed string: !dlroW ,olleH`
This method of reversing a string using recursion leverages the call stack to process the string in reverse order, building up the reversed string as the recursion unwinds.
0 Comments