Professional Documents
Culture Documents
String
String
String
String
Strings, are a sequence of characters.
By new keyword
By char to string
By string literal
By declaring a string variable and assigning a group of
Syntax:
String <variable_name>=value;
Ex:
String s="welcome";
Ex:1
public class String1
{
public static void main(String[] args) {
String s="Welcome java";
System.out.println(s);
}
}
By new keyword
Syntax:
Ex:
System.out.println(s);
}
By char to string
Converting the character array into strings.
Syntax:
Ex:
char ch[]={'s','t','r','i','n','g','s'};
String s="Sachin";
s.concat("Tendulkar");
System.out.println(s);
}
Java String Buffer class
Java StringBuffer class is used to create mutable (modifiable)
string.
mutable
Syntax:
Ex:
(modifiable) string.
The Java StringBuilder class is same as StringBuffer
Ex:
StringBuilder sb = new StringBuilder(“Hello”);
Ex:
public class StringBuild
{
public static void main(String[] args)
{
StringBuilder sb=new StringBuilder("Hello ");
System.out.println(sb);
}
}
Difference between String and StringBuffer
Difference between StringBuffer and
StringBuilder