0% found this document useful (0 votes)
3 views2 pages

Lab Programs

The document illustrates using different Java string constructors including string literal, char array, byte array, byte array with encoding, StringBuffer, and StringBuilder.

Uploaded by

rekhakeerti19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Lab Programs

The document illustrates using different Java string constructors including string literal, char array, byte array, byte array with encoding, StringBuffer, and StringBuilder.

Uploaded by

rekhakeerti19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

4.

Implement a java program to illustrate the use of different types of string


class
constructors.
public class Sample {
public static void main(String args[]) {
//Using String Literal
String str1="Hello World";
System.out.println("Using String Literal: " +str1);

//Using Char Array


char[] charArray={'H','e','l','l','o'};
String str2=new String(charArray);
System.out.println("Using char array:" +str2);

//Using Byte Array


byte[] byteArray={72,101,108,108,111};
String str3=new String(byteArray);
System.out.println("Using byte array:" +str3);

//Using Byte Array with character encoding


byte[] byteArrayWithEncoding={72,101,108,108,111};
String str4 =new String(byteArrayWithEncoding,0,5, java.nio.charset.StandardCharsets.UTF_8);
System.out.println("Using byte arraywith encoding: "+str4);

//Using StringBuffer
StringBuffer stringBuffer=new StringBuffer("Hello,StringBuffer");
String str5=new String(stringBuffer);
System.out.println("Using StringBuffer: "+str5);

//Using StringBuilder
StringBuilder stringBuilder=new StringBuilder("Hello, StringBuilder");
String str6=new String(stringBuilder);
System.out.println("Using StringBuilder: "+str6);
}

You might also like