Java Language

Topics: String, Subroutine, Mutator method Pages: 15 (2935 words) Published: August 22, 2013



A String is a series of characters, example [‘h’,’e’,’l’,’l’,’o’]. In Java, string is an object. The Java platform has String class to create and manipulate strings in a Java application. To create a string, write

String remark = “This is it!”;

In the above statement, “This is it!” is a string literal, a series of characters. This string literal is assigned to a String object named remark. The string literal can be displayed using the Java output statements, System.out.print( ); or System.out.println( );.

Likewise, a String object can be created by using the new operator and a constructor method.

ex: char [ ] remarkArray = {‘A’,’w’,’e’,’s’,’o’,’m’,’e’,’!’}; String remarkString = new String(remarkArray);

The last statement displays the string “Awesome!”.

String length

The String class has a method named, length( ), which returns the number of characters in a string object. In the example below, the length of the string object named len is 7: ex: String palindrome = “racecar”;

int len = palindrome.length( );

note: a palindrome is a word or sentence that is spelled the same forward and backward. Case and punctuation is ignored.
Here is a short application to reverse a palindrome string.
* className: Palindrome
* This application will print a palindrome word.
* @author: The Java Tutorials
* @url: * @comments: Roberto U. Acepcion Jr.
public class Palindrome
public static void main(String[ ] args)
String palindrome = "Dot saw I was Tod";//string is assigned to a String object named palindrome int len = palindrome.length( );//counts the number of characters in the string char[ ] tempCharArray = new char[len]; //uses the value of len as the number of elements char[ ] charArray = new char[len];

// put original string in an array of chars – first loop for (int i = 0; i < len; i++)//uses the value of len to control the loop {
tempCharArray[i] = palindrome.charAt(i); //assigns each character to an element in the array }

// reverse array of chars – second loop
for (int j = 0; j < len; j++)//uses the value of len to control the loop {
charArray[j] = tempCharArray[len - 1 - j]; //assigns each character to an element in the array }

String reversePalindrome = new String(charArray); //creates a new String object //with contents of second array System.out.println(reversePalindrome);//displays the reversed string }


doT saw I was toD

How the program works:
1. To reverse the string, the program converts the string to an array of characters (first for loop( ) ) 2. Then reverses the contents of the first array and puts it into a second array (second for loop( ) ) 3. Afterwards converts the contents of the second array back to a string and assigns it to a String object To shorten the codes, such as eliminating the first loop, use the String class method, getChars( ), to convert a string, or a portion of a string, into an array of characters: palindrome.getChars(0, len, tempCharArray, 0);

String Concatenation

The String class has a method for concatenating two strings: string1.concat(string2);
This returns a new string that is string1 with string2 added to it at the end. You can also use the concat() method with string literals, as in: "My name is ".concat("Rumplestiltskin");

Strings are more commonly concatenated with the + operator, as in "Hello," + " world" + "!" which results in "Hello, world!". The + operator is widely used in print statements. For example:...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Object-oriented Programming and Java Statement Essay
  • Java Ring Essay
  • Java Chapter 2 Quiz Essay
  • Java Model Test Papers for Icse
  • Essay on Java Hw
  • Introduction to Java Programming Essay
  • Computer Guess Paper for C++ and Java
  • PRG 421 JAVA PROGRAMMING II Complete Class Includes All DQs Individual and Team Assignments UOP Latest Essay

Become a StudyMode Member

Sign Up - It's Free