A Java String
contains an immutable sequence of Unicode characters. Unlike C/C++, where string is simply an array
of char
, A Java String is an object of the class java.lang.
String is special class in Java and all String literal e.g.
"abc"(anything inside double quotes) are maintained in a separate
String pool, special memory location inside Java memory, more
precisely inside PermGen Space.
Any time you create a new String object using String literal, JVM
first checks String pool and if an object with similar content
available, than it returns that and doesn't create a new object. JVM
doesn't perform String pool check if you create object using new
operator.
Unlike an ordinary class:
String is associated with string literal in the form of double-quoted
texts such as "Hello, world!".
You can assign a string literal directly into a String variable,
instead of calling the constructor to create a String instance.
The '+' operator is overloaded to concatenate two String operands.
'+' does not work on any other objects such as your Person.java etc.
String is immutable. That is, its content cannot be modified once it
is created. For example, the method toUpperCase() constructs and
returns a new String instead of modifying its existing content.
Strings receive special treatment in Java, because they are used frequently in a program. Hence, efficiency (in terms of computation and storage) is crucial.
The designers of Java decided to retain primitive types in an object-oriented language, instead of making everything an object, so as to improve the performance of the language.
Primitives are stored in the call stack, which require less storage spaces and are cheaper to manipulate. On the other hand, objects are stored in the program heap, which require complex memory management and more storage spaces.
The '+' operator, which performs addition on primitives (such as int
and double), is overloaded to operate on String objects. '+'
performs concatenation for two String operands. Java does not support
operator overloading for software engineering consideration unlike C++
where you you can turn a '+' operator to perform a subtraction.
The '+' operator is the only operator that is internally overloaded to support string concatenation in Java. Take note that '+' does not work on any two arbitrary objects.
The JDK compiler, in fact, uses both String and StringBuffer to handle string concatenation via the '+' operator. For examples,
String msg = "a" + "b" + "c";
will be compiled into the following codes for better efficiency:
String msg = new
StringBuffer().append("a").append("b").append("c").toString();
For performance reason, Java's String is designed to be in between a primitive and a class.