1 / 28

String Class in Java

String Class in Java. java.lang Class String java.lang.Object java.lang.String We do not have to import the String class since it comes from java.lang. An object of the String class represents a string of characters. “abcde”;. Two ways to create a String object.

lot
Télécharger la présentation

String Class in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String Class in Java • java.lang Class String • java.lang.Objectjava.lang.String • We do not have to import the String class since it comes from java.lang. • An object of the String class represents a string of characters. “abcde”;

  2. Two ways to create a String object • Create a String literal: String str = "abc"; • With the key word new since it is an object String str = new String(“abc”);

  3. String is a Class: When an object is created it creates an Object Reference • When an object is created a reference to that object is stored in memory. public class Reference { public static void main(String[]args) { Reference r = new Reference(); System.out.println(r); } } Reference@83e35b memory location for object

  4. Storing String objects • Java has 2 types of memory for Strings. 1. Objects stored in a Pool: String s = “Sun”; 2. Objects are stored in the Heap : created with the word new. String s = new String(“Sun”);

  5. String reference String reference "Sun" String reference “Sun" When created this way, Java looks in the pool to see if there is already a String with the same name. If so points to it and has the same reference as other objects with that content. When created WITH THE WORD NEW objects go on the heap. Each object is brand new and has its own reference. It not shared. These are created as new objects and do not have the same reference. Every object created is unique and has its own reference. String s1 = "Sun"; String s2 = s1; String s3 = “Sun”; String s4 = new String(“Sun“); String s5 = new String (“Sun”); There is only one reference created. All objects share the same reference to the object “Sun”. s1 s4 s2 s5 s3 POOL HEAP

  6. String s1 = "Hello"; // String literal String s2 = "Hello"; // String literal String s3 = s1; // same reference String s4 = new String("Hello"); // String object String s5 = new String("Hello"); // String object

  7. Why does this matter? String comparison: There are three ways to compare String objects: • By equals() method (inherited from Object) • By == operator • By compareTo() method

  8. By equals() method: • Equals() method compares the original content of the String. Does it have the same content. public boolean equals(Object another) public booleanequalsIgnoreCase(String another)

  9. Equality using equals(Object o) • String n = "Computer"; • String s = "Computer"; • String t = new String("Computer"); • String u = new String("Computer"); • String v = new String (“computer”); boolean b = n.equals(s); true boolean b = n.equals(t); true boolean b = n.equals(u); true boolean b = n.equalsIgnoreCase(v); true

  10. String reference String reference “Computer" String reference “Computer" By == operator • String n = "Computer"; • String s = "Computer"; • String t = new String("Computer"); • String u = new String("Computer"); t n u s HEAP POOL

  11. == equality • String n = "Computer"; • String s = "Computer"; • String t = new String("Computer"); • String u = new String("Computer"); n == s true n == t false t == u false

  12. If you create a String using new it creates a new String and will not reference the same object. String s1 = new String(“Sun”); String s2 = new String(“Sun”); Sun String Reference String Reference Sun System.out.println(s1 == s2) ; // false two references created System.out.println(s1.equals(s2)); // true says same thing

  13. compareTo method parameters intcompareTo(Object o) // Object o is the object to be compared or intcompareTo(String anotherString) // String to be compared What is returned if the two strings are equal to each other returns 0 . if argument is > than String return value < 0 (negative number) if the argument is < than the string return > 0 (positive number) .

  14. Lexicographic order: • Lexicographic order is a generalization of alphabetical order. • In this ordering, numbers come before letters and capital letters come before lower case letters. Lexicographic comparison is like alphabetizing the Strings. • numbers • uppercase • lower case http://ss64.com/ascii.html

  15. Lexicographical • Upper case characters are regarded as less than lower case characters. • "APPLE".compareTo("apple") returns a negative integer. -32 • Argument is greater than String A ascii code of 65 < a ascii code of 97 • “apple”.compareTo(“APPLE”) returns positive 32 • argument less than String a ascii code of 97 > A ascii code of 65 • “apple”.compareTo(“apple”) returns 0 equal to each other • if the two strings are equal to each other returns 0 . • if argument is > than String return value < 0 (negative number) • if the argument is < than the string return > 0 (positive number

  16. Return Value : if the two strings are equal to each other returns 0 . if argument is greater than String return value < 0 (negative number) if the argument is less than the string return > 0 (positive number) String str1 = "Abc"; String str2 = "abc"; String str3 = "year"; String str4 = "table"; String str5 = "abc"; System.out.println(str1.compareTo(str2)); System.out.println(str2.compareTo(str1)); System.out.println(str3.compareTo(str4)); System.out.println(str5.compareTo(str2)); “ABC to “abc argument is > so negative -32 “abc to “Abc” argument is < so positive 32 “year” to “”table” argument is < so positive 5 “abc” to “abc” equal returns 0 -32 32 5 0

  17. String Class in Java • java.lang Class String • java.lang.Objectjava.lang.String • We do not have to import the String class since it comes from java.lang. • An object of the String class represents a string of characters. “abcde”;

  18. Empty Strings • An empty string has no characters; • Its contents are “null”. String s1 = “"; String s2 = new String(); Empty strings

  19. String indexes • String is a string of characters. Each letter in the string has its own index location and can be accessed by it. index location The length of the String is how many letters it contains: 8 The ending index of the String is length – 1; 7

  20. Index locations • Strings have index locations from • 0 to length-1 • strawberry // 10 letters in the word • // index from 0 to 9 • length() = 10 • length()-1 = 9 • Index starts at 0 and goes to 9 0123456789

  21. String Methods: • Page 78 • There are many ways to manipulate Strings. • Look on page 78 those tested on AP • You always use the period to separate the object from the method. • s1.length();

  22. int length (); returns an int Returns the number of characters in the string Methods — length() String f = “Flower”; String w = “Wind”; Returns: 6 4 int lenF = f.length(); int lenW = w.length();

  23. Strings have index locations from 0 to length-1 Methods — substring String s2 = s.substring (i, k); returns the substring of chars in positions from i to k-1 String s3 = s.substring (i); returns the substring from i char to the end String s = “strawberry”; strawberry i k strawberry i Returns: raw rawberry String s2 = s.substring (2,5); start at 2 end at 4 String s3 = s.substring (2); start at 2 thru end

  24. String n = "Computer"; String one = n.substring(0,7); String two = n.substring(1,6); String three = n.substring(2,5); String four = n.substring(4); String five = n.substring(3); String six = n.substring(1,n.length()-2); String seven = six.substring(0, n.length()/2); • s.substring(i, k); returns the substring of chars in positions from i to k-1 • s.substring(i); returns the substring from i char to the end

  25. Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2. extraEnd("Hello") → "lololo"extraEnd("ab") → "ababab"extraEnd("Hi") → "HiHiHi“ public String extraEnd(String str) { String end = str.substring(str.length()-2); return end+end+end; }

  26. Given a string of even length, return the first half. So the string "WooHoo" yields "Woo". firstHalf("WooHoo") → "Woo"firstHalf("HelloThere") → "Hello"firstHalf("abcdef") → "abc“ public String firstHalf(String str) { String firstHalf = str.substring(0,str.length()/2); return firstHalf; }

  27. Methods — Concatenation String s1 = “obi”; String s2 = “wan”; String result = s1 + s2; obiwan String result = s1.concat (s2); the same as s1 + s2 obiwan

  28. Methods — Find (indexOf) Index of return the index location of the first occurrence of the character requested. String date ="July 5, 2012 1:28:19 PM"; date.indexOf ('J'); 0 date.indexOf ('2'); 8 date.indexOf ("2012"); 8 date.indexOf ('2', 9); 11 date.indexOf ("2020"); -1 date.lastIndexOf ('2'); 15 0 8 11 15 Returns: (starts searching at position 9) (not found)

More Related