[!INDEX]

  1. non primitive dT. ( reference, reference describe, nonpdt-store value)
  2. General Code for string str as a reference
  3. Differences Primitive - Non Primitive : (memory, method, null)
  4. String implemented as a array/(Sequence) of characters ( char[] , charat[] mt)
  5. charAt[] mt code for, str = "Hello"
  6. Character Array to string output :
  7. charSequence interface ( read only + method + implements)
  8. CharSequence code mt =lenght +charAt +subSequence +toString
  9. Why Use CharSequence( flexibility (don't restrict), buffer builder)
  10. code StringBuilder implements CharSequence
  11. summery about above code
  12. string as a class ( package, sequence of array, immutable )
  13. Syntax of the String Class(SCC):
  14. Key Points of the String Class:

1. non primitive dT. ( reference, reference describe, nonpdt-store value)

[!NOTE]

  1. also called reference types because they store references to the actual data.
  2. Like String , Arrays , Classes and Objects , Interfaces.
  3. Reference Type:
  4. In Java, String is a class, and creating a String, means creating an object of that class.
  5. Means String variable, stores a reference (or memory address) to where the actual string value is stored in memory, not the value itself (as opposed to primitives like int, where the variable stores the value directly).

2. General Code for string str =hello, as a reference

public class Main {
    public static void main(String[] args) {
        String str = "Hello"; // `str` is a reference to a `String` object
        System.out.println(str); // Output: Hello
    }
}

3. Differences Primitive - Non Primitive : (memory, method, null)

[!NOTE]

  1. Memory:
  2. String are stored in heap memory, whereas
  3. primitive types are stored in stack memory.
  4. Methods:
  5. Non-primitive types can have methods and fields.
  6. Primitive types don't have methods or fields.
  7. Nullability:
  8. Non-primitive types can be null (e.g., String s = null;),
  9. while primitive types cannot.

4. String implemented as a array/(Sequence) of characters ( char[] , charat[] mt)

[!NOTE]

  1. A String is essentially a series of characters placed together in a specific order. For example, the String "Hello" is made up of the characters 'H', 'e', 'l', 'l', and 'o'.
  2. Internally, a String in Java is implemented as an array of characters (char[]). When you create a String, Java stores the individual characters in a character array.
  3. You can access individual characters using methods like .charAt().

5. charAt[] mt code for, str = "Hello"

public class Main {
    public static void main(String[] args) {
        String str = "Hello";
        System.out.println(str.charAt(0)); // Output: H
        System.out.println(str.charAt(1)); // Output: e
    }
}

6. Character Array to string output :

public class Main {
    public static void main(String[] args) {
        char[] chars = {'H', 'e', 'l', 'l', 'o'};
        String str = new String(chars); // Create a String from character array
        System.out.println(str); // Output: Hello
    }
}

7. charSequence interface ( read only + method + implements)

[!NOTE]

  1. read-only sequence of characters.
  2. general-purpose interface.
  3. access and manipulate character sequences (text) in a uniform manner.
  4. String, StringBuilder, and StringBuffer implement's CharSequence interface.
  5. allows read access to the characters in the sequence.
  6. No methods to modify the contents, making it immutable from the interface’s perspective.
  7. Methods
  8. int length(): Returns the number of characters in the sequence.
  9. char charAt(int index): Returns the character at the specified index.
  10. CharSequence subSequence(int start, int end): Returns a new CharSequence that is a subsequence of the original, starting from start to end.
  11. String toString(): Returns a String containing the character sequence.
  12. implements
  13. String: Immutable sequence of characters.
  14. StringBuilder: Mutable sequence of characters that is not thread-safe.
  15. StringBuffer: Mutable sequence of characters that is thread-safe.
  16. CharBuffer: Represents a sequence of characters in a buffer (used in NIO for efficient data handling).

[!important]


8. CharSequence code mt =lenght +charAt +subSequence +toString

public class Main {
    public static void main(String[] args) {
        CharSequence charSeq = "Hello, World!";  // String implements CharSequence
        System.out.println("Length: " + charSeq.length());  // Output: Length: 13
        System.out.println("Character at index 1: " + charSeq.charAt(1));  // Output: e
        System.out.println("Subsequence: " + charSeq.subSequence(0, 5));  // Output: Hello
        System.out.println("String representation: " + charSeq.toString());  // Output: Hello, World!
    }
}

9. Why Use CharSequence( flexibility (don't restrict), buffer builder)

[!NOTE]

  1. Flexibility:
  2. You can use CharSequence as a general type when you don't want to restrict your code to a specific class like String. This makes your code more flexible and adaptable.
  3. Immutability:
  4. Even though classes like StringBuilder and StringBuffer are mutable, the CharSequence interface itself only provides methods for read-only access, allowing you to ensure that the sequence is treated as immutable within certain parts of your code.

10. code StringBuilder implements CharSequence

public class Main {
    public static void main(String[] args) {
        CharSequence charSeq = new StringBuilder("Hello");  // StringBuilder implements CharSequence
        System.out.println("Length: " + charSeq.length());  // Output: Length: 5
        System.out.println("Character at index 1: " + charSeq.charAt(1));  // Output: e
    }
}

11. summery about above code

[!NOTE]

  1. In this case, charSeq is actually a StringBuilder object, but you can interact with it using CharSequence methods, providing flexibility while still using a mutable object behind the scenes.

12. string as a class ( package, sequence of array, immutable )

[!NOTE]

  1. Yes, String is a class in Java, part of the java.lang package.
  2. It represents a sequence of characters and provides a wide range of methods to manipulate and work with strings.
  3. The String class is immutable, which means once a String object is created, it cannot be modified. Any operation that appears to modify the string will actually create a new String object.

13. Syntax of the String Class(SCC):

public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
    // Fields
    private final char value[];
    private int hash; // Cache for hashCode

    // Constructors
    public String() {
        this.value = new char[0];
    }

    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

    public String(char value[]) {
        this.value = Arrays.copyOf(value, value.length);
    }

    public String(char value[], int offset, int count) {
        this.value = Arrays.copyOfRange(value, offset, offset + count);
    }

    public String(byte[] bytes) {
        this.value = new char[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            value[i] = (char) (bytes[i] & 0xFF);
        }
    }

    // Other constructors and methods
    // implicitly extends the object class
}

14. Key Points of the String Class:

[!NOTE]

  1. public final class:
  2. String is declared as final, meaning it cannot be subclassed (extended). This is important for its immutability and security features.
  3. Implements Interfaces:
    • String implements the Serializable interface, which allows it to be serialized (converted into a byte stream).
    • It implements Comparable<String>, which allows strings to be compared to each other.
    • It also implements CharSequence, which provides read-only access to the string's characters.
    • In Java, the String class implicitly extends the Object class, just like every other class in Java.
  4. Immutable Nature:
    • String objects are immutable, so the character array (value[]) is declared as private final. This prevents modification of the internal character data after the string is created.
  5. Constructors:
    • Default constructor: Initializes an empty string ("").
    • String copy constructor: Creates a new String from another String.
    • Character array constructor: Creates a String from a character array.
    • Byte array constructor: Creates a String from a byte array, converting bytes into characters.