Search This Blog

Tuesday, 4 October 2011

Strings in Java

0 comments

STRINGS IN JAVA! 
In Java, a string is a sequence of characters that are stored in an object. Strings are defined and handled by a class named String, which is defined in Java’s class library. Every string, be it a constant or variable, is actually an object of type String. For e.g., consider the following statement:
System.out.println(“this is a string, too”);
Here, the string “this is a string, too” is a string constant and also an object.
Java provides three classes for handling strings: String, StringBuffer and StringBuilder. All these classes are declared as final, which means that none of these classes may be sub classed. All these string classes are available in java.lang package and hence they are available to all Java programs automatically.
String objects are immutable in java, i.e., once created, they can’t be altered, i.e., the content of the string can’t be modified. If we want to change a string after its creation, we can do that in any one of the following two ways:
  1. Create a new string with modifications.
  2. Use the peer class of String, called StringBuffer, which allows strings to be altered.
Constructing a String:
Strings can be constructed in a variety of ways. The easiest way is to declare a variable of type String and assign it with a string as follows:
String myString = “Hello, World!”;
After creation, we may use a string object through the program. Two strings can be concatenated (added) to form a new string using the + operator. Following are some examples for creating a new string:
String myString = ”I” + ” like” + ” Java”
String firstname = “Sachin”;
String lastname = “Tendulkar”;
String name = firstname + lastname;
The String class provides several methods for performing operations on strings. Some of the methods are: equals( ), length( ) and charAt( ). The method equals() can be used for testing two strings for equality. The function Length( ) will get you the length of the string. Using charAt( ), we can obtain the character at a specified index within a string.
Command-line Arguments:
Command line arguments are information passed as input to a program while starting an application. They are given as strings followed by the program name on the command line to start the application. One or more arguments can be passed to a program from the command line.
To access the command line arguments from a Java application, we have an array of String parameter in the method main( ). The following program displays all of the command-line arguments that are passed while running it:
class CommandLine {
public static void main(String args[]){
for (int i = 0; i < args.length; i++)
System.out.println(“args[” + i + “]: ” + args[i]);
}
}
This program can be executed after compilation as follows:
java CommandLine This is a test for commandline arguments
The output of this command will be the sequence of strings given after the program name CommandLine as shown below:
This is a test for commandline arguments
String Constructors:
The string class supports several constructors, one of which is the default constructor. The default constructor will create an empty string as follows:
String s = new String();
If we want to create a string with initial value, there are varieties of constructors in the String class. The general forms of these constructors are as follows:


The first parameterized constructor in the above list uses an array of characters to construct the string. All the elements of the character array passed as argument will be used for constructing the string. Example is as follows:
char chars[] = {‘a’, ‘b’, ‘c’};
String s = new String(chars);

Here, the parameter passed for constructing the string is an array of characters. Hence, the first type of parameterized constructor is called, which initializes the string s with the value “abc”. If we want to construct the string using a sub range of characters from the character array, then use the second constructor:
String(char chars[], int stratIndex, int numChars)

In this constructor, three parameters are used: character Array, starting Index of the array and the No. of characters to be used for constructing the string. An example is as follows:
char chars[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’};
String s = new String(chars, 2, 3);

this initializes s with the string “cde”.
The third constructor
String(String strObj)

is used for creating a new string from an existing one. The new string will have the same set of characters that of the old one.

The last two (constructors) are for creating a string from an array containing ASCII values of set of characters. ASCII values are 8-bit numbers assigned to each and every character in the character sets. The first parameter these constructors is an array of type byte that contains the ASCII codes of the characters to use in constructing the string. An example is as follows:
byte ascii[] = {65,66,67,68,69,70};
String s1 = new String(ascii);

This two lines of code will initialize the string s1 with abcdef.

String Literal:

String literal is nothing but a string placed within double quotes. For instance, “abc” is a string literal. For each and every string literal in the program, Java automatically constructors a string object. Thus we can use a string literal to initialize a string object as follows:
String s1 = “abc”;

String Concatenation:
String concatenation is nothing but combing two strings into one. It can be done in two ways: using + operator or using concat( ) method. Concatenation using the first method is similar to Add operation. The second method uses the method of String object for concatenation.
While concatenation, when two strings used as operands, they will be simply joined together. When one of the operands is of different type, it will be converted first before used in concatenation. For e.g.,
String s = “four:” + 2 + 2;

will initialize the string s with the string “four: 22” rather than “four: 4”.

Leave a Reply