Constructing a String

You can create a string object from a string value of from an array of characters. To create a string from a string literal, us a syntax like this one:

String str1=new String("Hello");

Hello is string literal a sequence of characters enclosed inside double quotes. 
Java treats a string literal as a String object. So the Statement is valid:-

 String str1="Hello";

You can also create a string from an array of characters. For example, the following statements create the string "Hello".

char[] charary={'H,'e','l','l','o'};
String message = new String(charary);
Note:-    A String variable holds a reference to a String object that stores a string value. Strictly speaking, the term String variable. String object and String value are different, but most of time the distinctions between them can be ignored. For simplicity the term string will often be used to refer to String variable, String object and String value.

class constructstring
{
public static void main(String[] arg)
{
String str1=new String("Hello");
System.out.println(str1);
String str2="Hello";
System.out.println(str2);
char[] charary={'H','e','l','l','o'};
String str3=new String(charary);
System.out.println(str3);
}
}


Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons