Sie sind auf Seite 1von 4

Java Strings Lawrence M.

Brown
ktee@erols.com

Java Strings
1. Introduction

In Java strings are objects designed to represent a sequence of characters. Because


character strings are commonly used in programs, Java supports the ability to declare String
constants and perform concatenation of Strings directly without requiring access to methods of the
String class. This additional support provided for Java Strings allows programmers to use Strings in
a similar manner as other common programming languages.

• A Java String is read-only and once created the contents cannot be modified.

2. Declaring and Allocating Strings

The String class has seven different constructors. Only the most common constructors will
be presented. For complete information on the String class, access Sun's API page at
http://java.sun.com/products/jdk/1.1/docs/api/java.lang.String.html.

The simplest method to create a String object is to enclose the string literal in quotes and
assign the value to a String object. Creation of a String through assignment does not require the
use of the new operator, for example

String str = "abc";


String language = "Java";

Alternatively, String objects can be created through constructors. The following


constructors are supported:

public String()

Constructs a new String with the value "" containing no characters. The value is not null.

public String( String value )

Constructs a new String that contains the same sequence of characters as the specified
String argument.

public String( char[] value )

Constructs a new String containing the same sequence of characters contained in the
character array argument.

String s1 = new String();


String s2 = new String( "abc" ); // "abc" is first converted to a String and then
// passed as an String argument
String s3 = new String( s2 );

char data[] = { 'a', 'b', 'c' };


String s4 = new String( data );

1998 1 December 31, 1998


Java Strings Lawrence M. Brown
ktee@erols.com

3. String Concatenation

• Java Strings can be concatenated (joined) using the + and += operators to create new Strings.

String language = "Java";


String course = "Introduction to " + language;

course += ", CS 101";

• Every time an operation modifies a String object, a new read-only String object is created.

In the above example, the += operator creates a new String object containing the characters
"Introduction to Java, CS 101". The original memory containing "Introduction to Java" is unchanged
and will be eventually collected by the Garbage Collector because the memory space is no longer
referenced by a variable.

4. Comparing Strings
Java provides a variety of methods to compare String objects, including:

public int compareTo( String str )

Compares the current String object to str, and returns 0 only if the two strings contain the
same sequence of characters (case sensative). A negative value is returned if the current
String is lower in the Unicode set than the String str. A positive value is returned if the
current String is higher in the Unicode set than the String str.

public boolean equals( Object obj )

Compares the current String object with obj and returns true if obj is another String
containing the same sequence of characters; false is returned otherwise.

public boolean equalsIgnoreCase( String str )

Performs a case-insensitive comparison of the current String object with str and returns true
if str is another String containing the same sequence (ignoring case) of characters; false is
returned otherwise.

• Use of the == operator only tests whether two String object references refer to the same object
(memory space). The == operator does not test whether the contents of the two Strings are
equal.

Example == operator Output

String s1 = new String( "Hello" ); String s1: Hello


String s2 = new String( "Hello" ); String s2: Hello
String s3 = s1; String s3: Hello
String s1 == s2 : false
System.out.println( " String s1 == s2 : " + (s1==s2) ); String s1 equals s2 : true
System.out.println( " String s1 equals s2 : " + s1.equals(s2) ); String s1 == s3 : true
System.out.println( " String s1 == s3 : " + (s1==s3) );

1998 2 December 31, 1998


Java Strings Lawrence M. Brown
ktee@erols.com

• s1 and s3 reference the


same memory space,
thus s1 == s3 evaluates
to true. • The object contents of s1 and s2
are equal thus s1.equals(s2) is
true.
• s1 and s2 do not
reference the same
memory space, thus
s1 == s2 evaluates to
false.

• Never use == to test equality of String contents.

• If new is not used to assign a value to a String object, then all other Strings constructed similarly
with the same literal value will refer to the same object in memory.

String s4 = "Hello"; Output


String s5 = "Hello";
String s4 == s5 : true
System.out.println( " String s4 == s5 : " + (s4==s5) ); String s4 equals s5 : true
System.out.println( " String s4 equals s5 : " + s4.equals(s5) );

5. Useful String methods

public char charAt( int index )

Returns the character at the specified index. The index may range from 0 to length() - 1.

public String concat( String str )

Concatenates the specified String to the end of the current (this) String. If the length of the
argument string is 0, then this String is returned.

public int length()

Returns the length of the current String. The length is equal to the number of 16-bit Unicode
characters in the String.

public String toUpperCase()

Converts the String to uppercase.

public String toLowerCase()

Converts the String to lowercase


1998 3 December 31, 1998
Java Strings Lawrence M. Brown
ktee@erols.com

public String trim()

Removes white space from both ends of the String. All characters that have codes less than or
equal to '\u0020' (the space character) are considered to be white space.

public static String valueOf( boolean b )


public static String valueOf( char ch )
public static String valueOf( int inum )
public static String valueOf( long lnum )
public static String valueOf( float fnum )
public static String valueOf( double dnum )

Creates the String representation of the argument.

1998 4 December 31, 1998

Das könnte Ihnen auch gefallen