Instantiating Strings
The most appropriate approach to make a string is to use the following statement:
String mystring = “Hi world!”;
At whatever point it experiences a string exacting in your code, the compiler makes a
String object with its value for this situation, “Hi world!’.
Similarly as with other objects, you can make Strings by utilizing a constructor and a new
keyword. The String class has eleven constructors that permit you to give the starting
estimation of the string utilizing diverse sources, for example, a cluster of characters.
public class myStringdemo{
public static void main(string args[]){
char[] myarray = { ‘h’, ‘i’, ‘.’};
String mystring = new String(myarray);
System.out.println( mystring );
}
This would deliver the accompanying result:
hi.
Note: The String class is changeless, so that once it is made a String object, its type can’t
be changed. In the event that there is a need to make a great deal of alterations to Strings
of characters, then you ought to utilize String Buffer & String Builder Classes.
Do'stlaringiz bilan baham: |