String
String
Strings, are a sequence of characters.
In Java programming language, strings are treated as objects.
The [Link] class is used to create a string object
How to create a string object?
There are two ways to create String object:
By string literal
By new keyword
By char to string
By string literal
By declaring a string variable and assigning a group of
characters to the string variable.
Java String is created by using double quotes.
Syntax:
String <variable_name>=value;
Ex:
String s="welcome";
Ex:1
public class String1
{
public static void main(String[] args) {
String s="Welcome java";
[Link](s);
}
}
By new keyword
Using the new operator, create a String object by passing the
string value as an argument to the String.
Java String is created by using new keyword.
Syntax:
String <object> = new String(<value>);
Ex:
String s=new String("welcome to java“);
Ex:2
public class String1 {
public static void main(String[] args)
String s= new String("Welcome java");
[Link](s);
}
By char to string
Converting the character array into strings.
Syntax:
String <object> = new String(<Char_Array>);
Ex:
char ch[]={'s','t','r','i','n','g','s'};
String name = new String(ch);
Ex:3
public class StringExample
{
public static void main(String args[])
{
char ch[]={'s','t','r','i','n','g','s'};
String s1=new String(ch);//converting char array to string
[Link](s1);
}
}
Strings Are Immutable
Strings are immutabe which means, their value cannot be
changed once they are created.
Is String A Class Or A Datatype?
String is a class defined in [Link] package.
In Java, every class is considered as user defined data type.
So we can take string as a data type.
String are immutable example
classTestimmutablestring
public static void main(String args[])
String s="Sachin";
[Link]("Tendulkar");
[Link](s);
}
Java String Buffer class
Java StringBuffer class is used to create mutable (modifiable)
string.
The StringBuffer class in java is same as String class except it is
mutable
i.e. it can be changed.
Java StringBuffer class is thread-safe i.e. multiple threads cannot
access it simultaneously. So it is safe and will result in an order.
Creating Stringbuffer Objects
String Buffer can be defined simply by the use of new operator
and by passing the string value inside the String Buffer.
Syntax:
StringBuffer <object> = new StringBuffer(String _ value);
Ex:
StringBuffer sb = new StringBuffer(“Hello”);
Ex:
public class StringBuf
{
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer("Hello ");
[Link](sb);
}
}
Java StringBuilder class
java StringBuilder class is used to create mutable
(modifiable) string.
The Java StringBuilder class is same as StringBuffer
class except that it is non-synchronized.
It is available since JDK 1.5.
Creating Stringbuilder Objects
StringBuilder can be defined simply by the use of new operator
and by passing the string value inside the StringBuilder
constructor.
Syntax:
StringBuilder <object> = new StringBuilder(String_value);
Ex:
StringBuilder sb = new StringBuilder(“Hello”);
Ex:
public class StringBuild
{
public static void main(String[] args)
{
StringBuilder sb=new StringBuilder("Hello ");
[Link](sb);
}
}
Difference between String and StringBuffer
Difference between StringBuffer and
StringBuilder