0% found this document useful (0 votes)
5 views20 pages

Understanding Java String Objects

This lecture covers the basics of String objects in programming, including how to instantiate them, their memory representation, and basic methods such as length, charAt, and substring. It also explains string concatenation, input methods, and escape sequences for special characters. Exercises and readings for the next lecture are provided at the end.

Uploaded by

ogonendiyeb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views20 pages

Understanding Java String Objects

This lecture covers the basics of String objects in programming, including how to instantiate them, their memory representation, and basic methods such as length, charAt, and substring. It also explains string concatenation, input methods, and escape sequences for special characters. Exercises and readings for the next lecture are provided at the end.

Uploaded by

ogonendiyeb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ISS112

Introduction to programming

Lecture 7 String Object


LECTURE OBJECTIVES

• By the end of this lecture you must be able to:


• Instantiate String object & invoke its basic
methods
• Draw Memory Model diagrams (objects)

(JFE, Chapter 2)

2
1. String Object

• An object which holds a sequence of characters


• String variables must be declared and initialized
Assigning a value to a string variable & display the value)
Example
String name;
name = “Mary”; name
null 71 99
[Link](“%s”, name); 71 “Mary”
name = “Jones”;
99 “Jones”
[Link](“%s\n”, name);
What is the output?

3
String Storage

• Remember: a string is a sequence of characters


• All characters take up positions, including spaces and all non-letter,
non-number characters
• Each char inside a String has an index number:
– The first character is located at position 0, second at position 1, ...
• Last character is located at index number of characters - 1
Example
String myString = ”Hello Java!”;

4
How is variable myString represented in memory?

Reference variable Address String object

Address myString
70 70 ”Hello Java!”

• A string variable stores the address of memory


location holding the actual string object.
• Assumed address 70 in the above example.
• Below is a simplified representation of the above
myString
70

70 ”Hello Java!”
5
2. String Concatenation
• Joins two strings into one
• Use + to do that! firstName
55 55 “Mary”
Example
lastName
String firstName = “Mary”; 65 “Jones”
65
String lastName = “Jones”;
fullName 67 “MaryJones”
67 87
87 “Mary Jones”
String fullName = firstName + lastName; // value :“MaryJones“
fullName = firstName + “ “ + lastName; //value: “Mary Jones"
Note
• + can be used to add numbers AND join (add) Strings
• This is called Operator Overloading:
• + operator does different things depending on the types of
operands.
6
Concatenating String with other data types

• We have seen an overloaded operator already:


Example
int myInt = 4;
String str1, str2, str3, myStr = ”Hello!”;
double myDouble = 9.9;
char myChar = ’T’;
str1 = myStr + myInt;
str2 = myChar + myStr;
str3 = myStr + myDouble;
[Link](“%s\n”, str1);
[Link]( “%s\n”, myInt + myStr);
[Link](“%s\n”, str2);
[Link](“%s\n”, str3);
7
3. String Input

• Methods for reading string input


1. String next()
• Reads the next value of type String that is entered
at the keyboard.
• Reads one word at a time
E.g. String department = [Link]( );

2. String nextLine()
• Reads the next value of type String that is entered
at the keyboard until the user hits the ‘Enter’ key
E.g. String fullName = [Link]( );
8
Example Code
import [Link];
public class PracticeStrings
{
public static void main(String[] args)
{
String word1, word2;
Scanner kBoard = new Scanner([Link]);
// Prompt and read input into already declared variables
[Link](”Enter student’s full name”);
word1 = [Link]();
[Link](”Enter Course code”);
word2 = [Link]();
// Display some output
[Link]( “%s has enrolled for %s\n“ , word1, word2);
}
} 9
4. Basic String Methods
a. int length()
• returns the number of characters in a string

Example
// recall : String myString = ”Hello Java!”;
int strLength = [Link]();
[Link](“%d\n”, strLength); // prints 11 to the screen

NOTE:
• A value returned by a method can be assigned to an
appropriate variable or it can be used in an expression
• E.g. int doubleLength = 2 * [Link]();
10
4. Basic String Methods continued

b. char charAt(int position)


• Returns the character at the specified position.
• Each char inside a String has an index number:
• The first char is index zero (0)
• The charAt method returns a char at a given index
inside a String:
Example
int i = 4;
String name = “Harry”;
char start = [Link](0);
char last = [Link](i);
11
4. Basic String Methods continued

c. String substring (int startPosition)


• returns a copy of the string starting from startPosition
until the end of the string.
• startPosition MUST be a valid position.

Example
String name = “Harry“;
String newName;
newName = [Link](1);
[Link](“ %s\n“,newName);

12
4. Basic String Methods continued

d. String substring (int startPosition, int endPosition)


• returns a copy of the string starting from startPosition
until endPosition -1.
• startPosition and endPosition must be valid positions
and startPosition < endPosition.

Example
String name = “Harry“;
String newName;
newName = [Link](1,4);
[Link](“ %s\n“,newName);

13
4. Basic String Methods continued

e. int indexOf (char ch)


• returns index of the first occurrence of character specified
by ch.
• returns -1 if ch is not found.

Example
String name = “Harry“;
int pos;
pos = [Link](‘r’);
[Link](“ %d\n“, pos);
What would be the result of [Link](‘h’)?

14
4. Basic String Methods continued

f. int indexOf (char ch, int beginPosition)


• returns index of the first occurrence of character specified
by ch from position specified by beginPosition.
• returns -1 if ch is not found.

Example
String name = “Harry“;
int pos;
pos = [Link](‘r’, 3);
[Link](“ %d\n“, pos);
What would be the result of [Link](‘r’, 4)?

15
4. Basic String Methods continued

g. int indexOf (String str)


• returns index of the first occurrence of String specified by
str.
• returns -1 if str is not found.

Example
String name = “Harry“;
int pos;
pos = [Link](“rr”);
[Link](“ %d\n“, pos);
What would be the result of [Link](“are”)?

16
4. Basic String Methods continued

f. int indexOf (String str, int beginPosition)


• returns index of the first occurrence of string specified by
str from position specified by beginPosition.
• returns -1 if character is not found.

Example
String name = “Harry“;
int pos;
pos = [Link](“rr”, 2);
[Link](“ %d\n“, pos);
What would be the result of [Link](“rr”, 3)?

17
5. String Escape Sequences

• How would you print a double quote?


• Preface the " with a \ inside the double quoted String
[Link](“He said \“Hello\“ “);
• OK, then how do you print a backslash?
• Preface the \ with another \ character
[Link](“C:\\Temp\\[Link]“);
• Special characters inside Strings
• Output a newline with a ‘\n’ *
[Link](“*\n**\n***\n“); **
***
18
5. Summary

- String & basic string methods


- Memory diagram for an object

• Exercises
• R2.4, R2.7, R2.8, R2.15 & R2.17
• P2.11, P2.12, P2.13, P2.16, P2.17 & P2.18

• Reading for next Lecture

19
Computing Skills 20

You might also like