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

Info iconThis 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);
Background image of page 1

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

View Full DocumentRight 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
Background image of page 2
Sometimes you will want to pass information into a program when you run it.
Background image of page 3

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

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

Page1 / 14

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online