Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Introduction To String

Let us discuss String through an example. On your Eclipse IDE, click on the package 'com.example.core' and then create a new Java file and name it as StringExample.java.

  package com.example.core;

  /**
   * String represents a series of characters. 
   * The fully qualified name of it is java.lang.String.
   * 
   * @author Rolan Liwanag
   *
   */
  public class StringExample {

    public static void main(String[] args) {
	  String aString = "abcde";
	  System.out.println("The value of aString is " 
	    + aString);
	  aString = "The quick brown fox jumped over the " 
	    + "lazy dog.";
	  System.out.println("The value of aString is " 
	    + aString);
	  String anotherString = "The quick brown fox jumped "
	    + "over the lazy dog.";
	  System.out.println("The value of anotherString is " 
		+ anotherString);
	  /*
	   * At this point, there are 2 variables, aString and 
	   * anotherString. Both have the same value. When we 
	   * declared anotherString, JVM will check if a string 
	   * with the same value exists in the string constant  
	   * pool. A reference to that instance will be  
	   * returned. Thus, in this case, no new object is 
	   * created. If its too technical, let it be for now.  
	   * The important thing is, you now know how to 
	   * declare and set the value of a string.
	   */
	  String bString = new String("a value");
	  System.out.println("The value of bString is " 
	    + bString);
	  String cString = new String("a value");
	  System.out.println("The value of cString is " 
	    + cString);
	  /*
	   * This method of declaring a string is using the 
	   * 'new' keyword. Every time we use the 'new'  
	   * keyword, we are creating an instance of that 
	   * class. This means, a new object is created in the 
	   * normal heap memory. In this case, there will be 2 
	   * String objects in the heap memory. Why did Java 
	   * introduced the concept of String literal? It is 
	   * for memory efficiency.
	   */
    }
  }
                            

String Concatenation and Substring

The next example will show you how to manipulate strings. Create a new Java file and name it as StringOperations.java. Place it under the package 'com.example.core'.

  package com.example.core;

  /**
   * This class discusses the basics of string operations.
   * 
   * @author Rolan Liwanag
   *
   */
  public class StringOperations {

    public static void main(String[] args) {
	  //The substring method
	  String aString = "The quick brown fox jumped over " 
	    + "the lazy dog.";
	  /*
	   * 0 is the index where it will start capturing a 
	   * character. 9 is the index where it wont capture a
	   * character any longer. So technically, the last 
	   * character captured is on index 8.
	   */
	  String aStringPart1 = aString.substring(0, 9);
	  /*
	   * 9 is the index where it will start capturing a 
	   * character. aString.length() is the index where it
	   * wont capture a character any longer. So 
	   * technically, the last character captured is on 
	   * index aString.length()-1.
	   * NOTE : aString.length() is the total number of 
	   * characters in the string. In this case, it is 45. 
	   * The last index is 44.
	   */
	  String aStringPart2 = 
	    aString.substring(9, aString.length());
	  System.out.println("The value of aStringPart1 is " 
	    + aStringPart1);
	  System.out.println("The value of aStringPart2 is " 
	    + aStringPart2);
	  //aString remains unchanged
	  System.out.println("The value of aString is " 
	    + aString);
		
	  //String concatenation
	  String concatenatedString = aStringPart1 + " -" 
	    + aStringPart2 + "     ";
	  System.out.println("The value of concatenatedString" 
	    + " = " + concatenatedString);
	  /*
	   * To concatenate a string, simply join the strings 
	   * with the + sign.
	   */
	  System.out.println("The length of concatenatedString" 
	    + " = " + concatenatedString.length());
		
	  //The trim method removes trailing spaces at the 
	  //beginning and at the end of the given string.
	  //remember, the concatenated strings might have 
	  //contained a lot of trailing spaces
	  String trimedString = concatenatedString.trim();
	  System.out.println("The length of trimedString is " 
	    + trimedString.length());
	  String anotherTrimmedString = "   3 spaces in front " 
	    + "  3 spaces in the middle and 3 spaces at the " 
	    + "end.   ".trim();
	  System.out.println("The value of anotherTrimmedString" 
	    + " is " + anotherTrimmedString);
	  //take note that it did not remove the trailing 
	  //spaces in the middle of the string.
		
	  //Splitting a string
	  String[] splitStringArr = trimedString.split(" ");
	  /*
	   * The above means, split the given string by 
	   * whenever we encounter the character space.
	   */
	  System.out.println("The length of the splitStringArr " 
	    + "array is " + splitStringArr.length);
	  System.out.println("The value of splitStringArr[0] is" 
	    + " " + splitStringArr[0]);
	  System.out.println("The value of splitStringArr[1] is" 
	    + " " + splitStringArr[1]);
	  System.out.println("The value of splitStringArr[2] is" 
	    + " " + splitStringArr[2]);
	  System.out.println("The value of splitStringArr[3] is" 
	    + " " + splitStringArr[3]);
	  //trimmedString should still be the same
	  System.out.println("the value of trimedString is " 
	    + trimedString);
	}
  }

                            



Back    Next