A String in Java, in short words, is an object that stores a sequence of characters, think on a “char array”. But doesn’t have a char array internally.

Also in Java, Strings has some interesting qualities:

  • Strings are immutable objects, this means once is created can’t be modified.
  • Like any other class in java, extends from the Object class, so Strings contains its numeric representation trough the hashCode() method.
  • Strings can be directly instanced with quotes (“”) and using the “new” key word for the constructor.
  • The main difference between using quotes and the constructor is the String Pool.
  • The String pool is a reserved area in the JVM heap, which will store Strings for a future recycling, this means, if you declare a string like String a = “cat”, and then , String b = “cat” . Instead of creating a new String object, Java will check if the string already exists in the pool then will return a reference to the same memory location. But if doesn’t exists it will create a new string in the pool and return the reference instead.
  • On the other hand, when you use the new (constructor) for instantiating a String, Java forces the creation of a new String object directly on the heap space.