Strings - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Strings

Description:

Every time you use a '' string, a string object is created automatically. ... Java adopts this immutability restriction to implement an efficient memory ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 18
Provided by: csMi8
Learn more at: https://www.cs.miami.edu
Category:

less

Transcript and Presenter's Notes

Title: Strings


1
Strings
  • A string is a sequence of characters that is
    treated as a single value. Strings are objects.
  • We have been using strings all along. For
    example, to display text
  • Every time you use a string, a string object
    is created automatically.
  • String is a class in the java.lang package.

2
Explicit String Objects
  • A declaration and object creation are needed for
    instances of the String class. For example,

We normally use shorthand notation (only for
Strings)
3
Explicit String Objects
  • Blitititi out to reality StringVar.java
  • Blitititi out to reality StringVar2.java
  • Blitititi out to reality StringVar2.java

4
String variables are References - 1
Code
Both word1 and word2 are allocated memory (to
store references), but the objects themselves are
not yet created, so they both contain null.
L
word1
word2
L
State of Memory
5
String variables are References - 2
Code
One String object is created and assigned to
word1, so word1 contains the address of this
object.
word1
word2
L
State of Memory
6
String variables are References - 3
Code
Content of word1, which is an address, is
assigned to word2, making word2 refer to the same
object.
State of Memory
  • Gadzook out to reality StringAlias.java

7
Concatenating Strings
  • The operator can also concatenate strings.
  • A new string object is created - the operands are
    not affected
  • Gadzook out to reality StringCat.java

8
Command line Strings
  • The formal arguments to the main method receive
    strings from the command line arguments.
  • When running a program, supply command line
    arguments after the program name, e.g.,
  • java MyJavaProgram cat 27 'Java is great!'
  • has three command line arguments.
  • Gadzook out to reality StringCat.java

9
Accessing Individual Elements
  • Individual characters in a String accessed with
    the charAt method.

10
Determining the Size
  • We determine the number of characters in a String
    with the length method.

Error because no object is created for str3, so
it is a null.
  • Olipidoo out to reality StringVowels.java
  • Olipidoo out to reality StringWords.java

11
Equality () vs. equalsCase 1
word1 and word2 point to the same object.
word1 word2
word1.equals( word2 )
12
Equality () vs. equalsCase 2
word1
word2
word1 and word2 point to different objects having
the same string.
word1 word2
word1.equals( word2 )
13
Equality () vs. equalsCase 3
word1
word2
word1 and word2 point to different objects with
different strings.
word1 word2
word1.equals( word2 )
  • Quixote out to reality StringJavas.java

14
Other Useful String Operators
15
Strings and Methods
  • Strings are reference variables, so (like arrays)
    formal parameters point to the actual parameter
    data
  • Strings returned from methods point to the data
    created in the method
  • Likidylik out to reality StringMethod.java

16
Strings are Immutable
  • A String object is immutable, which means that
    once a String object is created, is cannot be
    changed.
  • It is not possible to add, delete, or modify
    characters of a String object.
  • The methods of the String class, such as
    toUpperCase and substring, do not modify the
    original string they return a new string.
  • Java adopts this immutability restriction to
    implement an efficient memory allocation scheme
    for managing String objects.
  • Oieeeeiooo out to reality StringImmut.java

17
StringBuffer
  • Creating a new string from the old one will work
    for most cases, but sometimes manipulating the
    content of a string directly is more convenient.
  • Manipulation means operations such as replacing a
    character, appending a string with another
    string, deleting a portion of a string, and so
    forth.
  • Java has a StringBuffer class for this.
  • StringBuffers are created from strings (no
    shorthand)
  • Ay out to reality StringBufferX.java
  • Ay out to reality StringBufferMake.java
Write a Comment
User Comments (0)
About PowerShow.com