Sunday, August 19, 2012

Java Strings

In Java String is not a primitive type .Strings are objects instance of a String class.

Example of String variable declaration

String s = new String("fast Learned");
String s = "fast Learned";

We can declare String variables in both the ways.

String objects are immutable in nature means you can't modify the value of a string object.
You are not convinced with the above statement as you have seen many java statement like
String s = "great" ;
s = s.concat(" effort");
Here we can see that the value of s variable is modified.Let me explain what happened
step 1
 "great" is a new String object assigned to the variable s.
 step 2
 " effort" is also a new String object.
  concat() function is used to concatenate 2 String object and create a new String object with a value of "great effort".

  step 3
   The newly formed String object is assigned to variable s.
 step 4
   We lost the String object "great" and " effort" in this process.
So we are unable to modify the String object only what we did is assigned a new String object to the String variable s




Let me explain the above pic .

String s1 ="fast"  //  We created a String object "fast" and a heap space address is allocated for that
String s2= s1;     // We assigned s1 into s2 means they are pointed to the same memory address.

String s2= "Learned"  // Now what happened the link that was created by the previous statement means pointing to the same memory address is now removed but a new link is established as s2 is now pointing to the memory address of "Learned" String object.

String pool


JVM used to maintain a String pool internally to optimize its memory management.
When we used to declare a String like

String s ="abc";  This means first "abc" is searched in the string pool if exists then refer to the same memory  address and if not then create a new string and referred to the variable and inserted into the pool so that next time onwards JVM don't need to allocate a separate memory address for "abc".

String s = new String; This always creates a new String object and then referred to the String variable.Then the "abc" variable is placed into the String pool.
String a = "abc";
String b = "abc";
System.out.println(a == b);  // True
The above value is true as they both refer to the same memory address in Heap space. The value come from the Java String pool.
String c = new String("abc");
String d = new String("abc");
System.out.println(c == d);  // False
The above value is false as the both create two different objects as they don't point to the same memory address.

Lets discuss about few utility String functions



 char java.lang.String.charAt(int index)

Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

String java.lang.String.concat(String str)

Concatenates the specified string to the end of this string.
If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

boolean java.lang.String.equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

boolean java.lang.String.equalsIgnoreCase(String anotherString)


Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.
Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:
The two characters are the same (as compared by the == operator)
Applying the method java.lang.Character.toUpperCase(char) to each character produces the same result
Applying the method java.lang.Character.toLowerCase(char) to each character produces the same result

int java.lang.String.length()

Returns the length of this string.

String java.lang.String.substring(int beginIndex)

Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

String java.lang.String.toLowerCase()

Converts all of the characters in this String to lower case using the rules of the default locale. This is equivalent to calling toLowerCase(Locale.getDefault()).

String java.lang.String.toUpperCase()

Converts all of the characters in this String to upper case using the rules of the default locale. This method is equivalent to toUpperCase(Locale.getDefault()).


String java.lang.String.trim()

Returns a copy of the string, with leading and trailing whitespace omitted.
If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.
Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).
This method may be used to trim whitespace (as defined above) from the beginning and end of a string.


Code Examples :
package com.fastlearned;

public class StringExample {
 
private String s1 = "fast";
private String s2 ="Learned";

public void showExamples()
{
 System.out.println("charAt = "+s1.charAt(2));
 System.out.println("concat = "+s1.concat(s2));
 System.out.println("equals = "+s1.equals(s2));
 System.out.println("equalsIgnoreCase = "+s1.equalsIgnoreCase("FAST"));
 System.out.println("length = "+s1.length());
 System.out.println("substring = "+s2.substring(3));
 System.out.println("toLowerCase = "+s2.toLowerCase());
 System.out.println("toUpperCase = "+s2.toUpperCase());
 System.out.println("concat = '"+"  great  ".trim()+"'");
 
  
}

public static void main(String[] str)
{
 StringExample stexample = new StringExample();
 stexample.showExamples();
}

}


Output :
charAt = s
concat = fastLearned
equals = false
equalsIgnoreCase = true
length = 4
substring = rned
toLowerCase = learned
toUpperCase = LEARNED
concat = 'great'


No comments:

Post a Comment