Artikel Terbaru

Monday, 30 January 2012

Best String examples with tutorials in java


String class or object in java


Strigns are sequence of characters in any language. For java, sequence of characters are 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 readonly.

String class is final class meaning we can't able to overried the behaviour 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 hadooop, in the next line, appending the string with 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 wasy 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 string, String.length method is used.
string.length();

Concate 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 create? 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 equals method.
In the above example, == returns true and equal method returns true.

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


How to Split string into substrings:-

strings are separeted into list of sub strings with delimeter 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 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 month.

%s represents strings type

%d represents integer type

%f represents float type

capital codes represents capital types of types

Hope you understand the basics of Strings in java