Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Note : In JDK 1.5+ you can use StringBuilder , which works exactly like StringBuffer , but it is faster and not thread-safe.
The easiest way of creating a Java String object is using a string literal:
String str1 = "I cant be changed once created!";
A Java string literal is a reference to a String object. Since a Java String literal is a reference, it can be manipulated like any other String reference. i.e. it can be used to invoke methods of String class.
For example,
int myLenght = "Hello world".lenght();
The Java language provides special support for the string concatenation operator ( + ), which has been overloaded for Java Strings objects. String concatenation is implemented through the StringBuffer class and its append method.
For example,
String finalString = "Hello" + "World";
Would be executed as
String finalString = new StringBuffer().append("Hello").append("World").toString();
The Java compiler optimizes handling of string literals. Only one String object is shared by all string having same character sequence. Such strings are said to be interned, meaning that they share a unique String object. The Java String class maintains a private pool where such strings are interned.
For example,
String str1="Hello";
String str2="Hello";
If(str1 == str2)
System.out.println("Equal");
Would print true when run.
Since the Java String objects are immutable. Any operation performed on one String reference will never have any effect on other references denoting the same object.
String Constructor >>

another important property of
Strings are one of the
Post new comment