Download presentation
Presentation is loading. Please wait.
Published byAdelia Barnett Modified over 9 years ago
1
String & Composition
2
Agenda This keyword. String class. String operations. Composition.
3
this keyword
4
The this Keyword Use this to refer to an instance data field. Use this to invoke an overloaded constructor of the same class. 1-4
5
Example 1-5
6
String Class 1-6
7
To create string you make : –String newString = new String(stringLiteral); EX: –String message = new String("Hello Java"); Since strings are used frequently, Java provides a shorthand initializer for creating a string: –String message = "Welcome to Java"; Constructing Strings 1-7
8
A String object is immutable; Its contents cannot be changed. Does the following code change the contents of the string? –String s = "Java"; –s = "HTML"; Strings Are Immutable 1-8
9
Trace Code String s = "Java"; s = "HTML"; 1-9
10
String Operations
11
The String Class With this class you can Obtaining String length. Retrieving Individual Characters in a string. String Concatenation. SubStrings. String Comparison. String Conversions. Finding a character or substring in a string. 1-11
12
Finding String Length Finding string length using the length() method: Ex : message = "Welcome"; message.length(); (returns 7) 1-12
13
Get Character from a String Do not use message[0] Use message.charAt(index) Note: Index starts from 0 1-13
14
String Concatenation String s3 = s1.concat(s2); String s3 = s1 + s2; Note : s1 + s2 + s3 same as (s1.concat(s2)).concat(s3); 1-14
15
Extracting Substrings You can extract a single character from a string using the charAt method. You can also extract a substring from a string using the substring method in the String class. String s1 = "Welcome to Java"; String s2 = s1.substring(0, 11) + "HTML"; 1-15
16
String Comparisons equals(object) String s1 = new String("Welcome“); String s2 = "welcome"; if (s1.equals(s2)){ // s1 and s2 have the same contents } if (s1 == s2) { // s1 and s2 have the same reference 1-16
17
String Conversions The contents of a string cannot be changed once the string is created. But you can convert a string to a new string using the following methods: toLowerCase() toUpperCase() trim() 1-17
18
Finding a Character or a Substring in a String "Welcome to Java".indexOf('W') returns 0. "Welcome to Java".indexOf('x') returns -1. "Welcome to Java".indexOf('o', 5) returns 9. "Welcome to Java".indexOf("come") returns 3. "Welcome to Java".indexOf("Java", 5) returns 11. "Welcome to Java".indexOf("java", 5) returns -1. "Welcome to Java".lastIndexOf('a') returns 14. 1-18
19
Convert to Strings The String class provides several static valueOf methods for converting to strings. For example, to convert a double value to a string, use String.valueOf(5.44). The return value is string consists of characters ‘5’, ‘.’, ‘4’, and ‘4’. 1-19
20
Example: Finding Palindromes Objective: Checking whether a string is a palindrome: a string that reads the same forward and backward 1-20
21
Composition
22
A class can have references to objects of other classes as members. Such a capability is called composition and is sometimes referred to as a has-a relationship. For example, an object of class AlarmClock needs to know the current time and the time when it is supposed to sound its alarm. 1-22
23
Questions
24
Thanks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.