Start course
1h 31m

In this course, we'll learn Strings in Java. 

Learning Objectives

  • What is String?
  • Creating String Objectives
  • Useful Methods of String Class
  • Immutable String
  • StringBuffer Class
  • StringBuilder Class

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In this video, we will do another project. In this project, we'll write a Java program that finds the reverse of a given string. If you're ready, let's start. First, I'll create a new project. I click on the file menu and select the new Java project options. The project name can be ReverseString. The Java version will be Java SE 1.8 and click the 'Finish' button. Okay, now I will create a new class. In reverse string project, right click on the source folder and select new class. The package name can be reversestring, and I will specify the class name as ReverseString. And I'll check the checkbox for the main method and click the 'Finish' button. 

Okay, first we will use the Scanner class for getting input from the user. Scanner scanner = new Scanner(; And I'll show a message to the user to enter a string by using the print method. System.out.print("Enter a string: "); and now I'll declare a variable string with string type, and assign the next line method of the scanner class to this string. The next line method is used to read the whole line. Also, I will declare a variable length with int type, and to sign the length of the string by using the length method of the string class. Okay, let's declare another variable, reversedString with string type, and its initial value will be empty. Okay, now I will create a for loop. 

With the for loop, I'll read the letter of the string from end to start. In the for loop, we declare a variable i and we assign the length minus 1 expression to this new variable i, because the index number of the last letter is one less than the size of the string. For example, if the expression that the user enters on the console consists of 10 characters, the index number of the last character will be nine. Therefore, the initial value of the variable i will be length - 1. Now, let's create the loop condition. The condition is i >= 0. Because the first index number is zero, the loop should continue until the index number of the first character reaches zero. And i is decremented by one. Okay, now let's write the necessary code in loop. 

We'll use the charAt() method to extract characters from the given string and concatenate them in reverse order to reverse the string. So, I write reversedString = reversedString + string.charAt(i); Thus the characters of the expression entered into the console in each loop will be passed to the reversedString object from end to the start. And when the loop is finished, the reverse of the expression entered in the console will be assigned to the reversedString object. Now, outside the loop, let's print the reversedString object to the console. In the print method, I write, "ReversedString is: ". And after the plus sign, I'll display the result value of reversedString. And lastly, I'll close the Scanner object. 

Okay, everything is ready. Let's run the code. I will enter the 'Java is easy' string. And after pressing the Enter key, yes, as you can see, our string is reversed and displayed. Now let's briefly summarize. In this program, we read a string from the user. Then in the for loop, we obtain the characters of the original string from the end by using the ChartAt() method of the string class, and we concatenate them to a new string by using the plus operator. Finally, we printed the reversedString on the console outside of the loop. Okay, I think this is understood. Let's take a short break here. I'll see you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics