Search code examples
javastringstandard-library

The String class and Java's dependency on it


So everyone knows that the shortest Java program that you can write is:

public class Program{
     public static void main(String []args){

     }
}

One would suspect that this shortest program would only consist out of language keywords, chosen names (like class name / function name / parameter name) and some syntax (brackets, etc).

But when you look closely suddenly the String class pops up. So does that mean that the language Java can not live without its String class? Or even without the complete standard library? (I thought C++ is able to exist without its standard library, no?)

Also I have been looking at the String class code but nowhere was an implementation of the operators + and += (maybe others are available?) to be found. How does this work then? Is this embedded as a special case in the compiler? Which would even bind the String class even tighter to the java language?

Are there other classes that are embedded so deeply in the Java language?


Solution

  • 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.