String in java
Strings are extensively used as a data type and it allows the programmer to handle words ,sentences ,phone number,or even just usual numbers.fundamentally String as an object that represents a sequences of character values.
String class
String class can be available in java.lang package. we can use String class in side the lang package .
In java String is a collection of character that can be enclosed in the double quotes.
ex: char []={'j','a','v','a'};
In above example String is a class and str is a object of String class that can be store the characters of string. we can store the character of String but we can't Store the complete String.in java this method is not allowed but other programming languages allows this method like cor c++ etc.
There are several methods are available in String to perform operations of String like concat() method ,replace() method ,reverse() method etc.
example
class Test
{
public static void main(string arg[])
{
string s="viren";
s.concat("shram");
System.out.println(s);
}
}
output
viren
Difference between String and String Buffer class:
There are several difference between String Buffer and Builder class as follows.
1. when we create a object of String class we can't perform any operation of existing object.if we perform any operation of then a new object will be created such type of non-changeable functionality is called immutable string.
ex: String s=new String("viren");
s.concat("sharma");
o/p: viren
when we create object of String Buffer class ,then we can change everything and perform any operation of existing object such kind of changeable functionality is called mutable string.
ex: String s=new String("viren");
s.concat("sharma");
o/p: virensharma
2. In java String class equals() method is override the content comparison .hence objects are different but content must be same than equals() returns true.
ex: String s1=new String(viren)
String s2=new String(viren)
Sopln(s1==s2); //false
sopln(s1.equals(s2)); // true
class Test
{
public static void main(string arg[])
{
string s="viren";
s.concat("shram");
System.out.println(s);
}
}
output
viren
Difference between String and String Buffer class:
There are several difference between String Buffer and Builder class as follows.
1. when we create a object of String class we can't perform any operation of existing object.if we perform any operation of then a new object will be created such type of non-changeable functionality is called immutable string.
ex: String s=new String("viren");
s.concat("sharma");
o/p: viren
when we create object of String Buffer class ,then we can change everything and perform any operation of existing object such kind of changeable functionality is called mutable string.
ex: String s=new String("viren");
s.concat("sharma");
o/p: virensharma
2. In java String class equals() method is override the content comparison .hence objects are different but content must be same than equals() returns true.
ex: String s1=new String(viren)
String s2=new String(viren)
Sopln(s1==s2); //false
sopln(s1.equals(s2)); // true
]
In java String Buffer class equals() method is not over ride for content comparision hence, when objects are different and content must be same then equals() returns fals
StringBuffer sb1=new StringBufferr(viren)
StringBuffer sb2=new StringBuffer(viren)
Sopln(s1==s2); //false
sopln(s1.equals(s2)); // false
StringBuffer sb2=new StringBuffer(viren)
Sopln(s1==s2); //false
sopln(s1.equals(s2)); // false