Overview of Java Arrays


<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents

</aside>

<aside> 💡

  1. Strings </aside>

<aside> 💡

  1. String Methods </aside>

<aside> 💡

  1. String Manipulation: StringBuffer, StringBuilder </aside>

Strings


  1. Java String

    1. In Java, string is basically an object that represents sequence of char values. An array of characters works same as Java string. For example:

      char[] ch={'s', 'o', 'm', 'a', 'i', 'y', 'a'};
      String s = new String(ch);
      
    2. is same as:

      String s="somaiya";
      
    3. Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.

    4. The java.lang.String class implements SerializableComparable and CharSequence interfaces.

      image.png


  2. CharSequence Interface.

    1. The CharSequence interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in Java by using these three classes.

      image.png

    2. The Java String is immutable which means it cannot be changed. Whenever we change any string, a new instance is created. For mutable strings, we can use StringBuffer and StringBuilder classes.


  3. What is String in Java?

    1. Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object.

  4. How to create a string object?

    1. There are two ways to create String object:
      1. By string literal
      2. By new keyword
    2. String Literal
      1. Java String literal is created by using double quotes. For Example:

        String s="welcome";
        
      2. Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool.

      3. For example:

        String s1="Welcome";  
        String s2="Welcome";//It doesn't create a new instance
        

        image.png

      4. In the above example, only one object will be created. Firstly, JVM will not find any string object with the value "Welcome" in string constant pool that is why it will create a new object. After that it will find the string with the value "Welcome" in the pool, it will not create a new object but will return the reference to the same instance.

        Note: String objects are stored in a special memory area known as the "string constant pool".

      5. Why Java uses the concept of String literal?

        1. To make Java more memory efficient (because no new objects are created if it exists already in the string constant pool).
    3. By new keyword
      1. In such case, JVM will create a new string object in normal (non-pool) heap memory, and the literal "Welcome" will be placed in the string constant pool. The variable s will refer to the object in a heap (non-pool).

        String s=**new** String("Welcome");//creates two objects and one reference variable