strings methods slides java aplus
DESCRIPTION
APLUS Comp Sci PresentationTRANSCRIPT
©A+ Computer Science www.apluscompsci.com 1
©A+ Computer Science www.apluscompsci.com 2
A String is a group of characters. Strings are used to store words,
which can consist of letters, numbers, and symbols.
©A+ Computer Science www.apluscompsci.com 3
s is a String reference. s is storing the location / memory address of
the String Object "compsci";
champ is a String reference. champ is storing the location /
memory address of the String Object "uilstate";
©A+ Computer Science www.apluscompsci.com 4
s is a String reference. s is storing the location / memory address of
the String Object "compsci";
©A+ Computer Science www.apluscompsci.com 5
s is a String reference. s is storing the location / memory address of
the String Object "compsci";
©A+ Computer Science www.apluscompsci.com 6
©A+ Computer Science www.apluscompsci.com 7
©A+ Computer Science www.apluscompsci.com 8
String is an immutable Object. String cannot be changed. All of
the String methods are accessor method. All of the String methods
are return methods.
©A+ Computer Science www.apluscompsci.com 9
The String length() method returns the character count.
length() looks at the String Object and returns back the number
of characters contained.
compsci contains 7 characters so a call to length() would
return 7.
©A+ Computer Science www.apluscompsci.com 10
Return methods typically perform some action then send back
a value. Return methods are also used as get methods to
retrieve a value from an Object.
©A+ Computer Science www.apluscompsci.com 11
The String charAt() method returns the character at the specific
spot.
charAt(0) would return the character at spot 0.
charAt(2) would return the character at spot 2.
©A+ Computer Science www.apluscompsci.com 12
©A+ Computer Science www.apluscompsci.com 13
©A+ Computer Science www.apluscompsci.com 14
The String substring() method returns a String containing a
section from the original String.
©A+ Computer Science www.apluscompsci.com 15
The String substring() method returns a String containing a
section from the original String.
©A+ Computer Science www.apluscompsci.com 16
©A+ Computer Science www.apluscompsci.com 17
The String indexOf() method looks for a value and returns the
spot at which that value is stored. If the value provided is not
present in the String, -1 is returned. -1 would not be a valid spot in
the String which is why -1 was chosen as the return value when a
value is not found.
©A+ Computer Science www.apluscompsci.com 18
The String indexOf() method looks for a value and returns the
spot at which that value is stored. If the value provided is not
present in the String, -1 is returned. -1 would not be a valid spot in
the String which is why -1 was chosen as the return value when a
value is not found.
©A+ Computer Science www.apluscompsci.com 19
©A+ Computer Science www.apluscompsci.com 20
It is very common to add strings together make a new string.
Methods could be used as well as using the plus + operator.
©A+ Computer Science www.apluscompsci.com 21
©A+ Computer Science www.apluscompsci.com 22
©A+ Computer Science www.apluscompsci.com 23
©A+ Computer Science www.apluscompsci.com 24
Return methods typically perform some action then send back
a value. Return methods are also used as get methods to
retrieve a value from an Object.
©A+ Computer Science www.apluscompsci.com 25
Return methods typically perform some action then send back
a value. Return methods are also used as get methods to
retrieve a value from an Object.
©A+ Computer Science www.apluscompsci.com 26
Return methods typically perform some action then send back
a value. Return methods are also used as get methods to
retrieve a value from an Object.
©A+ Computer Science www.apluscompsci.com 27
Method twice is a return method. Method twice takes in
parameter x and then sends back x multiplied by 2.
©A+ Computer Science www.apluscompsci.com 28
Method twice is a return method. Method twice takes in
parameter x and then sends back x multiplied by 2.
©A+ Computer Science www.apluscompsci.com 29
©A+ Computer Science www.apluscompsci.com 30
©A+ Computer Science www.apluscompsci.com 31
toString() is used to display an Object. print() and
println() automatically call toString() when displaying an
Object reference. toString() typically sends back all
data/properties from an Object as one String.
©A+ Computer Science www.apluscompsci.com 32
©A+ Computer Science www.apluscompsci.com 33
©A+ Computer Science www.apluscompsci.com 34
©A+ Computer Science www.apluscompsci.com 35
©A+ Computer Science www.apluscompsci.com 36
©A+ Computer Science www.apluscompsci.com 37
©A+ Computer Science www.apluscompsci.com 38
©A+ Computer Science www.apluscompsci.com 39
©A+ Computer Science www.apluscompsci.com 40
©A+ Computer Science www.apluscompsci.com 41
©A+ Computer Science www.apluscompsci.com 42