Login


Lost your password?

Don't have an account ? Register ×
  
Concept Learning Code Sheets

String declaration, initialisation, addition, copying #3354

Program to demonstrate simple declaration of string, declaration with initialisation and string addition and copying

class StringBasics
{
public static void main(String[] args)
{
String s1;
s1="Hello";
String s2="Programmer!";
System.out.println(s1+'!');
System.out.println(s1+" "+s2);
s2="Java Programmer!";
String s3=s1+" "+s2;
System.out.println(s3);
}
}

String declared and initialised using constructor #3405

Program to demonstrate declaration and initialisation syntax of string using constructor.

class StringConstructor
{
public static void main(String[] args)
{
String st1=new String();
System.out.println("Constructor w/o argument initialises with null string"+st1);
st1="First string initial value";
System.out.println("First string initialised as: "+st1);

String st2=new String("Second string initial value");
System.out.println("Constructor passed with initial value:"+st2);

String st3;
//System.out.println("Uninitialised will give error: "+st1);
st3="Third string initial value";
System.out.println("Third string intialised as: "+st3);
}
}

String as char array and conversion to String object #3407

Program to demonstrate declaration and initialisation syntax of string as character array and then conversion of this array form to string object form using a constructor.

class StringArrChar
{
public static void main(String[] args)
{
char ac[]={'J','a','v','a'};
System.out.println("Char Array will print correctly independently.");
System.out.println(ac);
System.out.println("Char array will not print correctly with + operator "+ac);
String st=new String(ac);
System.out.println("Converted to string prints correctly with + operator: "+st);
System.out.println("After conversion many string functions/methods apply.");
System.out.println("One method is of finding length which in this case is: "+st.length());
System.out.println("One can use many other methods on String class.");
}
}

String as char array and conversion of its part to String object #3505

Program to demonstrate declaration and initialisation syntax of string as character array and then conversion of only a part of this array form to string object form using a constructor.

class StringArrSub
{
public static void main(String[] args)
{
char ac[]={'H','a','b','i','t'};
System.out.print("Full character array is: ");
System.out.println(ac);
String s=new String(ac,3,2);
System.out.println("Sub-Part from index 3 counting 2 characters is: "+s);
String s1=new String(ac,0,3);
System.out.println("Sub-Part from index 0 counting 3 characters is: "+s1);
}
}

String object made from array of bytes #3508

Creating a String class object on the basis of a array of bytes array filled with ascii code values.

class StringArrByte
{
public static void main(String[] args)
{
byte acodes[]={74,97,118,97};
String s1=new String(acodes);
System.out.println(s1);
String s2=new String(acodes,1,2);
System.out.println(s2);
}
}



Code Sheets:5 
Back