Java String or strings in Java are the main carriers of textual information, so this class is popular and often used. The Java.lang package has several classes for working with strings - these are StringBuffer and StringBuilder. They are declared as final - this indicates that it is impossible to inherit from these classes. In order to check this, let's open the editor and write the word string, and then go to the class itself. Here we will see that it is final - which means no inheritance.
Methods of creating objects
An object of the Java String class can be created in several ways. One of them is with the new keyword and the class constructor. If you press CTRL + P, you can see all these constructors. Java String accepts a string, an array of char, int, byte, and objects of the StringBuffer and StringBuilder classes. The second way to create an object is to directly assign a reference enclosed in double quotes.
Classes StringBuilder, StringBuffer and their methods
The StringBuilder and StringBuffer classes are very similar to each other and are close in purpose to the String class. Using methods and constructors, objects of these classes can be converted to each other and to a string. StringBuilder also has constructors that take a string and a sequence of characters, so the string can be converted to objects. In both cases, there is a ToString method. It does not apply to the StringBuilder and StringBuffer classes, but to the Object class, with which they can be cast to a string. You can view all methods by pressing CTRL + P. The main difference between these two classes from each other is that StringBuilder is faster, but it is not thread safe, unlike StringBuffer. Thread safety is the ability of code to run on multiple threads without blocking.
Features of creating an object in the StringBuilder or StringBuffer classes
When creating a StringBuilder or StringBuffer object, the default constructor automatically reserves some memory for 16 characters. This can be verified using one of them, the Capacity method, and output to the console. As a result, the buffer size will be displayed - 16 characters. You can press CTRL + P again, and select the Int Capacity method. That is, in the constructor we can set some buffer size. There is also a way to set it later. To do this, select the Ensure Capacity method in StringBuilder. With it, we can set the minimum guaranteed sizebuffer. If we change it, for example, to 20, and then start the console again, we will get 34. The thing is that you can set exactly the minimum guaranteed size, that is, in any case, it will not be less than 20. But, if in this the class has a formula in some method or constructor, according to which the allowable value is calculated.
Assigning a string value to an object
In order to assign some string value to a certain object, you need to write this value into the constructor or use one of the Append methods. There are a lot of them, some of these methods can take a boolean and then convert it to a string and add it, some take char, array, sequences, etc. Unlike String, StringBuilder, and StringBuffer, when their methods are used, we continue to work with the same object rather than creating a new one.
Differences between String and StringBuilder methods
When working with a string, if you use some methods, for example, concatenation or gluing objects, then in this case a new string is created as a result. This can be seen in the output to the console. As a rule, the StringBuilder and StringBuffer classes are used when strings are added very often. For example, in a loop where there is a large amount of information and strings are constantly being added, it is better to use them. This will speed things up, as the StringBuilder does everything faster, there is no constant creation of a new object.
When adding strings (wheneach addition) a new object will be created. If there are more than a thousand of them, then a lot of extra resources will be spent, and the process itself will take a lot of time. StringBuilder also has a SetLength method where you can set the buffer size. For example, if you specify 15 and output the result to the console, you will get the number 20. When the required buffer size is reached, this object automatically increases according to the formula specified in this method. The increase can occur almost up to a certain limit. For example, in the Capacity method, the maximum number of characters in the object will be equal to the number of int.
Insert and Delete methods
The next method most commonly used with StringBuilder is Insert. It allows you to insert a character at a specified position. It also has many options. Another popular method is to remove a character from a string. To perform this operation, you need to turn to Delete and select some of its options from the list. For example, you can access a substring from start index to end, or remove a character using its index.
The last StringBuilder method we will parse is Recerse or reverse. It is used to reverse a line. The characters in it will line up in reverse order. If we execute it and output the object to the console, we will get its mirror image. In this case, we continue to work with the same object, while a new one is not created. It just stores different information now. StringBuilder has a lot of methods that are similar to those ofString. They work exactly the same. There is practically no difference between StringBuffer and StringBuilder in terms of methods, except for the thread safety of StringBuffer. Due to this feature, it works a little slower.
String class methods
Let's talk about the methods of the Java String class. Let's look at them first for the srt1 and str2 object, and then the static methods of the String class itself. The first method we'll be looking at is CharAt. It takes an index and returns a character. In Java, strings are indexed, that is, there is a character under each index. For example, if you create a Java object, then "j" will be at index "0", "a" at index "1", "v" - "2", "a" - "3". That is, if you write “0” in the method, then it will return the character at the zero index in this string. You can check the operation of this method by outputting to the console and running the application. In the first case, we will see the symbol "j". If we change “0” to “1”, we will see the symbol “a”.
The next Java String method we'll look at is CodepointAt. It returns an int and takes an int index. CodepointAt returns the Unicode character at the given index. That is, if we write the index “1”, again referring to the index of the string and the character “a”, this method will return the Unicode number corresponding to the character “a”. If we run the application, we will see the number 97. For example, if we know that the index “A” corresponds to the number 65 in Unicode encoding. Then, at startup, where the method says thisvalue, we get 65.
Comparing strings in Java String
One more method - compareTo. It returns an int and takes another string. In Java String, strings are compared with each other using this method. It takes str1 and compares it to the string it receives, either lexicographically or lexicographically, in a case-sensitive manner, that is, both uppercase and lowercase. If the contents of the lines are the same, then we should get “0” when the application starts, since characters are subtracted in the process. Therefore, if the content is different, the response will receive some number equal to the difference of the indices.
Methods compareToIgnoreCase and Concat
The next method we'll look at is compareToIgnoreCase. It is similar to the previous one, only it does not take into account the case of letters, that is, it does not matter whether the letter is uppercase or lowercase. In the code editor, we write str with the Java value and capitalize the last letter. And as a result, we still get 0. We will get a different value if there is some other character. Now consider the method for Java String - Concat. It is designed to add, merge or concatenate two strings. Concat is similar to the usual addition of strings using the “+” sign.
Methods Equals and EqualsIgnoreCase
The Equals method takes another object of type Object. But since all our inherited classes are of type Object, it means that we can also pass a string. The return type of the Equals method is boolean, that is, our string is compared with another object for equality. Compare the contents of twoobjects. That is, if we pass the string str2, then if its content is not equal to the object, we will get the answer false, and if it is equal, then true. There is also a case-insensitive string comparison method. It's called EqualsIgnoreCase. It is similar to compareTo and works in a similar way. The return type here is boolean. In the case of compareTo, we get the character difference as a result, or 0 if the content matches.
HashCode and IndexOf methods
The next method is HashCode. It returns the hash code of our object. Each object has it, and is a number, more precisely, a bit string of a fixed length. You can get it using this method. Consider the IndexOf method. In fact, there are several, but we will consider only one. It accepts the variable ch, which is a Unicode variable. That is, if this variable matches a character in the string, then the index of this character is returned, if not, then “-1”. If our Unicode number is in this string, then the index of that character is returned. If there is no such character, then “-1” is returned.
Methods IsEmpty, Length, Split, Substring
Now let's look at the IsEmpty method. It returns true if the length of the string is 0, meaning it is empty. The next method is Length. It is responsible for the number of characters in a string, that is, it returns the length of a Java String. Spaces are also characters. The Split method takes a delimiter string and returns an array of strings. We can use a literal enclosed in double quotes or a string. When the method encounters a space, itdivides the string and distributes the parts into an array. As a result, we will get an array of strings. We can put a comma in the string, Split will also split it into parts. Let's turn to the Substring method. It extracts from the string a substring of length endindex-beginindex starting from the last position. One Java String example for this method is to write a string and give it two indices, then the string will be cut and it will start at the first index and end at the second. The specific part of str1 will be output to the console. "\n" is added to line break in Java String.
Methods ToCharArray, ToLowerCase, ToUpperCase, Trim
The ToCharArray method returns an array of characters, that is, the string is parsed character by character. If you declare some char, and then put a certain element into the method, then each character will be in the char array. Then each element of the array is printed to the console. Another method is ToLowerCase. It converts the string to lower case. ToUpperCase works in the opposite way - it makes all the letters in the string capitalized. A very commonly used method is Trim. It trims spaces at the beginning and end of the string. There are a lot of static methods of the class itself, because they accept different types and arrays. The most commonly used is ValueOf. It converts a variable of the given type to a string.