CSE102 - Week 5

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

CSE 102

Principles of PROGRAMMING

Lecture 5 - The String class

Slides prepared by Rose Williams, Binghamton University


Department of Computer Science,
FUD - 2024
The Class String
 There is no primitive type for strings in Java
 The class String is a predefined class in Java that
is used to store and process strings

 Objects of type String are made up of strings of


characters that are written within double quotes
◦ Any quoted string is a constant of type String
"Live long and prosper.“

 A variable of type String can be given the value


of a String object
String blessing = "Live long and prosper.";

4-2
Concatenation of Strings
 Concatenation: Using the + operator on two
strings in order to connect them to form one
longer string
◦ If greeting is equal to "Hello ", and javaClass is equal
to "class", then greeting + javaClass is equal to
"Hello class"
 Any number of strings can be concatenated
together
 When a string is combined with almost any other
type of item, the result is a string
◦ "The answer is " + 42 evaluates to
"The answer is 42"

4-3
Classes, Objects, and Methods
 A class is the name for a type whose values are
objects
 Objects are entities that store data and take
actions
◦ Objects of the String class store data consisting of
strings of characters
 The actions that an object can take are called
methods
◦ Methods can return a value of a single type and/or
perform an action
◦ All objects within a class have the same methods, but
each can have different data values

4-4
Classes, Objects, and Methods
 Invoking or calling a method: a method is
called into action by writing the name of
the calling object, followed by a dot,
followed by the method name, followed by
parentheses
◦ This is sometimes referred to as sending a
message to the object
◦ The parentheses contain the information (if any)
needed by the method
◦ This information is called an argument (or
arguments)

4-5
String Methods
 The String class contains many useful methods
for string-processing applications
◦ A String method is called by writing a String object, a
dot, the name of the method, and a pair of parentheses
to enclose any arguments
◦ If a String method returns a value, then it can be placed
anywhere that a value of its type can be used
String greeting = "Hello";
int count = greeting.length();
System.out.println("Length is " +
greeting.length());
◦ Always count from zero when referring to the position or
index of a character in a string

4-6
Some Methods in the Class String
(Part 1 of 8)

4-7
Some Methods in the Class String
(Part 2 of 8)

4-8
Some Methods in the Class String
(Part 3 of 8)

© 2006 Pearson Addison-Wesley. All


rights reserved 4-9
Some Methods in the Class String
(Part 4 of 8)

4-
10
Some Methods in the Class String
(Part 5 of 8)

4-
11
Some Methods in the Class String
(Part 6 of 8)

4-
12
Some Methods in the Class String
(Part 7 of 8)

4-
13
Some Methods in the Class String
(Part 8 of 8)

4-
14
String Indexes

4-
15
String Processing
 A String object in Java is considered to be
immutable, i.e., the characters it contains cannot
be changed.
 There is another class in Java called
StringBuffer that has methods for editing its
string objects
 However, it is possible to change the value of a
String variable by using an assignment
statement
String name = "Soprano";
name = "Anthony " + name;

4-
16
Character Sets
 ASCII: A character set used by many
programming languages that contains all the
characters normally used on an English language
keyboard, plus a few special characters
◦ Each character is represented by a particular number
 Unicode: A character set used by the Java
language that includes all the ASCII characters
plus many of the characters used in languages
with a different alphabet from English

4-
17
Naming Constants
 Instead of using "anonymous" numbers in a
program, always declare them as named
constants, and use their name instead
public static final int INCHES_PER_FOOT = 12;
public static final double RATE = 0.14;
◦ This prevents a value from being changed inadvertently
◦ It has the added advantage that when a value must be
modified, it need only be changed in one place
◦ Note the naming convention for constants: Use all
uppercase letters, and designate word boundaries with
an underscore character

4-
18
Comments
 A line comment begins with the
symbols //, and causes the compiler to
ignore the remainder of the line
◦ This type of comment is used for the code
writer or for a programmer who modifies the
code
 A block comment begins with the symbol
pair /*, and ends with the symbol pair */
◦ The compiler ignores anything in between
◦ This type of comment can span several lines
◦ This type of comment provides documentation
for the users of the program

4-
19
Program Documentation
 Java comes with a program called javadoc
that will automatically extract
documentation from block comments in
the classes you define
◦ As long as their opening has an extra asterisk
(/**)

 Ultimately, a well written program is self-


documenting
◦ Its structure is made clear by the choice of
identifier names and the indenting pattern
◦ When one structure is nested inside another,
the inside structure is indented one more level

4-
20
Comments and a Named Constant

4-
21

You might also like