Where is a Java string stored


Strings and StringBuffer


String is one of the most widely used data types in Java. Character strings are stored in a variable with the data type string. In Java, strings are always put in double quotes. The length of a character string is variable. A string object can be created with the new operator. However, the new operator is not absolutely necessary, as it is otherwise called implicitly during the first assignment.


// Declaration and initialization of a string using the new operatorString string1 = newString ("simple text"); // declaration and direct assignment of a simple stringString string2 = "simple text";

In principle, a character string is nothing more than an array of individual characters (char). Therefore, it should be noted that here too, as with arrays, the first character has the index 0. Therefore the index of the last element does not correspond to the length of the character string, but to length -1. Otherwise you can get a java.lang.ArrayIndexOutOfBoundsException here as well.

The String class offers numerous methods. We only want to list a few selected ones here.


The equality operator "==", as used with numeric data types, cannot be used on strings. Therefore the data type String represents the method equals with which you can check two strings for content equality. If they are equal, "true" is returned, otherwise "false". Strings that differ only in upper and lower case are regarded as unequal (case-sensitive).


String password1 = "x4593kfn"; String password2 = "x4893kfn"; // The passwords are checked for equality with equalsif (password1.equals (password2)) {System.out.println ("The passwords are the same!");} Else {System.out.println ("The passwords do not match!");}



With the method length it can be determined how many characters a character string consists of.

String string = "0123456789"; int length = string.length (); // length = 10



The method trim cuts off both leading and trailing blanks in a character string.


String with spaces outside = "The spaces must be removed"; // outer spaces are removed String without spaces outside = with spaces outside.trim ();

It is important to note that the trim method returns the shortened string. The variable with spaces outside from the example above remains unchanged.



With indexOf it is determined at which point in a character string another character string or a letter begins. This function is very helpful, for example, if you want to search for the occurrence of certain words in a text. indexOf returns the index (starting with 0) at which the searched character string begins. If the string is not found, -1 is returned.


String text = "This is a text in which a search word occurs"; String search = "Search word"; int position = text.indexOf (search); // position = 30



With the method substring a substring is returned from a string. The start index and the length from the start index are specified as parameters.


String wholeString = "Eraser"; String partString1 = wholeString .substring (0.6); // partString1 = "Eraser"; String partString2 = wholeString .substring (6); // partString2 = "rubber";


toUpperCase / toLowerCase

To convert a string from lower case to upper case or vice versa, Java offers the methods toUpperCase or. toLowerCase.


String str = "hello world"; System.out.println (str.toUpperCase ()); // outputs "HELLO WORLD"

As with the trim method, it is important to note that the toUpperCase or toLowerCase method returns the character string converted into uppercase letters. In the example above, the variable str itself remains unchanged.


Concatenation of character strings (concatenation)

In order to connect several strings with each other, either the function concat or the + Operator be used.


String part1 = "java"; String sep = "-"; String part2 = "tutorial"; String name = part1.concat (sep) .concat (part2); System.out.println (name + ". Org"); // prints "java-tutorial.org"

If a string variable is to be changed by a concatenation, the assignment can be simplified using the "+ =" operator.


String welcomeMsg = "Hello"; welcomeMsg + = "My Friend!"; / * Simplification of welcomeMsg = welcomeMsg + "My Friend"; * / System.out.println (welcomeMsg); // returns "Hello My Friend!" out



In addition to the string data type, there is also the data type for handling character strings StringBuffer. However, this data type is rarely used. StringBuffer differs from String in that the StringBuffer class offers methods to manipulate the character strings, such as append (), insert (), delete (), setCharAt (), trimToSize () .... While new string objects are always created when the methods presented above are called in the String class, changes are made to the same object in the StringBuffer class. StringBuffer must be created explicitly with the new operator.


StringBuffer buf = newStringBuffer ("Welcome"); buf.append ("on java-tutorial.org!"); / * StringBuffer object is extended by a character string * / buf.insert (0, "Herzlich"); / * String is inserted at index 0 * / System. out.println (buf); / * returns "Welcome to java-tutorial.org!" out */

It would go beyond the scope of this article to explain all the functions of String and StringBuffer. A list of all methods can be found in the Java documentation.