{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week10 - Chapter 16 Strings and Characters in Java String...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 16 Strings and Characters in Java 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 1 st 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("Coach Dependahl"); "Coach Dependahl" is a String constant. The 2 nd 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. This approach is used because fixed, immutable strings can be implemented more efficiently than changeable ones. For those cases in which a modifiable string is desired, there is a companion class to String called StringBuffer . StringBuffer objects contain strings that can be modified after they are created. Both the String and StringBuffer classes are defined in java.lang . Thus, they are available to all programs automatically. Both are declared final , which means that neither of these classes may be subclassed. Strings can be constructed in a variety of ways. The easiest is to use a statement like this: String myString = "Coach Dependahl" 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);
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Java defines one operator for String objects: +. It is used to concatenate two strings. For example: String myString = "Coach" + " " + "Dependahl" Results in myString containing "Coach Dependahl" 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( ) . Note, like arrays, the first element of a String is considered to be at position 0. String myString = "Coach Dependahl"; String myString2 = "Coach"; System.out.println("Length of myString=" + myString.length( ) ); System.out.println("Char 3 in myString: " + myString.charAt(2)); If (myString.equals(myString2)) System.out.println("myString equals myString2"); else System.out.println("myString does not equal myString2"); Output Produced: Length of myString=15 Char 3 in myString: a myString does not equal myString2 // Demonstrate String Arrays class StringDemo { public static void main(String args[ ]) { String str[ ] = {"one" , "two" , "three"}; for (int x=0; x<str.length; x++) System.out.println("str[" + x + "]: " + str[x]); } } Output Produced: str[0]: one str[1]: two str[2]: three Command-Line Arguments
Image of page 2
Sometimes you will want to pass information into a program when you run it.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern