Strings in Java

Strings are full-blown objects in Java. Some of their characteristics:

  1. Strings are objects and are copied by reference. (Unlike Java primitive types which are copied by value like integers)

  2. Java Strings are IMMUTABLE. Once initialized they cannot be changed. This has the big advantage that the Java VM can share equal strings by making the string references point to the same memory location. Many applications (like e.g. parsers and scanners) use a lot of the same string tokens. They would be allocated only once in Java. Construction takes a bit longer as the string has to be hashed to generate the memory address.

  3. Strings are complex objects and keep metadata about the characters contained, e.g. length. This allows strings to have gaps and the string length can be determined quickly by looking at the length field. C needs to run through the string to detect its length as we will see.