Java
  Home arrow Java arrow Page 11 - A Closer Look at Methods and Classes
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

A Closer Look at Methods and Classes
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2005-06-30

    Table of Contents:
  • A Closer Look at Methods and Classes
  • Overloading Constructors
  • Using Objects as Parameters
  • A Closer Look at Argument Passing
  • Returning Objects
  • Recursion
  • Introducing Access Control
  • Understanding static
  • Introducing final
  • Introducing Nested and Inner Classes
  • Exploring the String Class
  • Using Command-Line Arguments

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Closer Look at Methods and Classes - Exploring the String Class


    (Page 11 of 12 )

    Although the String class will be examined in depth in Part II of this book, a short exploration of it is warranted now, because we will be using strings in some of the example programs shown toward the end of Part I. String is probably the most commonly used class in Java’s class library. The obvious reason for this is that strings are a very important part of programming.

    The first thing to understand about strings is that every string you create is actually an object of type String. Even string constants are actually String objects. For example, in the statement

    System.out.println("This is a String, too");

    the string “This is a String, too” is a String constant. Fortunately, Java handles String constants in the same way that other computer languages handle “normal” strings, so you don’t have to worry about this.

    The second thing to understand about strings is that objects of type String are immutable; once a String object is created, its contents cannot be altered. While this may seem like a serious restriction, it is not, for two reasons:

    • If you need to change a string, you can always create a new one that contains the modifications.

    • Java defines a peer class of String, called StringBuffer, which allows strings to be altered, so all of the normal string manipulations are still available in Java. (StringBuffer is described in Part II of this book.)

    Strings can be constructed a variety of ways. The easiest is to use a statement like this:

    String myString = "this is a test";

    Once you have created a String object, you can use it anywhere that a string is allowed. For example, this statement displays myString:

    System.out.println(myString);

    Java defines one operator for String objects:+. It is used to concatenate two strings. For example, this statement

    String myString = "I" + " like " + "Java.";

    results in myString containing “I like Java.”

    The following program demonstrates the preceding concepts:

    // Demonstrating Strings.
    class StringDemo {
     
    public static void main(String args[]) {
       
    String strOb1 = "First String";
       
    String strOb2 = "Second String";
       
    String strOb3 = strOb1 + " and " + strOb2;
       
    System.out.println(strOb1);
       
    System.out.println(strOb2);
       
    System.out.println(strOb3);
     
    }
    }

    The output produced by this program is shown here:

    First String
    Second String
    First String and Second String

    The String class contains several methods that you can use. Here are a few. You can test two strings for equality by using equals( ). You can obtain the length of a string by calling the length( ) method. You can obtain the character at a specified index within a string by calling charAt( ). The general forms of these three methods are shown here:

    boolean equals(String object)
    int length( )
    char charAt(int index)

    Here is a program that demonstrates these methods:

    // Demonstrating some String methods.
    class StringDemo2 {
     
    public static void main(String args[]) {
        String strOb1 = "First String";
        String strOb2 = "Second String";
        String strOb3 = strOb1;
       
    System.out.println("Length of strOb1: " +
                           strOb1.length());
       
    System.out.println("Char at index 3 in strOb1: " + 
                           strOb1.charAt(3));
       
    if(strOb1.equals(strOb2))
          System.out.println("strOb1 == strOb2");
        else
          System.out.println("strOb1 != strOb2");
       
    if(strOb1.equals(strOb3))
          System.out.println("strOb1 == strOb3");
        else
          System.out.println("strOb1 != strOb3");
      }
    }

    This program generates the following output:

    Length of strOb1: 12
    Char at index 3 in strOb1: s
    strOb1 != strOb2
    strOb1 == strOb3

    Of course, you can have arrays of strings, just like you can have arrays of any other type of object. For example:

    // Demonstrate String arrays.
    class StringDemo3 {
      public static void main(String args[]) {
        String str[] = { "one", "two", "three" };
       
    for(int i=0; i<str.length; i++)
          System.out.println("str[" + i + "]: " +
                              str[i]);
      }
    }

    Here is the output from this program:

    str[0]: one
    str[1]: two
    str[2]: three

    As you will see in the following section, string arrays play an important part in many Java programs.

    More Java Articles
    More By McGraw-Hill/Osborne


     

    Buy this book now. This article is excerpted from chapter 7 of Java: the Complete Reference, J2SE 5 Edition, written by Herbert Schildt (McGraw-Hill/Osborne, 2004; ISBN: 0072230738). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT