Professional Documents
Culture Documents
Java I Lecture 12.pps
Java I Lecture 12.pps
Chapter 10
Strings and Characters
or
String myname = new String(“Joe” );
// initialized to Joe
String a = “Ten ”;
int i = 4;
String c ;
c = a + i;
c = a.concat( (String) b );
String a = “”;
boolean b = false;
String c =“Microsoft is
dishonest=”;
a = c + b;
Microsoft is
dishonest=false
Java I--Copyright © 2000 Tom Hunter
Creating and Working with Strings
• The += operator is also overloaded:
String c = “Microsoft “;
String b = “rules”;
c += b;
{ c == “Microsoft rules” }
a = b + c;
• True or False: the above statement is a Syntax error:
True, this is a syntax error. To force the
conversion to String, at least one of the
operands on the + sign must be a String.
Java I--Copyright © 2000 Tom Hunter
Creating and Working with Strings
• What about this example, where we’re concatenating a String
object to a regular object. What happens here?
String a = “”;
String b = “Test“;
Employee e = new Employee(“GW”,”Bush”);
a = b + e;
a = b + e.toString();
b “hello”
String a = “hello”;
String b = “hi”;
String c;
c = a;
c = b;
a = p + s;
ParsleySage
a = s + r; a
SageRosemary
a = p + s + r;
ParsleySageRosemary
• In another language,
such as C or C++, this would lead
ParsleySage
to the situation known as a
memory leak, when
SageRosemary
un-referenced objects a
clutter up the RAM
and the memory ParsleySageRosemary
cannot be reclaimed.
Java I--Copyright © 2000 Tom Hunter
Strings: Garbage Collection
String a = “Testing”;
int x = 0;
x = a.length(); {x == 7}
! Bonehead Alert !
To find the length of an array object, you use the
property length. (Notice, it’s not a method!)
String s = greet.substring( 0, 4 );
Howdy
The first argument 0,
is the 1st character
of the substring that
you do want to copy.
0 1 2 3 4 The 4 is the 1st
character that you
So...{s is equal to “Howd”} don’t want to copy.
Java I--Copyright © 2000 Tom Hunter
Finding
Individual
Characters:
charAt()
String a = “Testing”;
char b = ‘ ’;
b = a.charAt( 3 );
{ b == ‘t’} Testing
0 1 2 3 4 5 6
String a = “hello”;
String b = “hello”;
a.equals( b );
String b = “hello”;
“hello”.equals( b );
String b = “HELLO”;
“hello”.equalsIgnoreCase( b );
n = b.trim();
{ n == “hacker heaven” }
StringBuffer e;
StringBuffer e;
StringBuffer e;
d = “Ten” + 4 + “ya”;
d=new StringBuffer().append(“Ten”).append(4).append(“ya”);
charAt() jumbled
char x; 0 12 3 4 5 6
StringBuffer j;
j = new StringBuffer(“jumbled”);
x = j.charAt( 3 );
{ x == ‘b’ }
Java I--Copyright © 2000 Tom Hunter
Method
substring()
{ z == ‘mbl’ }
Java I--Copyright © 2000 Tom Hunter
Method
reverse()
StringBuffer king;
king = new StringBuffer( “murder” );
king.reverse();
{ king.toString() == “redrum” };
StringBuffer sb;
sb = new StringBuffer(“Test”);
sb.ensureCapacity( 19 );
• What would be the capacity of sb after this statement?
20 = 16 + 4
sb.ensureCapacity( 21 );
42 = 20*2 + 2
• If you asked for less that was already allocated, nothing
changes.
• However, if you asked to increase the allocation, it gives
you the larger of two-times the original plus 2 OR your
request.
Java I--Copyright © 2000 Tom Hunter
StringBuffer Class: ensureCapacity()
• Finally, let’s test that:
StringBuffer sb; original
sb = new StringBuffer(“Test”); 20
sb.ensureCapacity( 43 );
43
• Since this request is larger than two-times the original
allocation, plus two, our request prevails.
int x = “}”;
Character.isDefined( x );
int x = “t”;
Character.isLetter( x );
int x = “3”;
Character.isLetter( x );
—space,
—tab or
—newline
int countTokens()
boolean hasMoreTokens()
boolean hasMoreElements()
Object nextElement()
String nextToken()
int countTokens()
boolean hasMoreTokens()
boolean hasMoreElements()
String nextToken()