Java | Strings

07 min read


A string is a sequence of characters. Java does not implement strings as character arrays but as objects of class String. Even string literals are actually string objects.

(eg) in System.out.println("Hello World"); 

This string literal is also treated as a String object. This makes string handling convenient as Java has predefined methods to compare two strings, concatenate, change the case of the string, search for a substring, etc.

Basically, there are 9 String Constructors as follows:


1.To create an empty string - String s = new String( ); -> creates an instance of String with no characters in it.

's' is the reference variable which stores the reference to the object, thus created. Objects of type String are immutable (ie., its size or the contents cannot be changed). Each time we need an altered version of an existing string, a new String object is created that contains the modifications. The original string is left unchanged.

(eg) System.out.println("Hello World" + "Program");

Here "Hello World" creates one string object with these characters as its data members, "Program" creates another string object. and "+" implies when these 2 are concatenated, the result is neither stored in String object labeled A nor in B. But a new object, which is entirely different from A & B is created with these data members.

  • Fixed immutable strings can be implemented more efficiently than changeable ones.
  • If a modified string is desired, String Buffer and String Builder classes can be used.


2. To create strings with character array - String (char[ ] chars)

(eg) char [ ] vowels = {'a','e','i','o','u'};

String s = new String(vowels);


3. To create a String from a portion of a character array - String(char[ chararray, int startindex, int numchars)

Here "startindex" is the index at which the subrange starts and "numchars" is the number of characters to use.

(eg) char [ ] vowels =  {'a','e','i','o','u'};

String s = new String(vowels,2,3);

From 2nd character use 3 characters to build the string.


4. To create a String from a byte array

Though Java's char type uses 16 bits to represent the basic Unicode char set, the typical format for strings on the Internet uses arrays of 8-bit bytes constructed from ASCII char set.

Thus if the programmer is assured that the characters in a string are from ASCII chat set, it is advisable to use byte data type rather than char. So, String class provides constructors that initialize a string when a byte array is given.

String (byte[ ] byte array) Which contains integers within the range of -128 to +127


5. To create a String from a portion of the byte array-  String(byte[ ] bytearray, int startindex, int numchars)

(eg) byte[ ] bytearray = {65 66,67,68,69,70);

Even if negative numbers are given, the equivalent characters are displayed

String s 1 = new String(bytearray);

System.out.println(s1); ---> ABCDEF

String s2 = new String(bytearray,3,3)

System.out.println(s2); ---> DEF

Note: In both (4) and (5), the byte to character conversion is done by using default character encoding of the platform.


6.To construct a String object that contains the same character sequence as another object - String(Sring strObi)  ---> string object

(eg) char[ ] c = { 'J','A','V','A'};

String s1=new String(c);

String s2 = new String(s 1);

System.out.println(s1); ---> JAVA

System.out.println(s2); ---> JAVA


7.To build a String from a StringBuffer object - String(StringBuffer sbuff)


8.To construct a String from a StringBuilder object - String(StringBuilder sbuild)


9.To construct a String from an array that contains Unicode code points -The array should be an integer array to store the Unicode sequences.

String(int[ ] codepoints, int startindex, int numchars)


String, StringBuffer, and StringBuilder classes are defined in java.lang - Hence, available to all programs, by default.

  • These classes are final ie., none of these can be subclassed
  • All these classes implement CharSequence interface.

But a string reference variable of one object can be changed to point to some other string object, at a later point of time.

  • Input (stdin)

    Output (stdout)

    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)


    Expected Output


    Compiler Message