ALL ABOUT Java.lang PACKAGE
Discussions
String
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); System.out.println(remarkString);
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: http://docs.oracle.com/javase/tutorial/java/data/strings.html * @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