Monday, January 30, 2012

Best String examples with tutorials in java

String class or object in java

Strings are a sequence of characters in any language. For Java, the sequence of characters is stored in the form of java.lang.String object. Strings are immutable objects, meaning once created, the contents of the string, can not be modified in memory that means string objects are read-only. String class is final class meaning we can't able to override the behavior of the methods defined in strings.

why String objects are read-only:-
String s="hadoop";
s="cloud"+s;

In the above example, string 's' is created with value as Hadoop, in the next line, appending the string with the cloud that means, string "cloud" object is created,
and the new string is created with value 'cloudhadoop' which is referenced by s, original value "Hadoop" is unreferenced

ways to String object creation:-

there are many ways to create a string object in java using overloaded constructors
String cloudString=new String(); string object created without value

String cloudString=new String("cloudhadoop"); string object created with cloudhadoop value
String cloudChars="{'c','l','o','u','d'}"
String cloudString=new String(cloudChars);
String cloudString="cloudhadoop"
String cloudString=new String(cloudChars);

Length of the string.

to find out the number of characters in the string, String. length method is used.

string.length();

Concrete strings or adding the strings in java:-

we can use either concat or plus operator to add the strings. Both will be used to add the strings at the end.

String sObj="cloud";
String addedString="hadoop+sObj; or

String sObj="cloud";
String addedString="s.concat("hadoop");

Comparison of two Strings:-


String sObj="cloud";
String addedString="cloud";
In the above how many objects are created? the answer is only one object but the references are created two which are pointing to the cloud object.

In Java, we can compare references the two strings using "==" operator
if we want to compare two string objects data, we can use the equals method.
In the above example, == returns true and equal method returns true.

we can also use the equalsIgnoreCase method to ignore the string case sensitivity

How to Split a string into substrings:-

strings are separated into a list of substrings with delimiter specified using String.split(String) method
String s="Best String examples";
String delimeter=" ";
String array[]=s.split(delimeter);
output:- Best String examples

format String with an example in java:-

format method is provided in String object which is similar to c language println format. To format the one of the string, we use codes to represent the different value types.

String.format("Hi %s, today is %d day of month","Kiran",29);

and the output is Hi Kiran, today is 29 day of the month.

%s represents strings type

%d represents integer type

%f represents float type

capital codes represent capital types of types

Convert String to Integer in java

This is common requirement encountered in programmer life
String should contains numbers enclosed in double quotes.
We can convert in two ways. One way is to using Integer.parseInt() method which returns primitive int value.
Other way is using Integer.valueOf() metthod which returns Integer Object.

It will convert to Integer
String numberString = "90";
int number = Integer.parseInt(numberString);
System.out.println(number); // returns 90
String numberString1 = "94";
int number1 = Integer.valueOf(numberString1);
System.out.println(number1); // returns 94
String numberString2 = "94abc";
int number2 = Integer.valueOf(numberString2);
System.out.println(number2); // returns java.lang.NumberFormatException: For input string: "94abc"
}

if the number in string format is not a valid number, It throws NumberFormatException


Hope you understand the basics of Strings in java