Strings are a collection of characters implemented under the String class. However, they may also appear directly in the source code.
String operations[edit | edit source]
The only operation permitted on a string is the + operator. In the context of a string, this operator performs concatenation. The value on the right, a string, is appended to the string on the left.
Strings can also be concatenated with integers or other numbers. If this is the case, the variable or object is automatically converted to a string.
String performance[edit | edit source]
Even though you may be able to concatinate strings indefinitely, this is not the best means to do what you want. Since strings are immutable objects, attemtping to change a string will create a new string object, and making identical copies of the strings.
If you need to constantly append characters to a string, you may want to use a StringBuilder class.
String constructors[edit | edit source]
You can also create strings using these methods. Explanation to below every example of the String constructor.
String aString= new String(); // aString is a reference to an empty string. aString can be any name that you want to name the variable String aGreeting; // aGreeting is reference to a String aGreeting = new String("Hello world"); Greeting1 = "Hello world"; // Shortcut for constructing String objects. String Greeting2 = new String(Greeting1); // Greeting2 is a copy of the String Greeting1
|Project: Introduction to Programming in Java|
|Previous: Boolean Variables — Java Programming/Strings — Next: Java Classes|