String is the sequence of character represented by the object of String class in java. Two other classes’ string buffer and string builder can be used for same thing. String object provides some predefined method those are used to manipulate the string.
String object is said to be the immutable this means that you can not manipulate the string object. But you can work with that object. When you manipulate the string object then new string object is created and it consists of manipulated contents. But it is hard to observe by programmer without experience.
Here to solve this problem you can use the String buffer class to create the string object in which you can manipulate the string contents with the same object. That is the object of string class the mutable.
String buffer is the thread safe.
Another class to create the mutable string objects is string builder class. But it is no thread safe.
Creating String
There are many ways to create the string object
1 | You can declare string reference variable and assign the string lateral to it, then string object is created automatically and the reference of that object is stored in the reference variable you declared. for example String str=”this my first string”; |
2 | You can create the string object using new operator. What you want to write in string is passed in the constructor of string class. there are 11 constructor those are used to create the string through different ways String str=new String(“this is my first string”); |
3 | Creating the string using character array char[] chs = { 'h', 'e', 'l', 'l', 'o', '.'}; String srt = new String(chs); |
4 | Creating the string using sub character array char[] chs = { 'h', 'e', 'l', 'l', 'o', '.'}; String srt = new String(chs,2,2); |
5 | If you leave the string constructor blank, the n it creates the empty string. String str=new String(); |
6 | There is the constructor String (byte[] bytes) which can Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.byte b[]={65,66,67,68,68,70}; String str3=new String(b); System.out.println(str3); Output: ABCDDF |
7 | Creating the string of the sub array of byte from specified position of specified length using the constructor String (byte[] bytes, int offset, int length) byte b[]={65,66,67,68,68,70}; String str6=new String(b,2,3); |
System.out.println(str6);
Output: CDE | |
8 | Creating string from sub array byte by decoding the bytes using specified CharSet. Some Character set are following. US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1 UTF-8 Eight-bit UCS Transformation Format UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark Try{ byte b7[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80}; String str7=new String(b7,0,13,"ISO-8859-1"); System.out.println(str7); }catch(UnsupportedEncodingException e){System.out.println(e);} This constructor throws an exception. |
9 | Creating string of whole byte array This is same as above only difference is that we passing complete byte array in the constructor. Try{ byte b7[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80}; String str7=new String(b7,"ISO-8859-1"); System.out.println(str7); }catch(UnsupportedEncodingException e){System.out.println(e);} |
10 | Crating string using array of Unicode characters int unicodes[]={'\u0041','\u0042','\u0043','\u0044','\u0045','\u0046'}; String str8=new String(unicodes,0,5); System.out.println(str8); Output: ABCDE |
11 | Creating string using anther string String str1="This is first string"; String str2=new String(str1); System.out.println(str2); |
12 | Creating string from string buffer StringBuffer sb=new StringBuffer("Hemraj"); String str12=new String(sb); System.out.println(str12); |
13 | Creating string from string builder StringBuilder sbu=new StringBuilder("Hemraj"); String str13=new String(sbu); System.out.println(str13); |
class MyString
{
public static void main(String args[])
{
String str1="This is first string";
System.out.println(str1);
String str2=new String("This is second string");
System.out.println(str2);
byte b[]={65,66,67,68,69,70};
String str3=new String(b);
System.out.println(str3);
String str6=new String(b,2,3);
System.out.println(str6);
byte b7[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80};
try{
String str7=new String(b7,0,13,"ISO-8859-1");
System.out.println(str7);
}catch(UnsupportedEncodingException e){System.out.println(e);}
int unicodes[]={'\u0041','\u0042','\u0043','\u0044','\u0045','\u0046'};
String str8=new String(unicodes,0,5);
System.out.println(str8);
String str11=new String(str8);
System.out.println(str11);
StringBuffer sb=new StringBuffer("Hemraj");
String str12=new String(sb);
System.out.println(str12);
StringBuilder sbu=new StringBuilder("Hemraj");
String str13=new String(sbu);
System.out.println(str13);
}
}
{
public static void main(String args[])
{
String str1="This is first string";
System.out.println(str1);
String str2=new String("This is second string");
System.out.println(str2);
byte b[]={65,66,67,68,69,70};
String str3=new String(b);
System.out.println(str3);
String str6=new String(b,2,3);
System.out.println(str6);
byte b7[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80};
try{
String str7=new String(b7,0,13,"ISO-8859-1");
System.out.println(str7);
}catch(UnsupportedEncodingException e){System.out.println(e);}
int unicodes[]={'\u0041','\u0042','\u0043','\u0044','\u0045','\u0046'};
String str8=new String(unicodes,0,5);
System.out.println(str8);
String str11=new String(str8);
System.out.println(str11);
StringBuffer sb=new StringBuffer("Hemraj");
String str12=new String(sb);
System.out.println(str12);
StringBuilder sbu=new StringBuilder("Hemraj");
String str13=new String(sbu);
System.out.println(str13);
}
}
Output:
This is first string
This is second string
ABCDEF
CDE
ABCDEFGHIJKLM
ABCDE
ABCDE
Hemraj
Hemraj
Comments