String Pool in Java |

Veröffentlicht am: 19 März 2024
auf dem Kanal: College of Coding
15
1

Okay first of all understand this video topic through Ai...
What is Java String Pool?
ANS: String Pool is possible only because String is immutable in Java and its implementation of String interning concept. String pool is also example of Flyweight design pattern. String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String. When we use double quotes to create a String, it first looks for String with the same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference. However using new operator, we force String class to create a new String object in heap space. We can use intern() method to put it into the pool or refer to another String object from the string pool having the same value.
Code:
package com.journaldev.util;

public class StringPool {

/**
Java String Pool example
@param args
*/
public static void main(String[] args) {
String s1 = "Cat";
String s2 = "Cat";
String s3 = new String("Cat");

System.out.println("s1 == s2 :"+(s1==s2));
System.out.println("s1 == s3 :"+(s1==s3));
}

}
Output:
s1 == s2 :true
s1 == s3 :false


Auf dieser Seite können Sie das Online-Video String Pool in Java | mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer College of Coding 19 März 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 15 Mal angesehen und es wurde von 1 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!