The course is part of this learning path
In this course, we'll learn Strings in Java.
- What is String?
- Creating String Objectives
- Useful Methods of String Class
- Immutable String
- StringBuffer Class
- StringBuilder Class
- 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 lesson, we will talk about the immutable concept, and we'll try to understand how strings are immutable. In Java, string objects are immutable. Immutable simply means that you cannot change the object itself, but you can change the reference to the object. Once a string object is created, its data or state can't be changed, but a new string object is created. Let's try to understand the immutability concept by the example in the slide. We declare a string object S with the Java value. This new string instance is created and placed in the String Pool. And we can use the concat method to append the string with the program value at the end. Also, the new object is created and placed in the Pool. As you can see in the figure, two objects are created, but reference variable S still refers to 'Java' not 'Java Program', because strings are immutable objects.
Now, let's move on to the Eclipse and get some practice. First, I'll create a new class. In the string project, I'll right click on the stringexample package and select the 'New Class' option. The class name can be StringImmutable, and I will check the box for the main method and click the finish button. First, I will show you the mutability through the primitive int type. So, let's create an int variable, int number = 5. And in the next line I write, number++. And now, let's print the number variable on the console, System.out.println( number). So, the value printed on the console will be six because the primitive type int is mutable, and we can change its value not create another variable. Okay, let's run and see. As you can see, the result is six. Likewise, let's change a string. I will change a new string object. String numberString = "5".
Now, let's use the concat method to append a new string to the numberString object; numberString.concat("5"). And let's print the numberString on the console, System.out.println(numberString). So, you can think of the concat method as the plus sign. It'll concatenate two strings. Add the strings you wrote in parentheses to the end of the string you applied this method. So, while the value of the numberString object is currently five, it adds another five to the end. But since the string is immutable, here, the numberString is still five, not 55. Let's run and see. As you can see, the result is five, but you can create a new object and store the current value there. For example, if you assign the result of the numberString.concat("5") statement a new string or the numberString again, this time, the value of the numberString will change because a new object was created this time. To better understand this, I will again print the ID number to the console. Just below the line where we created the numberString object, I'm writing System.out.println(System.identifyHashCode(numberString)), and I'll copy and paste this code one more time after the concat method.
Yes. Now we can more easily understand the numberStrings here. And here are different objects. Let's run the application and test it. As you can see, when the numberString object was created, this was its ID. When we used the concat method, its ID changed and 55 was written to the console this time. I think it's a little clearer now. Now let's do one more example and end the lesson. I will create a new string. I'm writing String name = JAVA. Now again, I'll print Java in lowercase letters to the console using the toLowerCase() method which is one of the useful methods of the String class. In the meantime, we'll talk about the methods in the String class in more detail in our next lesson. Now let's call the toLowerCase() method; name.toLowerCase(), and let's print the variable name to the console. What do you think will be printed on the console?
Of course, again, JAVA in capital letters because the string is immutable. And although we use the toLowerCase method, we do not transfer it to a new object. So, the name string is still the same. Let's run it and see. As you can see, the console still says JAVA; in capital letters. Now let's pass it to another variable and print this variable to the console. At the beginning of this line of code, I'm typing; string name2 =. Now, let's print the object name2 to the console with the name object and let's run the code. Notice that the variable name still holds the capitalized JAVA, while the name2 variable holds the lowercase word Java. Yes, that's how strings are immutable. Let's take a short break here. See you in the next lesson.
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.