[!INDEX]
- non primitive dT. ( reference, reference describe, nonpdt-store value)
- General Code for string str as a reference
- Differences Primitive - Non Primitive : (memory, method, null)
- String implemented as a array/(Sequence) of characters ( char[] , charat[] mt)
- charAt[] mt code for, str = "Hello"
- Character Array to string output :
- charSequence interface ( read only + method + implements)
- CharSequence code mt =lenght +charAt +subSequence +toString
- Why Use CharSequence( flexibility (don't restrict), buffer builder)
- code StringBuilder implements CharSequence
- summery about above code
- string as a class ( package, sequence of array, immutable )
- Syntax of the
StringClass(SCC):- Key Points of the
StringClass:
1. non primitive dT. ( reference, reference describe, nonpdt-store value)
[!NOTE]
- also called reference types because they store references to the actual data.
- Like
String, Arrays , Classes and Objects , Interfaces.- Reference Type:
- In Java,
Stringis a class, and creating aString, means creating an object of that class.- Means
Stringvariable, stores a reference (or memory address) to where the actual string value is stored in memory, not the value itself (as opposed to primitives likeint, 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]
- Memory:
Stringare stored in heap memory, whereas- primitive types are stored in stack memory.
- Methods:
- Non-primitive types can have methods and fields.
- Primitive types don't have methods or fields.
- Nullability:
- Non-primitive types can be null (e.g.,
String s = null;),- while primitive types cannot.
4. String implemented as a array/(Sequence) of characters ( char[] , charat[] mt)
[!NOTE]
- A
Stringis essentially a series of characters placed together in a specific order. For example, theString"Hello"is made up of the characters'H','e','l','l', and'o'.- Internally, a
Stringin Java is implemented as an array of characters (char[]). When you create aString, Java stores the individual characters in a character array.- 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]
- read-only sequence of characters.
- general-purpose interface.
- access and manipulate character sequences (text) in a uniform manner.
String,StringBuilder, andStringBufferimplement'sCharSequenceinterface.- allows read access to the characters in the sequence.
- No methods to modify the contents, making it immutable from the interface’s perspective.
- Methods
int length(): Returns the number of characters in the sequence.char charAt(int index): Returns the character at the specified index.CharSequence subSequence(int start, int end): Returns a newCharSequencethat is a subsequence of the original, starting fromstarttoend.String toString(): Returns aStringcontaining the character sequence.- implements
- String: Immutable sequence of characters.
- StringBuilder: Mutable sequence of characters that is not thread-safe.
- StringBuffer: Mutable sequence of characters that is thread-safe.
- CharBuffer: Represents a sequence of characters in a buffer (used in NIO for efficient data handling).
[!important]
- Using charSequence if we use append
- String: Immutable, any modification creates a new object.
- StringBuilder: Mutable, modifications update the original object.
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]
- Flexibility:
- You can use
CharSequenceas a general type when you don't want to restrict your code to a specific class likeString. This makes your code more flexible and adaptable.- Immutability:
- Even though classes like
StringBuilderandStringBufferare mutable, theCharSequenceinterface 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]
- In this case,
charSeqis actually aStringBuilderobject, but you can interact with it usingCharSequencemethods, providing flexibility while still using a mutable object behind the scenes.
12. string as a class ( package, sequence of array, immutable )
[!NOTE]
- Yes,
Stringis a class in Java, part of thejava.langpackage.- It represents a sequence of characters and provides a wide range of methods to manipulate and work with strings.
- The
Stringclass is immutable, which means once aStringobject is created, it cannot be modified. Any operation that appears to modify the string will actually create a newStringobject.
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]
public final class:Stringis declared asfinal, meaning it cannot be subclassed (extended). This is important for its immutability and security features.- Implements Interfaces:
Stringimplements theSerializableinterface, 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
Stringclass implicitly extends theObjectclass, just like every other class in Java.- Immutable Nature:
Stringobjects are immutable, so the character array (value[]) is declared asprivate final. This prevents modification of the internal character data after the string is created.- Constructors:
- Default constructor: Initializes an empty string (
"").- String copy constructor: Creates a new
Stringfrom anotherString.- Character array constructor: Creates a
Stringfrom a character array.- Byte array constructor: Creates a
Stringfrom a byte array, converting bytes into characters.