Your SlideShare is downloading. ×
07slide
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

07slide

1,047
views

Published on


1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
1,047
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
42
Comments
1
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Transcript

    • 1. Chapter 7 Strings
      • Processing strings using the String class, the StringBuffer/StringBuilder class, and the StringTokenizer class.
      • Use the String class to process fixed strings.
      • Use the StringBuffer/StringBuilder class to process flexible strings.
      • Use the StringTokenizer class to extract tokens from a string.
      • Use the command-line arguments.
    • 2. String
      • String
        • a sequence of characters
        • Treated as object in Java
    • 3. The String Class
      • Declaring a String:
        • String message = "Welcome to Java!"
        • String message = new String("Welcome to Java!“);
        • String s = new String();
      • String Comparisons (equals, compareTo)
      • Substrings (substring(index), substring(start, end))
      • String Concatenation (concat, +)
      • String Length (length())
      • Retrieving Individual Characters in a string
    • 4. String Comparisons
      • equals
      • String s1 = "Welcome";
      • String s2 = "welcome";
      • if (s1.equals(s2))
      • { // s1 and s2 have the same contents }
      • if (s1 == s2)
      • {
      • // s1 and s2 have the same reference
      • }
    • 5. Strings are immutable
      • Strings are immutable. The contents of a string cannot be changed. To improve efficiency and save memory, Java Virtual Machine makes a great effort to identify the identical strings and store them in the same memory location, but it does not guarantee that all of the same strings are stored in the same memory location. Therefore, you must use the equals method to test whether two strings have the same contents, and the == operator to test whether the two strings have the same references (that is, point to the same memory location).
    • 6. Strings are immutable
      • String object is immutable; its contents cannot be changed. So what happen in the following code?
      • String s = “Java”;
      • s = “HTML”;
    • 7. String Comparisons, cont.
      • compareTo(Object object)
      • String s1 = “abc";
      • String s2 = “abg";
      • if (s1.compare(s2) > 0)
      • { // s1 is greater than s2 lexicographically }
      • else if (s1.compare(s2) == 0)
      • { // s1 is equal to s2 lexicographically }
      • else
      • // s1 is less than s2 lexicographically
      • //answer = -4
    • 8. Substrings
      • public String substring(int beginIndex, int endIndex)
        • Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
      • public String substring (int beginIndex)
        • Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
    • 9. Substrings
      • String s1 = "Welcome to Java";
      • String s2 = s1.substring(0,11) + "HTML";
      • // Welcome to HTML
      • String s3 = s1.substring(11)
      • // Java
    • 10. String Concatenation
      • String s3 = s1.concate(s2);
      • Is equivalent to:
      • String s3 = s1 + s2;
    • 11. Finding String Length
      • Finding string length using the length() method:
      • message = "Welcome";
      • message.length() (returns 7 )
    • 12. Retrieving Individual Characters in a String
      • Use message.charAt(index)
      • Index between 0 to message.length() – 1
      • message.charAt(0); //W
    • 13. String Conversions
      • The contents of a string cannot be changed once the string is created. But you can convert a string to a new string using the following methods:
      • toLowerCase
      • toUpperCase
      • trim
      • replace(oldChar, newChar)
    • 14. Convert char and numbers to Strings
      • The String class provides several static valueOf methods for converting a character, an array of characters, and numeric values to strings. These methods have the same name valueOf with different argument types char, char[], double, long, int, and float. For example, to convert a double value to a string, use String.valueOf(5.44). The return value is string consists of characters ‘5’, ‘.’, ‘4’, and ‘4’.
      • To convert string to primitive data types:
      • Double.parseDouble(str);
      • Integer.parseInt(str);
    • 15. For other methods in String class refer to:
      • http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
      • s.indexOf();
      • s1.toCharArray();
    • 16. Example 7.1 Finding Palindromes
      • Objective: Checking whether a string is a palindrome: a string that reads the same forward and backward.
      CheckPalindrome Run
    • 17. The StringBuffer Class
      • The StringBuffer class is an alternative to the String class. In general, a string buffer can be used wherever a string is used. StringBuffer is more flexible than String . You can add, insert, or append new contents into a string buffer. However, the value of a string is fixed once the string is created.
    • 18. The Character Class
      • Java provides a wrapper class for every primitive data type: Character, Boolean, Byte, Short, Integer, Long, Float, Double.
      • java.lang package
      • Character class
        • Character c = new Character (‘a’);
    • 19. StringBuilder/StringBuffer classes
      • Alternative to String class
      • Can be used wherever a string is used
      • More flexible, can add, insert or append new contents, whereas the value of a String object is fixed once the string is created.
      • StringBuilder class is introduced in JDK 1.5, similar to StringBuffer but the update methods in StringBuffer are synchronized.
        • StringBuilder – more efficient if accessed by single task
        • StringBuffer - more efficient if accessed by multiple task concurrently
    • 20. StringBuilder/StringBuffer classes
      • public StringBuffer()
      • No characters, initial capacity 16 characters.
      • public StringBuffer(int length)
      • No characters, initial capacity specified by the length argument.
      • public StringBuffer(String str)
      • Represents the same sequence of characters as the string argument. Initial capacity 16 plus the length of the string argument.
    • 21. Appending New Contents into a String Buffer
      • StringBuffer strBuf = new StringBuffer();
      • strBuf.append("Welcome");
      • strBuf.append(' ');
      • strBuf.append("to");
      • strBuf.append(' ');
      • strBuf.append("Java");
      • strBuf.insert(11, “HTML and ");
      • //”Welcome to HTML and Java”
    • 22. More info on StringBuffer class
      • http://java.sun.com/j2se/1.3/docs/api/java/lang/StringBuffer.html
    • 23. The StringTokenizer Class Constructors
      • The string tokenizer class allows an application to break a string into tokens.
      • The set of delimiters (the characters that separate tokens)
    • 24. The StringTokenizer Class Constructors
      • StringTokenizer(String s)
        • Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is "  f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.
    • 25. The StringTokenizer Class Constructors
      • StringTokenizer(String s, String delim)
        • Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.
    • 26. The StringTokenizer Class Constructors
      • StringTokenizer(String s, String delim, boolean returnTokens)
        • Constructs a string tokenizer for the specified string. All characters in the delim argument are the delimiters for separating tokens. If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens.
    • 27. The StringTokenizer Class Methods
      • boolean hasMoreTokens()
      • String nextToken()
      • String nextToken(String delim)
    • 28. More infor on StringTokenizer Class
      • http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
    • 29. Example 7.4 Testing StringTokenizer
      • Objective: Using a string tokenizer, retrieve words from a string and display them on the console.
      TestStringTokenizer Run
    • 30. Command-Line Parameters
      • class TestMain
      • {
      • public static void main(String[] args)
      • { ... }
      • }
      • java TestMain arg0 arg1 arg2 ... argn
    • 31. Processing Command-Line Parameters
      • In the main method, get the arguments from args[0], args[1], ..., args[n] , which corresponds to arg0, arg1, ..., argn in the command line.
    • 32. Example 7.5 Using Command-Line Parameters
      • Objective: Write a program that will perform binary operations on integers. The program receives three parameters: an operator and two integers.
      Calculator java Calculator + 2 3 java Calculator - 2 3 Run java Calculator / 2 3