Chapter 4 Lecture 4-3: Strings, char reading - Building Java Programs

TOPIC 8 EXERCISES. Tracing Exercises. 1. Suppose you have the following declaration. String str;. Which of the following values can be assigned to the 

Part of the document

Copyright 2010 by Pearson Education
1
Building Java Programs
Chapter 4 Lecture 4-3: Strings, char
reading: 3.3, 4.3-4.4 self-check: Ch. 4 #12, 15 exercises: Ch. 4 #15, 16 videos: Ch. 3 #3
Copyright 2010 by Pearson Education
2
Copyright 2010 by Pearson Education
3
Objects (usage)
object: An entity that contains data and behavior.
data: variables inside the object — behavior: methods inside the object
You interact with the methods;
the data is hidden in the object.
A class is a type of objects.
Constructing (creating) an object:
Type objectName = new Type(parameters);

Calling an object's method:
objectName.methodName(parameters);
Copyright 2010 by Pearson Education
4
Strings
string: An object storing a sequence of text characters.
Unlike most other objects, a String is not created with new. String name = "text"; String name = expression; Examples:

String name = "Marla Singer";

int x = 3; int y = 5; String point = "(" + x + ", " + y + ")";
Copyright 2010 by Pearson Education
5
Indexes
Characters of a string are numbered with 0-based indexes:

String name = "Ultimate"; First character's index : 0 Last character's index : 1 less than the string's length The individual characters are values of type char (seen later)
index 0 1 2 3 4 5 6 7 character U l t i m a t e
Copyright 2010 by Pearson Education
6
String methods