String, String Pool, Intern method in Java. Bengali-Ep:14

Publicado el: 04 septiembre 2022
en el canal de: Ajoy Debnath
117
6

Hi,
Hope you are all doing great. Let's learn together.
Join Telegram: 📲 https://t.me/contactajoydebnath

Chapters ❤
0:00 Introduction
00:21 What is String and Its Property
02:47 String is Immutable
05:09 Operator Overloading
07:12 StringBuffer and String Builder
10:16 Interfaces implemented by String
11:17 Different Ways to Create String
17:16 New keyword to Create String
21:53 Different String Methods
33:08 String Pool
43:36 Intern Method

Document Link -
https://docs.google.com/document/d/1L...

Note:
Java was developed by Sun Microsystems (which is now a subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, James Gosling and his team changed the name from Oak to Java.

Core Java Playlist:
   • Core Java  

Spring Playlist:
   • Spring Framework  

Like, Comment, Share, and Subscribe.
Thank you again.

STS Link:
https://spring.io/tools

JDK Link:
https://www.oracle.com/java/technolog...

String
1. Basically, a String is a sequence of characters but it’s not a primitive type.
2. The string is an immutable object which means that it cannot be changed once it is created.
3. The string is the only class where operator overloading is supported in Java. We can concat two strings using the + operator. For example "a"+"b"="ab".
4. Java provides two useful classes for String manipulation - StringBuffer and StringBuilder.(Mutable).

1. String Literal
String str = "Hello";
This is the most common way of creating strings.
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.

2. By new keyword
String str = new String("Hello");
In such case, JVM will create a new string object in heap memory not in the pool, and the literal "Hello" will be placed in the string constant pool (If not present). The variable 'str' will refer to the object in a heap (not in the pool).

Few Methods

equals() and equalsIgnoreCase()
String class provides equals() and equalsIgnoreCase() methods to compare two strings. These methods compare the value of a string to check if two strings are equal or not. It returns true if two strings are equal and false if not.

split()
split(String regex): This method splits the string using a given regex expression and returns an array of strings.
split(String regex, int limit): This method splits the string using a given regex expression and returns an array of strings but the element of the array is limited by the specified limit. If the specified limit is 2 then the method returns an array of size 2.

length()
Java String length() method returns the length of the string.

substring()
This method returns a part of the string based on specified indexes.

concat()
String concatenation is a very basic operation in Java. A string can be concatenated by using the “+” operator or by using concat() method.

format(),replace(),contains(CharSequence s)...etc.

String Pool in Java
A string Pool in Java is a pool of Strings stored in Java Heap Memory. We know that String is a special class in Java and we can create String objects using a new operator as well as providing values in double-quotes.

A string pool is also an example of a 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 a 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 a new operator, we force the 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.

How many Strings are getting Created in the String Pool?
String str = new String("Hello");
Either 1 or 2 strings will be created. If there is already a string literal “Hello” in the pool, then only one string “Hello” will be created in the heap. If there is no string literal “Hello” in the pool, then it will be first created in the pool and then in the heap space, so a total of 2 string objects will be created.

String intern() Method
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.


En esta página del sitio puede ver el video en línea String, String Pool, Intern method in Java. Bengali-Ep:14 de Duración hora minuto segunda en buena calidad , que subió el usuario Ajoy Debnath 04 septiembre 2022, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 117 veces y le gustó 6 a los espectadores. Disfruta viendo!