filmov
tv
Lecture 12: String, StringBuffer and StringBuilder classes in Java

Показать описание
String, StringBuffer and StringBuilder classes in Java :
String is a sequence of characters in java.
char array[] = new char[4];
char array[0] = ‘J’;
char array[1] = ‘a’;
char array[2] = ‘v’;
char array[3] = ‘a’;
In java strings are class objects and implemented using three classes, namely,
1. String
2. StringBuffer
3. StringBuilder
In java string is not a character array and is not NULL terminated like in C and C++ language.
Strings may be declared and created as follows:
String StringName; //String Declaration
String Name = new String (“String”); //String Initialisation
Example:
String Name = new String(“Sagar”);
Like array, it is possible to get the length of string using the length method of String class.
Java strings can be concatenate using ‘+’ operator.
Every immutable object in Java is thread safe that implies String class is also thread safe. String cannot be used by two threads simultaneously.
If we create String reference variable and once assigned string object to it then we cannot change the string object.
String demo = " Amol " ;
The above string object is stored in constant string pool and its value cannot be modified.
demo="Jagtap" ;
new "Jagtap" string is created in constant pool and referenced by the demo reference variable but "Amol" string still exists in string constant pool and its value is not overridden but we lost reference to the "Amol" string.
Java String class provides a lot of methods to perform operations on string such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
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.
The Java String is immutable which means it cannot be changed. Whenever we change any string, a new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes.
We will discuss immutable string later. Let's first understand what String in Java is and how to create the String object.
1. String Class:
There are two ways to create String object:
1. By string literal
2. By new keyword
________________________________________
1) String Literal:
We have seen that string object can be created by using the constructors which takes argument as a char array of string object or ASCII array (i.e. byte) using new.
The easiest way to create the string object is by using the literals.
When you are using the string literal, java automatically constructs a string obj. so we can use String Literals to initialize the String object.
String str = “kedar”;
Here, “kedar” is literal which initializes the str object with kedar string.
Java String literal is created by using double quotes. For Example:
String str="Good day";
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 string doesn't exist in the pool, a new string instance is created and placed in the pool.
For example:
String s1="Good day ";
String s2="Good day "; //will not create new instance
In the above example only one object will be created. Firstly JVM will not find any string object with the value "Good day" in string constant pool, so it will create a new object. After that it will find the string with the value "Good day" in the pool, it will not create new object but will return the reference to the same instance.
String objects are stored in a special memory area known as string constant pool.
Question: Why java uses concept of string literal?
Answer: Due to string literal concept memory does not get waste and also efficient memory use because no new string objects are created if it exists already in string constant pool.
2) By new keyword:
String str = new String ("Good Day");
Creates string object on heap memory and one reference variable that is str on stack memory.
StringBuffer class and StringBuilder class:
StringBuffer is peer class of String class. While String class creates strings of fixed length and cannot mutable in terms of content. While StringBuffer and StringBuilder class creates Strings of flexible length that can be modified in terms of both length and content.
We can insert characters and substrings in middle of a string or append another string to the end of any string.
Please like comment share and subscribe.
Prof. Amol M. Jagtap
For more contact : 7972761065
String is a sequence of characters in java.
char array[] = new char[4];
char array[0] = ‘J’;
char array[1] = ‘a’;
char array[2] = ‘v’;
char array[3] = ‘a’;
In java strings are class objects and implemented using three classes, namely,
1. String
2. StringBuffer
3. StringBuilder
In java string is not a character array and is not NULL terminated like in C and C++ language.
Strings may be declared and created as follows:
String StringName; //String Declaration
String Name = new String (“String”); //String Initialisation
Example:
String Name = new String(“Sagar”);
Like array, it is possible to get the length of string using the length method of String class.
Java strings can be concatenate using ‘+’ operator.
Every immutable object in Java is thread safe that implies String class is also thread safe. String cannot be used by two threads simultaneously.
If we create String reference variable and once assigned string object to it then we cannot change the string object.
String demo = " Amol " ;
The above string object is stored in constant string pool and its value cannot be modified.
demo="Jagtap" ;
new "Jagtap" string is created in constant pool and referenced by the demo reference variable but "Amol" string still exists in string constant pool and its value is not overridden but we lost reference to the "Amol" string.
Java String class provides a lot of methods to perform operations on string such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
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.
The Java String is immutable which means it cannot be changed. Whenever we change any string, a new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes.
We will discuss immutable string later. Let's first understand what String in Java is and how to create the String object.
1. String Class:
There are two ways to create String object:
1. By string literal
2. By new keyword
________________________________________
1) String Literal:
We have seen that string object can be created by using the constructors which takes argument as a char array of string object or ASCII array (i.e. byte) using new.
The easiest way to create the string object is by using the literals.
When you are using the string literal, java automatically constructs a string obj. so we can use String Literals to initialize the String object.
String str = “kedar”;
Here, “kedar” is literal which initializes the str object with kedar string.
Java String literal is created by using double quotes. For Example:
String str="Good day";
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 string doesn't exist in the pool, a new string instance is created and placed in the pool.
For example:
String s1="Good day ";
String s2="Good day "; //will not create new instance
In the above example only one object will be created. Firstly JVM will not find any string object with the value "Good day" in string constant pool, so it will create a new object. After that it will find the string with the value "Good day" in the pool, it will not create new object but will return the reference to the same instance.
String objects are stored in a special memory area known as string constant pool.
Question: Why java uses concept of string literal?
Answer: Due to string literal concept memory does not get waste and also efficient memory use because no new string objects are created if it exists already in string constant pool.
2) By new keyword:
String str = new String ("Good Day");
Creates string object on heap memory and one reference variable that is str on stack memory.
StringBuffer class and StringBuilder class:
StringBuffer is peer class of String class. While String class creates strings of fixed length and cannot mutable in terms of content. While StringBuffer and StringBuilder class creates Strings of flexible length that can be modified in terms of both length and content.
We can insert characters and substrings in middle of a string or append another string to the end of any string.
Please like comment share and subscribe.
Prof. Amol M. Jagtap
For more contact : 7972761065