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
Prerequisites
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there, in this lesson, we'll learn Strings in Java. A java string is a series of characters like the word "HELLO" or the phrase "Java is Funny". The string is created between double quotes. Our example on the slide shows the string object in memory. It consists of individual chars, HELLO. The chars in a string are identified by index numbers. In "HELLO", the left most character H is at Index 0 and the next character E is at Index 1, and so on. We will do a lot of examples related to the string index by using string methods. Well, how can we create a string object?
There are two ways to create a string object. First one is String Literal. The string literal is the easiest and most recommended way to create strings in Java. In this way, simply we can assign the characters in double quotes to the variables of the string class. Another way to create a string object is String Object. We create a string object by using the "new" keyword. Okay, if you're ready, let's move on to the Eclipse and make some practice. First, let's create a new Java project. I click the File menu and select the 'New Java Project'. The project name can be "String". I don't change anything and click the 'Finish' button.
In this project, right click on the Source folder and select 'New Class'. I'll specify the package name as "stringexample" and class name as "StringExample", and select the checkbox to add the main method, and click the 'Finish' button. Okay, let's create a string using the literal. I write 'String' with the first letter capital. As you can see, the string class is in the java.lang package. If you remember, we did not need to import the classes in the java.lang package because the java.lang package is automatically imported. Therefore, we do not need to do any import operations when using the string class. But if you import, you will not get any error.
After remembering this again, we can now create our string object; String s1 = "Java". And let's create another string; String s2 = "Programming". Now, let's print these strings to the console; S out s1 and S out s2. Let's run the code. As you can see, we printed the s1 and the s2 strings on the console. Now, let's create string objects using the "new" keyword; String s3 = new String. Its value can be "Java". And let's create another string object; String s4 = new String, its value can be "Programming". Now, let's print these strings on the console; S out s3 and S out s4. As you can see, we saw the same result. Now, let's concatenate them with the plus sign and print them on the console; S out s1 + " " + s2. S out s3+ " " + s4. And let's run it.
As you can see, this time the "Java Programming" expression is printed on the console. We can use the plus sign to concatenate the strings. Also, we can write it like that; String s5 = s1 + s2; String s6 = s3 + s4. And now, we can print the s5 and s6 on the console. And let's run it. As you can see, the result is the same. But when using the plus sign, you should pay attention to the following. If the plus sign is used to concatenate two strings or a string and a primitive type such as int or double, both values are written side by side. If it's used to combine numeric values such as two ints or two doubles, this time it adds mathematically.
For example, int num1 = 5, int num2 = 7. Now, let's print the num1 + num2 on the console and run it. As you can see, the result is 12, not 5 and 7. Let's also use the string and int values with the plus sign. String myNumString = 3. Now, I will print the num1 + myNumString, and let's run it. As you can see, the result is 53, not the 8 because one of the parameters is a string and one of the parameters is int. So, the plus (+) sign is used for the concatenate not the mathematical. But what is the result if there are three parameters? Let's look at that immediately, S out (2 + 5 + "7").
And another print method; S out (2 + "7" + 5). And another print method; S out ("7" + 2 + 5). Let's try to guess the result before running the code. If there are more than two parameters, the compiler starts from the left and move sequentially. In the first expression, since the first two parameters are int, it will add the two together mathematically, that is, it will be 2 + 5 = 7. Then it will concatenate 7 with the string 7 print 77 to the console, because the first parameter is an int but the second parameter is a string. So, int + string is not mathematical, it's a string concatenate. So, the result will be 77 not 14.
In the second print method, since the first two elements are different, that is, one is a string and one is an int, we will do string concatenation, not mathematically and get the value 27. But notice that 27 is a string, then it will process string 27 and int 5 values as string concatenation not mathematical. The result will be 275. If we look at the last method, since one of the first two parameters is a string and the other is int, we will do string concatenation again and get the value 72. But notice that 72 is still a string. So, imagine 7 and 2 written side by side. Then it will see the values 72 and 5 as string concatenation again and will print 725 on the console this time.
Now, let's run it and test it. Notice that the first result is 77, the second is 275, and the third is 725. As a result, you should be careful with string and int concatenation. If it is desired to connect a string and an int plus sign, string concatenation is applied here and the values are written side by side. If the plus sign is used to connect two int values, this time the mathematical operation is performed here. If there are more than one parameter, it processes the parameters sequentially, this time starting from the left-most. Yes, in this lesson we focused on the concept of string. We talked about how to create strings and how to connect them with a plus sign. Let's take a short break here. See you in our 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.