People and machines communicate with each other using specially constructed character sets. Symbols are assembled into words, and words into sentences or lines from which the text is created. Whatever application is created, a character set will always be present, regardless of the language used.
String object in Java
In Java, a string is a special object and is denoted by the English word String. Prior to Java version 1.7.0_06, it was based on an array of char value, consisting of a special data type - char. The object was a 16-bit Unicode character with a default value of '\ u0000'. There was also an int offset variable that said which character in the array the string begins with. The variable int count described the number of characters accepted by the string, starting with int offset. But with the advent of Java 9, the offset and count variables are no longer used. Another innovation was the so-called “compact string concept”. An attempt to introduce it was made earlier, back in Java 6, but then there were performance problems, and this idea was abandoned.
Innovations in Java 9
The char value array in the new version became byte value, since according to statistics most of the strings could be represented by characters of the Latin alphabet. And for them you do not need to use 2 bytes, just one. Therefore, it was decided to introduce an array of bytes and byte coder, which stores the encoding - latin-1 or utf-16, where in the first case the byte is zero, and in the second - one. In the line, 1 byte is allocated for each character, and as soon as a character that does not fit is found, 2 bytes are allocated for it. Because of this, the length of the string is not always equal to the length of the array. They match only if it contains the latin-1 encoding. A string is equal to half the length of the array if the encoding is utf-16. Also in the new version appeared constant boolean COMPACT_STRINGS.
String Class Features
The String class is immutable, which means immutability. It is also final, that is, you cannot inherit from it, you cannot make any of your own lines based on this class. Its objects cannot be changed after creation. In fact, all the methods that supposedly change the string create a new object. The main reasons for this are security and String pool. Security in terms of threads is that there is no need to synchronize operations. You can pass a string between threads. Therefore, no need to worry that the string will be changed.
It is also possible to store the hash code directly in the object. Security also consists in the fact that strings can be safely passed as parameters for authorization, and immutability ensures that during the transfer process they will not be intercepted and changed. String pool is a kind of string cache. In the memory where the objects are stored, there is a place where the lines created by specifying literals in the code are stored. To speed up work and save memory, you can store several links on one line, if their values are the same. There are special classes for implementing mutable String - StringBuilder and StringBuffer. Both of them are almost identical, but the second is thread safe.
String class methods
You can work with strings using various methods. They allow you to perform the following actions:
- Compare Java string lengths
- extracting a substring from a string;
- receive a character by index or index by character;
- check the string for void;
- find out the length of one line;
- convert strings to an array;
- change case;
- concatenate strings
- break a string into an array;
- delete empty characters at the beginning and at the end;
- replace substrings.
Before offset and count were removed, the method of extracting a substring from a string worked a little differently than in the new version. He only created a new wrapper for the String object, while the array remained old. It was possible to replace part of the string, but at the same time there was a link to the previous version. This led to memory leaks. You could only avoid such a problem using the copy constructor. In the new version of Java, a string stores only the data that is written to it.
Comparing two Java strings
Consider one of the methods more closely. Especially often used a special action in the Java language - string comparison. The String class has two variants of the compareTo () method for this. The compareTo (String anotherString) method is used to compare the Java string String object with the received String argument and matches them lexically. It will return an int with a value of -1 for "less", 0 for equality, or 1 for "more." This method is useful for sorting algorithms. A similar method for comparing strings in Java is Equals (). It uses the boolean values of equality and when the objects are equivalent, true will be returned. If the first option is used for sorting, the second is needed to determine equality. Another option for comparing Java strings is the compareToIgnoreCase (String str) method . It is similar to the previous one, but ignores the case of characters.