Constructors for string classes in Java

Source: Internet
Author: User
Tags truncated

Constructors in the String class
String (); Constructs an empty string object
String (byte[] bytes); Constructing a string object from a byte array
String (byte[] bytes,int offset,int length); A byte array, starting with offset, a total length of bytes constructs a string object
String (char[] value); Constructing a String object from a char array
String (byte[] char,int offset,int length); string object constructed from offset, with a total length of bytes, through a char array
String (string original); Construct a copy of the original and copy a original
String (stringbuffer buffer); StringBuffer array to construct strings object

public class Stringclasstest {
public static void Main (string[] args) {
byte array
Byte[] Barray = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H '};
Character array
Char[] CArray = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H '};
Declare a StringBuffer
StringBuffer strbuf = new StringBuffer ("Strbuf");
Instance a String Object
String str = new string ("str abcd");
Instance a String object constructs a String object (byte array) through an Btye array
String strb = new string (Barray);
Instance a String object constructs a string object (a character array) through a char array
String strc = new string (CArray);
Instance a String object constructs a String object (byte array, starting data, truncated data length) through a char array
String strbindex = new String (Barray, 1, 5);
Instance a String object constructs a string object (character array, starting data, truncated data length) through a char array
String strcindex = new String (CArray, 1, 2);
Instance a String object constructs a string object from a StringBuffer object
String strbuff = new string (STRBUF);
System.out.println ("instance of an argument-free string object:" +str);
System.out.println ("instance of a string object with a byte array parameter:" +STRB);
System.out.println ("instance of a string object with a char array parameter:" +STRC);
System.out.println ("instance of a string object with a byte array parameter, intercept from 1, intercept 5 bits:" +strbindex);
System.out.println ("instance a string object with char array parameter, intercept from 1, intercept 2 bits:" +strcindex);
System.out.println ("instance one with StringBuffer parameter string object:" +strbuff);
If it is a byte type, the output address
System.out.println (by);
If it is a character type, the output character
System.out.println (c);
}
}

Constructors for string classes in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.