Understanding The Importance Of Strings


Vaibhav Pandey By: Vaibhav Pandey

Strings are very important part of any programming language. Same is the case with Java. There are so many String methods in String class and it will become difficult to explain all of them. I am mainly focusing on some methods which are worthful to beginners. I will also explain some basics about Strings here which makes Strings easy to understand. So, lets begin.

Basics about Strings in Java

1.You can create Strings in various ways:-
a) By Creating a String Object
String s=new String(”abcdef”);

b) By just creating object and then referring to string
String a=new String();
a=”abcdef”;

c) By simply creating a reference variable
String a=”abcdef”;

2.All the strings gets collected in a special memory are for Strings called ” String constant pool”.

3.JVM does all string related tasks to avoid the memory wastage for more info on this refer “How JVM Handles strings”.

4.Every String is considered as a string literal.

5.Strings are immutable only reference changes string never changes.New string literals are referenced when there is any manipulation.The old string gets lost in the preceding.look below

String s=”abcd”;
s=s+”efgh”;

Important String Methods
Following are most commonly used methods in the String class.

1.public String concat(String s)
This method returns a string with the value of string passed in to the method appended to the end of String which used to invoke the method.

String s=”abcdefg”;
System.out.println(s.concat(”hijlk”));

Note:-Always use assignment operator in case of concat operator otherwise concat will be unreferenced and you will get old String.example

s.concat(”hijkl”);
System.out.println(s);

It will present output as ” abcdefg ” different than what we have expected.So always be careful in using the assignment operator in String method calls.

2.public charAt(int index)
This method returns a specific character located at the String’s specific index.Remember,String indexes are zero based.Example

String s=”Alfanso Mango”;
System.out.println(s.charAt(0));

The output is ‘A’

3.public int length()
This method returns the length of the String used to invoke the method.example:-

String s=”name”;
System.out.println(s.length());

The output is 4

4.public String replace(char old,char new)
This method return a String whose value is that of the String to invoke the method ,updated so that any occurrence of the char in the first argument is replaced by the char in the second argument .Example:-

String s=”VaVavavav”;
System.out.println(s.replace(’v',’V'));

The output is VaVaVaVaV

5.public boolean equalsIgnoreCase(String s)
This method returns a boolean value depending on whether the value of the string in the argument is the same as the String used to invoke the method.This method will return true even when character in the string object being compared have different cases.Example:-

String s=”Vaibhav”;
System.out.println(s.equalsIgnoreCase(”VAIBHAV”));

The output is true

6.public String substring(int begin) / public String substring(int begin,int end)
substring method is used to return a part or substring of the String used to invoke the method.The first argument represents the starting location of the substring.Remember the indexes are zero based.example:-

String s=”abcdefghi”;
System.out.println(s.substring(5));
System.out.println(s.substring(5,8));

The output would be
” fghi “
” fg “

7.public String toLowerCase()
This method returns a string whose value is the String used to invoke the method,but with any uppercase converted to lowercase.:-

String s=”AbcdefghiJ”;
System.out.println(s.toLowerCase());

Output is ” abcdefghij “

8.public String trim()
This method returns a String whose value is the String used to invoke the method ,but with any leading or trailing blank spaces removed.Example:-

String s=”hey here is the blank space “;
System.out.println(s.trim())

The output is ” heyhereistheblankspace”

9.public String toUpperCase()
This method returns a String whose value is String used to invoke the method ,but with any lowercase character converted to uppercase.Example:-

String s=”AAAAbbbbb”;
System.out.println(s.to UpperCase());

The output is ” aaaabbbbb “

Above mentioned String methods are most commonly used in String class. Do remember them or otherwise I am always up for you and of course you can refer this site every time you need something.

Comments

About The Author

Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com.

Comments are closed.

















Get Your Site
Submitted for Free
in the World's Largest
B2B Directory!

Email Address:
*URL:
*
*Indicates Mandatory Field
Terms & Conditions