• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

String constructors

July 17, 2012 By j2eereference 2 Comments

String class supports different types of constructors. Lets see the different types of constructors with example.

Default constructor

1
String str= new String();

The above will create an instance of string without any characters.

String object with character array as parameters

If you want to create String with initial values , We have variety of constructors

String (char chars[]))

We can also specify sub range of character array for initializing string .

String (char chars[],int startIndex,int numberOfCharacters)

Example:

1
2
3
4
5
6
7
8
9
10
package com.j2eereference.coreJava;
public class StringConstructors {
public static void main(String[] args) {
char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
String str1 = new String(chars);
String str2 = new String(chars, 2, 3);
System.out.println(str1);
System.out.println(str2);
}
}

output

abcdef
cde

String object from another string object

We can create string object from another String object as below.

1
2
3
4
5
6
7
8
9
package com.j2eereference.coreJava;
public class StringConstructors {
public static void main(String[] args) {
String str1 = new String("j2eereference");
String str2 = new String(str1);
System.out.println(str1);
System.out.println(str2);
}
}

Output

j2eereference
j2eereference

String constructor with byte array

We can create String object from byte array as below.

String(byte asciiChars[])

and

String(byte asciiChars[],int startIndex,int numberOfCharacters)

1
2
3
4
5
6
7
8
9
10
package com.j2eereference.coreJava;
public class StringConstructors {
public static void main(String[] args) {
byte ascii[] = { 65, 66, 67, 68, 69, 70 };
String str1 = new String(ascii);
String str2 = new String(ascii, 2, 3);
System.out.println(str1);
System.out.println(str2);
}
}

output

ABCDEF
CDE

String constructor with Unicode

Java 1.5 supports Unicode character set as argument in string constructor.

String(int codePoints[],int startIndex,int numberOfCharacters)

Here codePoints is the array contains Unicode points

String constructor with StringBuilder

Java 1.5 supports string constructors with StringBuilder parameter.

String(StringBuilder strBuilder)

1
2
3
4
5
6
7
8
package com.j2eereference.coreJava;
public class StringConstructors {
public static void main(String[] args) {
StringBuilder strBuilder = new StringBuilder("j2eereference");
String str = new String(strBuilder);
System.out.println(str);
}
}

output

j2eereference

 

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Comments

  1. Hemant says

    July 17, 2012 at 6:24 pm

    Really, I impressed by this site. I saw many java sites but this is one of the best site.
    This is simple and full of knowledge.

    Reply
  2. ravi krishnarpan says

    July 20, 2012 at 6:18 pm

    This site is simply the best !Thanks for imparting this kind of knowledge in simple but sublime
    language .

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.