Lecture5 - ICT102 - T222 For Lecture Class
Lecture5 - ICT102 - T222 For Lecture Class
Lecture5 - ICT102 - T222 For Lecture Class
Introduction to Programming
Lecture 5 – Strings
Focus for this week
• The value 25 will be stored in the memory location associated with the variable number.
• Objects are not stored in variables, however. Objects are referenced by variables.
• Object − Objects have states and behaviors. Example: A dog has states - color, name,
breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of
a class.
• Software objects also have a state and a behavior. A software object's state is stored in
fields and behavior is shown via methods.
Objects in real-world
Group 1
Group 2
Group 3
Group 4
2-9
String Objects
• A variable can be assigned a String literal.
String value = "Hello";
System.out.println(greeting + name);
}
2-12
• This statement runs the length method on the object pointed to by the value variable.
// This program demonstrates the String class's length method.
{
String name = "Herman";
int stringSize;
stringSize = name.length();
" characters.");
String Methods
• The String class contains many methods that help with the manipulation of String
objects.
• when we use String methods they return new string objects, they do not change the
original string object which is using those methods
// This program demonstrates a few of the String methods.
{
public static void main(String[] args)
{
String message = "Java is Great Fun!";
String upper = message.toUpperCase();
String lower = message.toLowerCase();
char letter = message.charAt(2);
int stringSize = message.length();
System.out.println(message);
System.out.println(upper);
System.out.println(lower);
System.out.println(letter);
System.out.println(stringSize);
}
} See example: StringMethods.java
2-16
Practice Question
• Write a program to store your name in Java, find the length of your name, Print name in
all CAP letters.
2-17
String Concatenation
• Java commands that have string literals must be treated with care.
• A string literal value cannot span lines in a Java source code file.
System.out.println("This line is too long and now it
has spanned more than one line, which will cause a
syntax error to be generated by the compiler. ");
2-18
String Concatenation
• The String concatenation operator can be used to fix this problem.
System.out.println("These lines are " +
"are now ok and will not " +
"cause the error as before.");
String Concatenation
• The Concatenation operator can be used to
format complex String objects.
System.out.println("The following will be printed " +
"in a tabbed format: " +
\n\tFirst = " + 5 * 6 + ", " +
"\n\tSecond = " (6 + 4) + "," +
"\n\tThird = " + 16.7 + ".");
• Unless the references point to the same object, the relational operators will not return
true.
// compares two String objects using the equals method.
name2 = "Mark",
name3 = "Mary";
• In the String class the equals and compareTo methods are case sensitive.
• In order to compare two String objects that might have different case, use:
– equalsIgnoreCase, or
– compareToIgnoreCase
9-23
Searching Strings
• The startsWith method determines whether a
string begins with a specified substring.
Searching Strings
• The String class also provides methods that will
locate the position of a substring.
– indexOf
returns the first location of a substring or character in the calling String
Object.
– lastIndexOf
returns the last location of a substring or character in the calling String
Object.
9-26
Extracting Substrings
String fullName = "Cynthia Susan Smith";
String lastName = fullName.substring(14);
Address “Smith”
9-27
– concat
Returns a String object that is the concatenation of two
String objects. s1.concat(”number”);
– replace
• Returns a String object with all occurrences of one character being
replaced by another character. s1.replace(‘i’,”u");
– trim
Returns a String object with all leading and trailing
whitespace characters removed. s1.trim(””);
9-28
Check Point
• Look at the following code:
String str1 = “To be, or not to be”;
String str2 = str1.replace(‘o’, ‘u’);
System.out.println(str1);
System.out.println(str2);
You hear a fellow student claim that the code will display
the following:
Tu be ur nut tu be
Tu be ur nut tu be
Is your fellow student right or wrong? Why?
2-29
Creating Constants
• Many programs have data that does not need to
be changed.
• Littering programs with literal values can make the
program hard do read and maintain.
• Replacing literal values with constants remedies
this problem.
• Constants allow the programmer to use a name
rather than a value throughout the program.
• Constants also give a singular point for changing
those values when needed.
2-30
Creating Constants
• Constants are declared using the keyword final.
• By convention, constants are all upper case and words are separated by the underscore
character.