Strings in java and their behaviour


String which we know as array of character are real live saviour in case of java. Java provide strings a special treatement just the way it does for its primitive datatypes. you don't know strings? start here.

The Array of character is not very common in the Java, like in other languages. Java provide a high level interface to manipulate the character arrays which is commonly known as string. Java provide the String Class for the purpose.

Java String class is a higher level interface to intercept and manipulate the character arrays. String is not a primitive data type but the class is made in such a way that it behave like a primitive type. Java String allows you to form string literals like you can do with the String is actually a collection of character. In Java you write character using Single Quote(') and String is written in Double Quotes(").
for example you can do it like this, and this is called character literal and string literal respectively.


char c = 'a'; //creating a character literal
String s = "string"; //creating a string literal

In you can create the character array like this


char[] c = {'a','b','c','d'}; //character array

you can directly create this array into the string using the string constructor*


String s = new String(c);

*String has several constructors which can be used to create the string. You can join the string to form a one whole** string using the '+' operator. for example


String a = "hello";
a = a+"world!";
System.out.print(a); //prints, hello world!

**The new string form is actually a completely new object with the older one already existing, but as the reference of the old string is now abandoned so this is eligible for the garbage collection. This whole process is known as the Mutation. So keep that in mind that String class is mutable class. To prevent the mutation there is another class provided which is known as String Buffer.

Its not only that you can create and use strings you get several built in functions with String class using which you can perform several operations on a string. lets take a few demonstrations to check out a few useful ones.

Length of a string: int length();
gives the length of string, i.e. number of characters in a string including the white spaces too.


System.out.print("hello world!".length()); //prints, 12

The return type of this method is int, but because print() is already overloaded to take up different type of parameters there is not problem in printing this method directly.

Finding a character in string at any specified position: char charAt(int);


System.out.ptint("Hello World!".charAt(1)); //prints, e

please note that this prints second character because this treats string as an array of characters and starts counting from 0. Finding if the sub string exists in the string: int indexOf() overloaded This method is overloaded to find positions of different type of parameters, mainly int and string itself, but you can define the location from where to start and where to go. for example:


System.out.print("hello world".indexOf("o")); //prints, 4, again it counts from zero.

similarly you can use int lastIndexOf(); to find the last occurrence of the sub string. If you want to convert the string into some other primitive type, there is a method with signature: type valueOf(String); in all the wrapper
*** classes of primitive types


Float.valueOf("2.35"); //returns float value
Integer.valueOf("2"); //returns int value
Boolean.valueOf("true"); //return boolean value

***wrapper classes are the classes which are defined for the primitive data types to provide a classful use to them. Wherever you need to use a primitive type as a class you can use the wrapper classes to make an object and use it. Similarly there is a method String toString(), which can be used to convert any type into a String type.


int a = 24;
a.toString(); //returns 24 as string
float f = 2.008;
f.toString(); //returns 2.008 as string

Replacing a part of string: String replace(old, new), replaceAll(regex,new), replaceFirst(old, new)
This replaces the occurrence of each character/string with the second parameter.


“hello World”.replace(‘o’,’m’); //prints, hellm Wmrld
“hello world”.replaceFirst(‘o’,’m’); //prints, hellm world
“123456789”.replaceAll(‘/[0-9]/’,’o’); //prints, ooooooooo

similarly there are several other methods provided with the String class which can be used on the String types to make appropriate use.