String Comparison

image

Just a reminder about String comparisons. Compare two Strings like this:

if ( s1.equals(s2) )

not like this:

if (s1 == s2)

The first compares string contents, the second, string addresses (this is another artifact of reference types, remember?). Failing to use equals() to compare two strings is probably the most common single mistake made by Java novices.

String uses another built-in class called StringBuffer to help it operate. StringBuffer differs from String in that you can change characters in the middle of a StringBuffer after it has been instantiated. StringBuffer doesn't have any support for searching for individual characters or substrings though. StringBuffer is widely used by the compiler to implement support for concatenating two Strings into a longer String. You'll use the class String in every program; you may never use StringBuffer.

image

JDK 1.5 introduced the class java.lang.StringBuilder. StringBuilder has the same API as StringBuffer, but it has performance optimizations that make it appropriate to use only in single-threaded code. This is an optimization for experts only. All the programs we have seen so far are single-threaded (no two pieces of the code are running in parallel). If you do have multiple threads of control in your programs and you want mutable Strings in those threads, you need to use StringBuffer.

The following code shows the important methods of the String class.

public final class String implements CharSequence, Comparable, Serializable {
           // constructors with various arguments
     public String();
     public String(java.lang.String);
     public String(java.lang.StringBuffer);
     public String(byte[]);
     public String(byte[],int);
     public String(byte[],int,int);
     public String(byte[],int,int,int);
     public String(byte[],int,int,java.lang.String)
                                      throws UnsupportedEncodingException;
     public String(byte[],java.lang.String) throws UnsupportedEncodingException;
     public String(char[]);
     public String(char[],int,int);
           // comparisons
     public char charAt(int);
     public int compareTo(java.lang.Object);
     public int compareTo(java.lang.String);
     public int compareToIgnoreCase(java.lang.String);
     public boolean endsWith(java.lang.String);
     public boolean equals(java.lang.Object);
     public boolean equalsIgnoreCase(java.lang.String);
     public boolean regionMatches(int, java.lang.String, int, int);
     public boolean regionMatches(boolean, int, java.lang.String, int, int);
     public boolean startsWith(java.lang.String);
     public boolean startsWith(java.lang.String, int);
           // search, extract and other routines
     public String concat(java.lang.String);
     public static String copyValueOf(char[]);
     public static String copyValueOf(char[], int, int);
     public byte [] getBytes();
     public void getBytes(int, int, byte[], int);
     public byte [] getBytes(java.lang.String)throws UnsupportedEncodingException;
     public void getChars(int, int, char[], int);
     public int hashCode();
     public int indexOf(int);
     public int indexOf(int, int);
     public int indexOf(java.lang.String);
     public int indexOf(java.lang.String, int);
     public native java.lang.String intern();
     public int lastIndexOf(int);
     public int lastIndexOf(int, int);
     public int lastIndexOf(java.lang.String);
     public int lastIndexOf(java.lang.String, int);
     public int length(); // gets the length of the string
     public String replace(char, char);
     //new in JDK 1.4, splits the string according to the pattern
     public String[] split (String pattern);
     public String[] split (String pattern, int limit);

     public String substring(int);
     public String substring(int, int);
     public char toCharArray()[]; public java.lang.String toLowerCase();
     public String toLowerCase(java.util.Locale);
     public String toString();
     public String toUpperCase();
     public String toUpperCase(java.util.Locale);
     public String trim(); // chops off leading & trailing spaces
           // conversion to String
     public static String valueOf(char);
     public static String valueOf(double);
     public static String valueOf(float);
     public static String valueOf(int);
     public static String valueOf(long);
     public static String valueOf(java.lang.Object);
     public static String valueOf(boolean);
     public static String valueOf(char[]);
     public static String valueOf(char[], int, int);
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.31.163